仅针对挂单状态和特定付款方式发送定制的新订单通知
Send customized new order notification only for pending order status and specific payment methods
我正在尝试仅针对“待定”订单状态而非“货到付款”付款方式发送有关新订单的通知。但是当客户选择货到付款付款时,管理员会收到重复的邮件,因为 Woocommerce 将此订单状态从“待定”更新为“已处理”。
// New order notification only for "pending" order status and not "cash on delivery" payment method
add_action( 'woocommerce_checkout_order_processed', 'pending_new_order_notification', 10, 1 );
function pending_new_order_notification( $order_id ) {
// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );
$payment_title = $order->get_payment_method_title();
// Only for "pending" order status and not Cash on delivery payment method
if( ! $order->has_status( 'pending' ) && ( $payment_title != 'cod' ) ) return;
// Get an instance of the WC_Email_New_Order object
$wc_email = WC()->mailer()->get_emails()['WC_Email_New_Order'];
## -- Customizing Heading, subject (and optionally add recipients) -- ##
// Change Subject
$wc_email->settings['subject'] = __('{site_title} - Новый заказ ({order_number}) - {order_date} ожидает оплату');
// Change Heading
$wc_email->settings['heading'] = __('Новый заказ');
$wc_email->settings['recipient'] .= ',name@email.com'; // Add email recipients (coma separated)
// Send "New Email" notification (to admin)
$wc_email->trigger( $order_id );
}
您的代码中存在一些错误和遗漏的内容,因此请尝试以下操作:
// Send email
add_action( 'woocommerce_checkout_order_processed', 'pending_custom_new_order_notification', 20, 3 );
function pending_custom_new_order_notification( $order_id, $posted_data, $order ) {
// Only for "pending" order status and not Cash on delivery payment method
if( $order->has_status( 'pending' ) && 'cod' !== $order->get_payment_method() ) {
// Send "New Order" email
$wc_email = WC()->mailer()->get_emails()['WC_Email_New_Order']->trigger( $order_id );
}
}
// Custom email subject
add_filter( 'woocommerce_email_subject_new_order', 'custom_new_order_email_subject', 20, 2 );
function custom_new_order_email_subject( $formated_subject, $order ){
// Only for "pending" order status and not Cash on delivery payment method
if( $order->has_status( 'pending' ) && 'cod' !== $order->get_payment_method() ) {
$formated_subject = sprintf( __('%s - Новый заказ (%s) - %s ожидает оплату', 'woocommerce'),
get_bloginfo( 'name' ),
$order->get_order_number(), // Order ID (or the order number)
$order->get_date_modified()->date_i18n('F j, Y') // Formatted date modified
);
}
return $formated_subject;
}
// Custom email heading
add_filter( 'woocommerce_email_heading_new_order', 'custom_new_order_email_heading', 20, 2 );
function custom_new_order_email_heading( $heading_txt, $order ){
// Only for "pending" order status and not Cash on delivery payment method
if( $order->has_status( 'pending' ) && 'cod' !== $order->get_payment_method() ) {
$heading_txt = __('Новый заказ', 'woocommerce');
}
return $heading_txt;
}
// Custom email recipient
add_filter( 'woocommerce_email_recipient_new_order', 'custom_new_order_email_recipient', 20, 2 );
function custom_new_order_email_recipient( $recipient, $order ){
// Only for "pending" order status and not Cash on delivery payment method
if( $order->has_status( 'pending' ) && 'cod' !== $order->get_payment_method() ) {
$recipient .= ',name@email.com';
}
return $recipient;
}
代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
Since WooCommerce 5+:
我正在尝试仅针对“待定”订单状态而非“货到付款”付款方式发送有关新订单的通知。但是当客户选择货到付款付款时,管理员会收到重复的邮件,因为 Woocommerce 将此订单状态从“待定”更新为“已处理”。
// New order notification only for "pending" order status and not "cash on delivery" payment method
add_action( 'woocommerce_checkout_order_processed', 'pending_new_order_notification', 10, 1 );
function pending_new_order_notification( $order_id ) {
// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );
$payment_title = $order->get_payment_method_title();
// Only for "pending" order status and not Cash on delivery payment method
if( ! $order->has_status( 'pending' ) && ( $payment_title != 'cod' ) ) return;
// Get an instance of the WC_Email_New_Order object
$wc_email = WC()->mailer()->get_emails()['WC_Email_New_Order'];
## -- Customizing Heading, subject (and optionally add recipients) -- ##
// Change Subject
$wc_email->settings['subject'] = __('{site_title} - Новый заказ ({order_number}) - {order_date} ожидает оплату');
// Change Heading
$wc_email->settings['heading'] = __('Новый заказ');
$wc_email->settings['recipient'] .= ',name@email.com'; // Add email recipients (coma separated)
// Send "New Email" notification (to admin)
$wc_email->trigger( $order_id );
}
您的代码中存在一些错误和遗漏的内容,因此请尝试以下操作:
// Send email
add_action( 'woocommerce_checkout_order_processed', 'pending_custom_new_order_notification', 20, 3 );
function pending_custom_new_order_notification( $order_id, $posted_data, $order ) {
// Only for "pending" order status and not Cash on delivery payment method
if( $order->has_status( 'pending' ) && 'cod' !== $order->get_payment_method() ) {
// Send "New Order" email
$wc_email = WC()->mailer()->get_emails()['WC_Email_New_Order']->trigger( $order_id );
}
}
// Custom email subject
add_filter( 'woocommerce_email_subject_new_order', 'custom_new_order_email_subject', 20, 2 );
function custom_new_order_email_subject( $formated_subject, $order ){
// Only for "pending" order status and not Cash on delivery payment method
if( $order->has_status( 'pending' ) && 'cod' !== $order->get_payment_method() ) {
$formated_subject = sprintf( __('%s - Новый заказ (%s) - %s ожидает оплату', 'woocommerce'),
get_bloginfo( 'name' ),
$order->get_order_number(), // Order ID (or the order number)
$order->get_date_modified()->date_i18n('F j, Y') // Formatted date modified
);
}
return $formated_subject;
}
// Custom email heading
add_filter( 'woocommerce_email_heading_new_order', 'custom_new_order_email_heading', 20, 2 );
function custom_new_order_email_heading( $heading_txt, $order ){
// Only for "pending" order status and not Cash on delivery payment method
if( $order->has_status( 'pending' ) && 'cod' !== $order->get_payment_method() ) {
$heading_txt = __('Новый заказ', 'woocommerce');
}
return $heading_txt;
}
// Custom email recipient
add_filter( 'woocommerce_email_recipient_new_order', 'custom_new_order_email_recipient', 20, 2 );
function custom_new_order_email_recipient( $recipient, $order ){
// Only for "pending" order status and not Cash on delivery payment method
if( $order->has_status( 'pending' ) && 'cod' !== $order->get_payment_method() ) {
$recipient .= ',name@email.com';
}
return $recipient;
}
代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
Since WooCommerce 5+: