需要对订单进行批量状态更新,以便从暂挂更改为已完成,但不发送确认电子邮件。但是,仍然需要保留电子邮件功能。这将是一个新的自定义批量操作,除了要更新为已完成的标准WooCommerce批量操作(仍将发送确认电子邮件)。我添加了额外的选项,没有问题,但找不到一种方法来排除电子邮件通知或临时禁用电子邮件通知的方法(这听起来不是一个好方法)。
到目前为止,代码如下。一切正常,除了$order->update_status('completed')触发确认电子邮件。
我尝试过使用set_status(),但它产生了相同的结果(update_status调用set_status)。
/*
* Custom bulk action in dropdown - Change status to completed without sending Confirmation Email
*/
add_filter( 'bulk_actions-edit-shop_order', 'register_bulk_action' ); // edit-shop_order is the screen ID of the orders page
function register_bulk_action( $bulk_actions ) {
$bulk_actions['complete_with_no_email'] = 'Change status to completed (no confirmation emails)';
return $bulk_actions;
}
/*
* Bulk action handler
*/
add_action( 'admin_action_complete_with_no_email', 'bulk_process_custom_status' ); // admin_action_{action name}
function bulk_process_custom_status() {
// if an array with order IDs is not presented, exit the function
if( !isset( $_REQUEST['post'] ) && !is_array( $_REQUEST['post'] ) )
return;
// New order emails
foreach( $_REQUEST['post'] as $order_id ) {
$order = new WC_Order( $order_id );
$order_note = 'Changed Status to Completed via bulk edit (no confirmation email)';
$order->update_status('completed', $order_note); //STILL SENDS EMAIL
}
// of course using add_query_arg() is not required, you can build your URL inline
$location = add_query_arg( array(
'post_type' => 'shop_order',
'changed' => count( $_REQUEST['post'] ), // number of changed orders
'ids' => join( $_REQUEST['post'], ',' ),
'marked_fulfilled_no_emails' => 1,
'post_status' => 'all'
), 'edit.php' );
wp_redirect( admin_url( $location ) );
exit;
}
/*
* Notices for Bulk Action
*/
add_action('admin_notices', 'custom_order_status_notices');
function custom_order_status_notices() {
global $pagenow, $typenow;
if( $typenow == 'shop_order'
&& $pagenow == 'edit.php'
&& isset( $_REQUEST['marked_fulfilled_no_emails'] )
&& $_REQUEST['marked_fulfilled_no_emails'] == 1
&& isset( $_REQUEST['changed'] ) ) {
$message = sprintf( _n( 'Order status changed.', '%s order statuses changed.', $_REQUEST['changed'] ), number_format_i18n( $_REQUEST['changed'] ) );
echo "<div class=\"updated\"><p>{$message}</p></div>";
}
}
希望避免触发确认电子邮件时,使用自定义批量编辑选项从订单。
发布于 2019-06-06 07:50:57
我找到的解决这个问题的最好方法是在循环通过选定的订单时使用update_post_meta添加一个标志,标志着它们绕过电子邮件confirmation.This,以及一个挂钩到woocommerce_email_recipient_customer_completed_order的函数,对于那些已经被标志的订单不返回任何内容,同时删除标志,以便所有其他功能仍然像往常一样触发电子邮件确认。相关函数如下:
添加挂钩以添加用于批量编辑的新选项:
/*
* Custom bulk action in dropdown - Change status to completed without sending Confirmation Email
*/
add_filter( 'bulk_actions-edit-shop_order', 'register_bulk_action' ); // edit-shop_order is the screen ID of the orders page
function register_bulk_action( $bulk_actions ) {
$bulk_actions['complete_with_no_email'] = 'Change status to completed (no confirmation emails)';
return $bulk_actions;
}
在这里处理这个新选项,确保用update_user_meta标记每个选定的帖子,这样我们就可以避免给它们发电子邮件了
/*
* Bulk action handler
* admin_action_{action name}
*/
add_action( 'admin_action_complete_with_no_email', 'bulk_process_complete_no_email' );
function bulk_process_complete_no_email() {
// if an array with order IDs is not presented, exit the function
if( !isset( $_REQUEST['post'] ) && !is_array( $_REQUEST['post'] ) )
return;
// Loop through selected posts
foreach( $_REQUEST['post'] as $order_id ) {
// Use this flag later to avoid sending emails via hook woocommerce_email_recipient_customer_completed_order
update_post_meta($order_id, 'bypass_email_confirmation', true);
$order = new WC_Order( $order_id );
$order_note = 'Changed Status to Completed via bulk edit (no confirmation email)';
$order->update_status('completed', $order_note);
}
// of course using add_query_arg() is not required, you can build your URL inline
$location = add_query_arg( array(
'post_type' => 'shop_order',
'changed' => count( $_REQUEST['post'] ), // number of changed orders
'ids' => join( $_REQUEST['post'], ',' ),
'marked_fulfilled_no_emails' => 1,
'post_status' => 'all'
), 'edit.php' );
wp_redirect( admin_url( $location ) );
exit;
}
这只是在批量操作完成后添加一个通知
/*
* Notices for the Bulk Action
* This just adds the notice after action has completed
*/
add_action('admin_notices', 'custom_order_status_notices');
function custom_order_status_notices() {
global $pagenow, $typenow;
if( $typenow == 'shop_order'
&& $pagenow == 'edit.php'
&& isset( $_REQUEST['marked_fulfilled_no_emails'] )
&& $_REQUEST['marked_fulfilled_no_emails'] == 1
&& isset( $_REQUEST['changed'] ) ) {
$message = sprintf( _n( 'Order status changed.', '%s order statuses changed.', $_REQUEST['changed'] ), number_format_i18n( $_REQUEST['changed'] ) );
echo "<div class=\"updated\"><p>{$message}</p></div>";
}
}
最后,每次站点要发送确认电子邮件时,都要钩住,并防止它在标志出现时这样做。重要的是,删除这里的标志,以便只有我们上面的批量操作将阻止确认电子邮件。
/*
* Hook into every time the site is going to email customer and stop it if the flag is present
*/
add_filter('woocommerce_email_recipient_customer_completed_order','handle_email_recipient_completed_order', 10, 2);
function handle_email_recipient_completed_order($recipient, $order) {
//if notifications are disabled (e.g. by bulk_process_complete_no_email)
$notifications_disabled = get_post_meta($order->get_id(), 'bypass_email_confirmation', true);
if ($notifications_disabled) {
update_post_meta($order->get_id(), 'bypass_email_confirmation', false);
return '';
} else {
return $recipient;
}
}
希望这对某些人有帮助,我花了很长时间试图找到这个特定问题的答案,但在其他地方找不到。
https://stackoverflow.com/questions/56455699
复制相似问题