gpt4 book ai didi

php - 在 Woocommerce 中添加自定义订单状态并发送有关状态更改的电子邮件

转载 作者:行者123 更新时间:2023-12-05 04:07:33 29 4
gpt4 key购买 nike

我想在我的 woocommerce 网站中添加自定义订单状态。每当状态更改为该自定义状态时,我想发送邮件。
我试过 Send an email notification when custom order status changes in WooCommerce
以及 https://github.com/sarun007/custom-email-plugin/tree/master
但是没用
我用的是woocommerce 3.2.6版本

最佳答案

新版代码:Add a new order status that Send an email notification in WooCommerce 4+


要使其适用于新的自定义订单状态(此处为“等待交货”),您需要:

  • 首先注册自定义的新订单状态,
  • 在管理员订单列表的批量编辑下拉列表中显示它(可选)
  • 按顺序显示编辑页面状态下拉列表
  • 在订单获得此自定义状态时发送自定义电子邮件通知。

代码:

// register a custom post status 'awaiting-delivery' for Orders
add_action( 'init', 'register_custom_post_status', 20 );
function register_custom_post_status() {
register_post_status( 'wc-awaiting-delivery', array(
'label' => _x( 'Awaiting delivery', 'Order status', 'woocommerce' ),
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'Awaiting delivery <span class="count">(%s)</span>', 'Awaiting delivery <span class="count">(%s)</span>', 'woocommerce' )
) );
}

// Adding custom status 'awaiting-delivery' to order edit pages dropdown
add_filter( 'wc_order_statuses', 'custom_wc_order_statuses', 20, 1 );
function custom_wc_order_statuses( $order_statuses ) {
$order_statuses['wc-awaiting-delivery'] = _x( 'Awaiting delivery', 'Order status', 'woocommerce' );
return $order_statuses;
}

// Adding custom status 'awaiting-delivery' to admin order list bulk dropdown
add_filter( 'bulk_actions-edit-shop_order', 'custom_dropdown_bulk_actions_shop_order', 20, 1 );
function custom_dropdown_bulk_actions_shop_order( $actions ) {
$actions['mark_awaiting-delivery'] = __( 'Mark Awaiting delivery', 'woocommerce' );
return $actions;
}

// Adding action for 'awaiting-delivery'
add_filter( 'woocommerce_email_actions', 'custom_email_actions', 20, 1 );
function custom_email_actions( $action ) {
$actions[] = 'woocommerce_order_status_wc-awaiting-delivery';
return $actions;
}

add_action( 'woocommerce_order_status_wc-awaiting-delivery', array( WC(), 'send_transactional_email' ), 10, 1 );

// Sending an email notification when order get 'awaiting-delivery' status
add_action('woocommerce_order_status_awaiting-delivery', 'backorder_status_custom_notification', 20, 2);
function backorder_status_custom_notification( $order_id, $order ) {
// HERE below your settings
$heading = __('Your Awaiting delivery order','woocommerce');
$subject = '[{site_title}] Awaiting delivery order ({order_number}) - {order_date}';

// Getting all WC_emails objects
$mailer = WC()->mailer()->get_emails();

// Customizing Heading and subject In the WC_email processing Order object
$mailer['WC_Email_Customer_Processing_Order']->heading = $heading;
$mailer['WC_Email_Customer_Processing_Order']->subject = $subject;

// Sending the customized email
$mailer['WC_Email_Customer_Processing_Order']->trigger( $order_id );
}

代码进入您的事件子主题(或事件主题)的 function.php 文件。

经过测试并有效(应该适用于 2.5 以上的任何 Woocommerce 版本)

In others function if you use the WC_Order method update_status() to change an order to 'awaiting-delivery' status like:

$order->update_status();

the related email notification will be sent too.

关于php - 在 Woocommerce 中添加自定义订单状态并发送有关状态更改的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48616541/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com