Представим ситуацию что пользователь не оплатил во время (в течение 1 часа по умолчанию) заказ и он перешел в статус Отменен. Либо мы намерено изначально создаём заказ в статусе На удержании, а после одобрения менеджером, переводим его в статус В ожидании оплаты. В этих случаях было бы удобно автоматически отправлять клиенту ссылку на оплату.
Функции
// Отправка письма клиенту при статусе «В ожидании оплаты» (с ссылкой на оплату)
add_action( 'woocommerce_order_status_changed', 'pending_new_order_notification', 20, 1 );
function pending_new_order_notification( $order_id ) {
$order = wc_get_order( $order_id );
if( $order->has_status( 'pending' ) ) { // Only for "pending" order status
// load the mailer class
$mailer = WC()->mailer();
//format the email
$recipient = $order->get_billing_email();
$subject = __("Заказ можно оплачивать", 'theme_name');
$content = get_custom_email_html( $order, $subject, $mailer );
$headers = "Content-Type: text/html\r\n";
//send the email through wordpress
$mailer->send( $recipient, $subject, $content, $headers );
}
}
function get_custom_email_html( $order, $heading = false, $mailer ) {
$template = 'emails/order_can_paid.php';
return wc_get_template_html( $template, array(
'order' => $order,
'email_heading' => $heading,
'sent_to_admin' => false,
'plain_text' => false,
'email' => $mailer
) );
}
Хук woocommerce_order_status_changed срабатывает при изменении статуса заказа.
Шаблон письма
В папке woocommerce/emails создаем новый шаблон order_can_paid.php содержимое которого можно скопировать из другого стандартного шаблона customer-processing-order.php, но с изменениями. Содержимое нового шаблона:
<?php
/**
* Customer processing order email
*
* This template can be overridden by copying it to yourtheme/woocommerce/emails/customer-processing-order.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* @see https://docs.woocommerce.com/document/template-structure/
* @package WooCommerce/Templates/Emails
* @version 3.7.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/*
* @hooked WC_Emails::email_header() Output the email header
*/
do_action( 'woocommerce_email_header', $email_heading, $email ); ?>
<?php /* translators: %s: Customer first name */ ?>
<p><?php printf( esc_html__( 'Hi %s,', 'woocommerce' ), esc_html( $order->get_billing_first_name() ) ); ?></p>
<?php /* translators: %s: Order number */ ?>
<p><?php printf( esc_html__( 'Ваш заказ #%s проверен менеджером, Вы можете его оплачивать:', 'woocommerce' ), esc_html( $order->get_order_number() ) ); ?></p>
<?php $pay_now_url = esc_url( $order->get_checkout_payment_url() ); ?>
<p>Перейдите по <a href="<?php echo $pay_now_url; ?>">ссылке</a> для оплаты заказа.</p>
<?php
/*
* @hooked WC_Emails::order_details() Shows the order details table.
* @hooked WC_Structured_Data::generate_order_data() Generates structured data.
* @hooked WC_Structured_Data::output_structured_data() Outputs structured data.
* @since 2.5.0
*/
do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );
/*
* @hooked WC_Emails::order_meta() Shows order meta data.
*/
do_action( 'woocommerce_email_order_meta', $order, $sent_to_admin, $plain_text, $email );
/*
* @hooked WC_Emails::customer_details() Shows customer details
* @hooked WC_Emails::email_address() Shows email address
*/
do_action( 'woocommerce_email_customer_details', $order, $sent_to_admin, $plain_text, $email );
/**
* Show user-defined additonal content - this is set in each email's settings.
*/
/*if ( $additional_content ) {
echo wp_kses_post( wpautop( wptexturize( $additional_content ) ) );
}*/
/*
* @hooked WC_Emails::email_footer() Output the email footer
*/
do_action( 'woocommerce_email_footer', $email );
Теперь пользователям автоматически будет приходить письмо и они смогут оплачивать заказ.
