Automattic\WooCommerce\Internal\Orders

OrderActionsRestController::send_email()protectedWC 1.0

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

Метод класса: OrderActionsRestController{}

Возвращает

Массив|WP_Error.

Использование

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->send_email( $request );
$request(WP_REST_Request) (обязательный)
The incoming HTTP REST request.

Код OrderActionsRestController::send_email() WC 9.8.4

protected function send_email( WP_REST_Request $request ) {
	$order       = wc_get_order( $request->get_param( 'id' ) );
	$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 );
	$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':
			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; 3. User-agent that requested the action.
		esc_html__( 'Email template "%1$s" sent to %2$s, via %3$s.', 'woocommerce' ),
		esc_html( $template->get_title() ),
		esc_html( $order->get_billing_email() ),
		$user_agent ? $user_agent : 'REST API'
	);

	$messages = array_filter( $messages );
	foreach ( $messages as $message ) {
		$order->add_order_note( $message, false, true );
	}

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