Automattic\WooCommerce\Internal\Orders

OrderActionsRestController::send_emailprotectedWC 1.0

Callback to run for POST wc/v3/orders/(?P<id>[\d]+)/actions/send_email.

Method of the class: OrderActionsRestController{}

Returns

Array|WP_Error.

Usage

// protected - for code of main (parent) or child class
$result = $this->send_email( $request );
$request(WP_REST_Request) (required)
The incoming HTTP REST request.

OrderActionsRestController::send_email() code WC 10.8.1

protected function send_email( WP_REST_Request $request ) {
	$order = wc_get_order( $request->get_param( 'id' ) );

	if ( ! $order instanceof WC_Order ) {
		return new WP_Error( 'woocommerce_rest_not_found', __( 'Order not found.', 'woocommerce' ), array( 'status' => 404 ) );
	}

	$email       = $request->get_param( 'email' );
	$force       = wp_validate_boolean( $request->get_param( 'force_email_update' ) );
	$template_id = $request->get_param( 'template_id' );
	$messages    = array();

	if ( $email ) {
		$message = $this->maybe_update_billing_email( $order, $email, $force );
		if ( is_wp_error( $message ) ) {
			return $message;
		}
		$messages[] = $message;
	}

	if ( ! is_email( $order->get_billing_email() ) ) {
		return new WP_Error(
			'woocommerce_rest_missing_email',
			__( 'Order does not have an email address.', 'woocommerce' ),
			array( 'status' => 400 )
		);
	}

	$available_templates = $this->get_available_email_templates( $order );

	if ( empty( $template_id ) ) {
		$template = $this->select_default_template( $order, $available_templates );

		if ( is_null( $template ) ) {
			return new WP_Error(
				'woocommerce_rest_no_email_template',
				__( 'No email template is available for this order.', 'woocommerce' ),
				array( 'status' => 400 )
			);
		}

		$template_id = $template->id;
	} else {
		$template = $this->get_email_template_by_id( $template_id, $available_templates );

		if ( is_null( $template ) ) {
			return new WP_Error(
				'woocommerce_rest_invalid_email_template',
				sprintf(
					// translators: %s is a string ID for an email template.
					__( '%s is not a valid template for this order.', 'woocommerce' ),
					esc_html( $template_id )
				),
				array( 'status' => 400 )
			);
		}
	}

	switch ( $template_id ) {
		// phpcs:disable WooCommerce.Commenting.CommentHooks.MissingSinceComment
		case 'customer_completed_order':
			/** This action is documented in includes/class-wc-emails.php */
			do_action( 'woocommerce_order_status_completed_notification', $order->get_id(), $order );
			break;
		case 'customer_failed_order':
			/** This action is documented in includes/class-wc-emails.php */
			do_action( 'woocommerce_order_status_failed_notification', $order->get_id(), $order );
			break;
		case 'customer_on_hold_order':
			/** This action is documented in includes/class-wc-emails.php */
			do_action( 'woocommerce_order_status_pending_to_on-hold_notification', $order->get_id(), $order ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
			break;
		case 'customer_processing_order':
			/** This action is documented in includes/class-wc-emails.php */
			do_action( 'woocommerce_order_status_pending_to_processing_notification', $order->get_id(), $order );
			break;
		case 'customer_refunded_order':
		case 'customer_pos_refunded_order':
			if ( $this->order_is_partially_refunded( $order ) ) {
				/** This action is documented in includes/class-wc-emails.php */
				do_action( 'woocommerce_order_partially_refunded_notification', $order->get_id() );
			} else {
				/** This action is documented in includes/class-wc-emails.php */
				do_action( 'woocommerce_order_fully_refunded_notification', $order->get_id() );
			}
			break;
		// phpcs:enable WooCommerce.Commenting.CommentHooks.MissingSinceComment

		case 'customer_invoice':
			return $this->send_order_details( $request );

		default:
			/**
			 * Action to trigger sending a custom order email template from a REST API request.
			 *
			 * The email template must first be made available for the associated order.
			 * See the `woocommerce_rest_order_actions_email_valid_template_classes` filter hook.
			 *
			 * @since 9.8.0
			 *
			 * @param int    $order_id    The ID of the order.
			 * @param string $template_id The ID of the template specified in the API request.
			 */
			do_action( 'woocommerce_rest_order_actions_email_send', $order->get_id(), $template_id );
			break;
	}

	$user_agent = esc_html( $request->get_header( 'User-Agent' ) );
	$messages[] = sprintf(
		// translators: 1. The name of an email template; 2. Email address.
		esc_html__( 'Email template "%1$s" sent to %2$s.', 'woocommerce' ),
		esc_html( $template->get_title() ),
		esc_html( $order->get_billing_email() )
	);

	$messages = array_filter( $messages );
	foreach ( $messages as $message ) {
		$order->add_order_note(
			$message,
			0,
			true,
			array(
				'user_agent' => $user_agent ? $user_agent : 'REST API',
				'note_group' => OrderNoteGroup::EMAIL_NOTIFICATION,
			)
		);
	}

	return array(
		'message' => implode( ' ', $messages ),
	);
}