我的目标是在订单状态为暂停且订单创建时间为 48 小时或更长时间时向客户发送一封包含自定义文本的电子邮件。
- 订单已超过 48 小时
- 向客户发送电子邮件
- 要求客户付款
- 包含订单链接(到我的帐户付款页面(
正在尝试使用我之前关于如果在 WooCommerce 中没有付款,则在 X 天后自动取消订单的问题之一的答案中的代码。
我稍微更改了代码:
add_action( 'restrict_manage_posts', 'on_hold_payment_reminder' );
function on_hold_payment_reminder() {
global $pagenow, $post_type;
if( 'shop_order' === $post_type && 'edit.php' === $pagenow
&& get_option( 'unpaid_orders_daily_process' ) < time() ) :
$days_delay = 5;
$one_day = 24 * 60 * 60;
$today = strtotime( date('Y-m-d') );
$unpaid_orders = (array) wc_get_orders( array(
'limit' => -1,
'status' => 'on-hold',
'date_created' => '<' . ( $today - ($days_delay * $one_day) ),
) );
if ( sizeof($unpaid_orders) > 0 ) {
$reminder_text = __("Payment reminder email sent to customer $today.", "woocommerce");
foreach ( $unpaid_orders as $order ) {
// HERE I want the email to be sent instead <=== <=== <=== <=== <===
}
}
update_option( 'unpaid_orders_daily_process', $today + $one_day );
endif;
}
这是我想与上述内容同步的电子邮件部分(阅读代码注释(:
add_action ('woocommerce_email_order_details', 'on_hold_payment_reminder', 5, 4);
function on_hold_payment_reminder( $order, $sent_to_admin, $plain_text, $email ){
if ( 'customer_on_hold_order' == $email->id ){
$order_id = $order->get_id();
echo "<h2>Do not forget about your order..</h2>
<p>CUSTOM MESSAGE HERE</p>";
}
}
那么,如何通过自定义文本为"暂停"订单发送电子邮件通知提醒呢?
以下代码将每天触发一次,并将发送电子邮件提醒,其中包含有关未付款订单的自定义消息(针对"暂停"订单状态(:
add_action( 'restrict_manage_posts', 'on_hold_payment_reminder' );
function on_hold_payment_reminder() {
global $pagenow, $post_type;
if( 'shop_order' === $post_type && 'edit.php' === $pagenow
&& get_option( 'unpaid_orders_reminder_daily_process' ) < time() ) :
$days_delay = 2; // 48 hours
$one_day = 24 * 60 * 60;
$today = strtotime( date('Y-m-d') );
$unpaid_orders = (array) wc_get_orders( array(
'limit' => -1,
'status' => 'on-hold',
'date_created' => '<' . ( $today - ($days_delay * $one_day) ),
) );
if ( sizeof($unpaid_orders) > 0 ) {
$reminder_text = __("Payment reminder email sent to customer $today.", "woocommerce");
foreach ( $unpaid_orders as $order ) {
$order->update_meta_data( '_send_on_hold', true );
$order->update_status( 'reminder', $reminder_text );
$wc_emails = WC()->mailer()->get_emails(); // Get all WC_emails objects instances
$wc_emails['WC_Email_Customer_On_Hold_Order']->trigger( $order->get_id() ); // Send email
}
}
update_option( 'unpaid_orders_reminder_daily_process', $today + $one_day );
endif;
}
add_action ( 'woocommerce_email_order_details', 'on_hold_payment_reminder_notification', 5, 4 );
function on_hold_payment_reminder_notification( $order, $sent_to_admin, $plain_text, $email ){
if ( 'customer_on_hold_order' == $email->id && $order->get_meta('_send_on_hold') ){
$order_id = $order->get_id();
$order_link = wc_get_page_permalink('myaccount').'view-order/'.$order_id.'/';
$order_number = $order->get_order_number();
echo '<h2>'.__("Do not forget about your order.").'</h2>
<p>'.sprintf( __("CUSTOM MESSAGE HERE… %s"),
'<a href="'.$order_link.'">'.__("Your My account order #").$order_number.'<a>'
) .'</p>';
$order->delete_meta_data('_send_on_hold');
$order->save();
}
}
代码进入活动子主题(或活动主题(的函数.php文件。经过测试并工作。