【问题标题】:Add custom emails Woocommerce添加自定义电子邮件 Woocommerce
【发布时间】:2016-08-30 13:10:27
【问题描述】:

我有一个用于管理我的货件的插件,它有两种自定义状态:等待发货和已发货。

我尝试添加在订单传递到发货时发送的电子邮件。

我在 Stack Overflow 上找到了这个:Woocommerce Refund Email 但我可以弄清楚它是如何工作的

这是我的插件文件代码:
我根据 helgatheviking 和 Adrien Leber 的建议更新了我的代码

function shippement_tracking_filter_actions( $actions ){
    $actions[] = "woocommerce_order_status_shipped";
    return $actions;
}
add_filter( 'woocommerce_email_actions', 'shippement_tracking_filter_actions' );

function add_expedited_order_woocommerce_email( $email_classes ) {
    require( 'includes/class-wc-expedited-order-email.php' );
    $email_classes['WC_Expedited_Order_Email'] = new WC_Expedited_Order_Email();
    return $email_classes;
}
add_filter( 'woocommerce_email_classes', 'add_expedited_order_woocommerce_email' );`

还有我的班级:

class WC_Expedited_Order_Email extends WC_Email {
    public function __construct() {

        $this->id               = 'expedited_order_tracking';
        $this->customer_email   = true;
        $this->title            = __( 'Shippement Traking', 'customEmail' );
        $this->description      = __( 'Sent tracking email to customer', 'customEmail' );
        $this->heading          = __( 'Your {site_title} order is shipped', 'customEmail' );
        $this->subject          = __( 'Your {site_title} order from {order_date} is shipped', 'customEmail' );

        $this->template_html    = 'emails/customer-order_tracking.php';
        $this->template_plain   = 'emails/plain/customer-order_tracking.php';

        add_action( 'woocommerce_order_status_shipped', array( $this, 'trigger' ) );

        parent::__construct();
    }

    public function trigger( $order_id )
    {
        var_dump($order_id);die();
    }

当我更改订单状态时,什么也没有发生!我的触发函数从不调用。

谁能帮帮我?

【问题讨论】:

  • 我可能会使用$email_actions[] = 'woocommerce_order_status_shipped; 而不是过渡。虽然我认为真正的交易杀手是你没有在这里传递类的新实例$email_classes['WC_Expedited_Order_Email'] 你正在传递包含的文件。在上面的行中包含该文件,然后实例化该类。见this tutorial
  • 感谢您的回答@helgatheviking。我试过了,但我认为我的触发函数仍然没有被调用
  • 也许 $order_id 没有通过?如果你在die() 之前echo "something" 怎么办?
  • 是的,我试过了,但我没有看到我的回声,我认为我的触发函数永远不会调用,但我不明白为什么
  • 经过一番研究!我觉得问题出在我在 add_action 中使用的状态:他是由外部模块添加的,而不是 WooCommerce 的核心!关于这个的想法?

标签: wordpress woocommerce


【解决方案1】:

我认为你误解了这部分:

function add_expedited_order_woocommerce_email( $email_classes ) {
   $email_classes['WC_Expedited_Order_Email'] = include( plugin_dir_path( __FILE__ ) . '/class-wc-expedited-order-email.php' );
   return $email_classes;
}

你必须先包含类文件,然后创建这个类的一个新实例:

function add_expedited_order_woocommerce_email( $email_classes ) {

    // include our custom email class
    require( 'includes/class-wc-expedited-order-email.php' );

    // add the email class to the list of email classes that WooCommerce loads
    $email_classes['WC_Expedited_Order_Email'] = new WC_Expedited_Order_Email();

    return $email_classes;

}

希望对你有帮助。

【讨论】:

  • 感谢您的回答!我更改了代码,并按照@ helgatheviking 的建议,将触发操作更改为woocommerce_order_status_shipped!但是什么也没发生,我的触发器函数永远不会被调用,我的 var_dump 也永远不会显示
  • 能否编辑您的答案,向我们展示您的新代码?
猜你喜欢
  • 2021-12-11
  • 2020-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-20
  • 2015-10-08
  • 2020-08-31
  • 2016-03-03
相关资源
最近更新 更多