当订单状态更改为自定义订单状态时,从 WooCommerce 发送电子邮件

问题描述:

我在 WooCommerce 安装中创建了一个自定义订单状态,称为 Quote.

I have created a custom order status in my WooCommerce installation, called Quote.

/* 
* Change order status on new orders depending on order contents:
*  If any product in the order is availble for quote, return 'quote' status. 
*  Otherwise the order status will be set to processing.
*/
add_filter ('woocommerce_payment_complete_order_status', 'change_status_to_quote_if_applicable', 10, 2);
function change_status_to_quote_if_applicable($order_status, $order_id) { 
    $order = new WC_Order($order_id);
    $items = $order->get_items();
    foreach ($items as $item) {
        $product = get_product($item['product_id']);
        if(product_available_for_quote($product)){
            return 'quote';
        }
    }
    return $order_status;
}

现在我想在收到已提供状态报价的订单时收到一封电子邮件.我根据这篇有用的文章创建了一个插件:http://www.skyverge.com/blog/how-to-add-a-custom-woocommerce-email/

Now I want to recieve an email whenever an order is recieved that has been given the status quote. I've created a plugin based on this helpful article: http://www.skyverge.com/blog/how-to-add-a-custom-woocommerce-email/

我的插件基本上是从文章中复制过来的,我只是更改了邮件的内容.我想改变的是触发电子邮件的内容.

My plugin is basically copied from the article, I've just changed the contents of the email. What I wanted to change is what triggers the email.

文章中的插件是这样的:

The plugin in the article has this:

// Trigger on new paid orders
add_action( 'woocommerce_order_status_pending_to_processing_notification', array( $this, 'trigger' ) );
add_action( 'woocommerce_order_status_failed_to_processing_notification',  array( $this, 'trigger' ) );

我希望在订单状态为报价"时触发电子邮件.我是这样做的:

And I want the email to be triggered when an order gets the status 'quote'. I did this:

// Trigger on new quote orders
add_action( 'woocommerce_order_status_pending_to_quote', array( $this, 'trigger' ) );

当订单获得报价"状态时不会发生任何事情.我已经检查了 class-wc-order.php,特别是函数 update_status,因为那是 woocommerce_order_status_.$this->status._to_.$new_status->slug代码> 被解雇.我做了一个 error_log 以查看 woocommerce_order_status_pending_to_quote 操作是否存在,并且确实存在.但是我插件中的触发器函数从来没有运行过.

Nothing happens when an order gets the 'quote' status. I've checked class-wc-order.php and specifically the function update_status, since that's where the woocommerce_order_status_.$this->status._to_.$new_status->slug is fired. I did an error_log to see that the woocommerce_order_status_pending_to_quote action exists, and it does. But my trigger function in my plugin is never run.

有什么想法吗?我尝试了大量不同的钩子,但似乎无法运行触发函数.

Any ideas? I've tried loads of different hooks but I can't seem to get the trigger function to run.

非常感谢!

我相信你可以使用你的自定义钩子,但首先你必须注册它.我有一个与 WooThemes 挂起的拉取请求,以允许过滤您在核心中发现的电子邮件操作.但是如果/直到它被接受,这就是它应该做的:

I believe you can use your custom hook, but first you have to register it. I have a pull request pending with WooThemes to allow the email actions you discovered in core to be filtered. But if/until it is accepted this is how it should be done:

/**
 * Register the "woocommerce_order_status_pending_to_quote" hook which is necessary to
 * allow automatic email notifications when the order is changed to refunded.
 * 
 * @modified from http://stackoverflow.com/a/26413223/2078474 to remove anonymous function
 */
add_action( 'woocommerce_init', 'so_25353766_register_email' );
function so_25353766_register_email(){
    add_action( 'woocommerce_order_status_pending_to_quote', array( WC(), 'send_transactional_email' ), 10, 10 );
}

WooCommerce 2.3.11+ 更新

有了这个commit,WooCommerce 确实允许您过滤电子邮件操作.因此,理论上,您现在应该能够使用以下内容将操作注册为电子邮件触发器:

Update for WooCommerce 2.3.11+

With this commit WooCommerce does allow you to filter the email actions. So, in theory, you should now be able to register an action as an email trigger with the following:

/**
 * Register "woocommerce_order_status_pending_to_quote" as an email trigger
 */
add_filter( 'woocommerce_email_actions', 'so_25353766_filter_actions' );
function so_25353766_filter_actions( $actions ){
    $actions[] = "woocommerce_order_status_pending_to_quote";
    return $actions;
}