首页 文章

在WooCommerce中自定义订单状态更改时发送电子邮件通知

提问于
浏览 822
0

我在我的WooCommerce中创建了一个名为Back Order( wc-backorder )的自定义订单状态:

/**
     * Add custom status to order list
*/
    add_action( 'init', 'register_custom_post_status', 10 );
    function register_custom_post_status() {
        register_post_status( 'wc-backorder', array(
            'label'                     => _x( 'Back Order', '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( 'Back Order <span class="count">(%s)</span>', 'Back Order <span class="count">(%s)</span>', 'woocommerce' )
        ) );

}

/**
 * Add custom status to order page drop down
 */
add_filter( 'wc_order_statuses', 'custom_wc_order_statuses' );
function custom_wc_order_statuses( $order_statuses ) {
    $order_statuses['wc-backorder'] = _x( 'Back Order', 'Order status', 'woocommerce' );
    return $order_statuses;
}

现在,我希望在收到已获得状态报价的订单时收到电子邮件 . 我根据这篇有用的文章创建了一个插件:How to Add a Custom WooCommerce Email

This link包含我的插件源代码和functions.php代码 .

我在 function.php 中添加了钩子:

add_action( 'woocommerce_order_status_wc-order-confirmed', array( WC(), 'send_transactional_email' ), 10, 10 );

function so_27112461_woocommerce_email_actions( $actions ){
    $actions[] = 'woocommerce_order_status_wc-order-confirmed';
    return $actions;
}
add_filter( 'woocommerce_email_actions', 'so_27112461_woocommerce_email_actions' );

订单变为“延期交货”状态时,没有任何反应 .

有任何想法吗?

我已经尝试了很多不同的钩子,但我似乎无法运行触发器功能 .

我使用的是最新版本的WordPress和WooCommerce(3.0)

谢谢

2 回答

  • 1
    add_action("woocommerce_order_status_changed", "my_custom_notification");
    
    function my_custom_notification($order_id, $checkout=null) {
       global $woocommerce;
       $order = new WC_Order( $order_id );
       if($order->status === 'backorder' ) {
          // Create a mailer
          $mailer = $woocommerce->mailer();
    
          $message_body = __( 'Hello world!!!' );
    
          $message = $mailer->wrap_message(
            // Message head and message body.
            sprintf( __( 'Order %s received' ), $order->get_order_number() ), $message_body );
    
          // Cliente email, email subject and message.
         $mailer->send( $order->billing_email, sprintf( __( 'Order %s received' ), $order->get_order_number() ), $message );
         }
    
       }
    

    试试这个

  • 0

    - EDIT / UPDATE -

    由于您使用的代码教程对于这个新的超级主要版本3.0来说真的已经过时(2013),这个挂在 woocommerce_order_status_changed 动作钩子中的自定义函数将完成这项工作 . 因此,当订单状态更改为您的自定义状态时,您将能够发送自定义的处理电子邮件通知 .

    这是WC 3.0的工作和测试代码:

    add_action('woocommerce_order_status_changed', 'backorder_status_custom_notification', 10, 4);
    function backorder_status_custom_notification( $order_id, $from_status, $to_status, $order ) {
    
       if( $order->has_status( 'backorder' )) {
    
            // Getting all WC_emails objects
            $email_notifications = WC()->mailer()->get_emails();
    
            // Customizing Heading and subject In the WC_email processing Order object
            $email_notifications['WC_Email_Customer_Processing_Order']->heading = __('Your processing Back order','woocommerce');
            $email_notifications['WC_Email_Customer_Processing_Order']->subject = 'Your {site_title} processing Back order receipt from {order_date}';
    
            // Sending the customized email
            $email_notifications['WC_Email_Customer_Processing_Order']->trigger( $order_id );
        }
    
    }
    

    此代码位于活动子主题(或主题)的function.php文件中,或者也可以放在任何插件文件中 .


    如果您的自定义状态为 wc-backorder, but not wc-order-confirmed ,则只需将 wc-order-confirmed 替换为 wc-backorder .

    要使其工作,您必须以这种方式更改最后2个挂钩函数:

    add_action( 'woocommerce_order_status_wc-backorder', array( WC(), 'send_transactional_email' ), 10, 1 );
    
    
    add_filter( 'woocommerce_email_actions', 'filter_woocommerce_email_actions' );
    function filter_woocommerce_email_actions( $actions ){
        $actions[] = 'woocommerce_order_status_wc-backorder';
        return $actions;
    }
    

    代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中 .

    这应该工作 (I can't test it as there is no the code of your custom plugin) .


    参考源代码:woocommerce_order_status_{$this->status_transition[to]} action hook

相关问题