wordpress 自定义电子邮件通知可防止Woocommerce自动发送电子邮件

lb3vh1jj  于 2023-04-29  发布在  WordPress
关注(0)|答案(1)|浏览(129)

我最近在Woocommerce中为“发货和发票”创建了一个插件,其中包括一个新的订单条件,用于“处理发货”操作。该插件有一个下拉菜单,一些动作和其他功能。我还添加了一个新的电子邮件通知功能,当订单状态更改为“已发货”时,它会向客户发送一封电子邮件,并附带发货通知。
然而,我遇到了一个问题与插件。我为电子邮件通知功能添加的代码似乎阻止了Woocommerce自动发送默认的电子邮件通知,例如“新订单(管理员)”和“新订单(客户)”等。我不知道哪里出了问题,因为我对编码和解决这些问题还比较陌生。
顺便说一句,当我尝试手动发送电子邮件通知时,电子邮件发送的方式应该是。但当新订单下、完成或任何其他状态发生变化时,什么都不会发生。
这里是我的代码片段;
首先,我注册了 * 发货 * 状态的自定义状态:

add_action( 'init', 'register_shipped_status', 20 );
function register_shipped_status() {
    register_post_status( 'wc-shipped', array(
        'label'                     => 'Shipped',
        'public'                    => true,
        'exclude_from_search'       => false,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'label_count'               => 'Shipped <span class="count">(%s)</span>',
    ) );
}

然后我把这个状态添加到相关的地方(即。dropdown)

add_filter( 'wc_order_statuses', 'shipped_wc_order_statuses', 20, 1 );
function shipped_wc_order_statuses( $order_statuses ) {
    $order_statuses[ 'wc-shipped' ] = 'Shipped';
    return $order_statuses;
}

add_filter( 'bulk_actions-edit-shop_order', 'shipped_dropdown_bulk_actions_shop_order');
function shipped_dropdown_bulk_actions_shop_order( $actions ) {
    $actions['mark_shipped'] = 'Change status to shipped';
    return $actions;
}

然后注册电子邮件类:

add_filter( 'woocommerce_email_classes', 'add_shipped_order_woocommerce_email' );
function add_shipped_order_woocommerce_email( $email_classes ) {

    require( SHIPMENT_INVOICE_PLUGIN_DIR . 'includes/classes/ShippedOrderEmail.php' );

    $email_classes['WC_Shipped_Order_Email'] = new WC_Shipped_Order_Email();

    return $email_classes;

}

并为电子邮件通知操作和触发器添加:

add_filter( 'woocommerce_email_actions', 'shipped_email_actions', 20, 1 );
function shipped_email_actions( $actions ) {
    $actions[] = 'woocommerce_order_status_wc-shipped';
    return $actions;
}

add_action( 'woocommerce_order_status_wc-shipped', array( 'WC_Emails', 'send_transactional_email' ), 10, 1 );

add_action('woocommerce_order_status_shipped', 'shipped_status_trigger_email_notification', 10, 2 );
function shipped_status_trigger_email_notification( $order_id, $order ) {
    WC()->mailer()->get_emails()['WC_Shipped_Order_Email']->trigger( $order_id );
}

这里是我的扩展类 WC_Shipped_Order_Email

  • (PS:这篇文章在ShippedOrderEmail中。php我之前在注册电子邮件类时提到过)*
class WC_Shipped_Order_Email extends WC_Email {

    public function __construct() {
        $this->id             = 'customer-shipped-order';
        $this->customer_email = true;
        $this->title          = 'Shipped order';
        $this->description    = 'This notification is an order notification sent to customers after shipping that includes product details and a shipping tracking number.';
        $this->template_html  = 'emails/customer-shipped-order.php';
        $this->placeholders   = array(
            '{order_date}'   => '',
            '{order_number}' => '',
        );

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

        parent::__construct();
    }

    public function trigger( $order_id ) {

        if ( ! $order_id )
            return;

        if ( $order_id && ! is_a( $order, 'WC_Order' ) ) {
            $order = wc_get_order( $order_id );
        }

        if ( is_a( $order, 'WC_Order' ) ) {
            $this->object                         = $order;
            $this->recipient                      = $this->object->get_billing_email();
            $this->placeholders['{order_date}']   = wc_format_datetime( $this->object->get_date_created() );
            $this->placeholders['{order_number}'] = $this->object->get_order_number();
        }

        if ( ! $this->is_enabled() || ! $this->get_recipient() )
            return;

        
        $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
    }

    public function get_content_html() {
        return wc_get_template_html(
            $this->template_html,
            array(
                'order'              => $this->object,
                'email_heading'      => $this->get_heading(),
                'additional_content' => $this->get_additional_content(),
                'sent_to_admin'      => false,
                'plain_text'         => false,
                'email'              => $this,
            )
        );
    }

    public function init_form_fields() {
        $placeholder_text  = sprintf( __( 'Available placeholders: %s', 'woocommerce' ), '<code>' . esc_html( implode( '</code>, <code>', array_keys( $this->placeholders ) ) ) . '</code>' );

        $this->form_fields = array(
            'enabled'   => array(
                'title'     => __( 'Enable/Disable', 'woocommerce' ),
                'type'      => 'checkbox',
                'label'     => __( 'Enable this email notification', 'woocommerce' ),
                'default'   => 'yes',
            ),
            'subject'   => array(
                'title'       => __( 'Subject', 'woocommerce' ),
                'type'        => 'text',
                'desc_tip'    => true,
                'description' => $placeholder_text,
                'placeholder' => $this->get_default_subject(),
                'default'     => '',
            ),
            'heading'            => array(
                'title'       => __( 'Email heading', 'woocommerce' ),
                'type'        => 'text',
                'desc_tip'    => true,
                'description' => $placeholder_text,
                'placeholder' => $this->get_default_heading(),
                'default'     => '',
            ),
            'additional_content' => array(
                'title'       => __( 'Additional content', 'woocommerce' ),
                'description' => __( 'Text to appear below the main email content.', 'woocommerce' ) . ' ' . $placeholder_text,
                'css'         => 'width:400px; height: 75px;',
                'placeholder' => __( 'N/A', 'woocommerce' ),
                'type'        => 'textarea',
                'default'     => $this->get_default_additional_content(),
                'desc_tip'    => true,
            ),
        );
    }
}
x8diyxa7

x8diyxa71#

1-2天后,重新检查我的代码,我发现问题。
我把这部分写错了:
错误:
函数shipped_email_actions($action)
更正:
函数shipped_email_actions($actions)
我编辑了我的代码(以及消息)。现在它工作得很完美。

相关问题