You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WooCommerce 9.1.2自定义订单状态邮件无法触发问题

问题:WooCommerce自定义订单状态邮件失效
  • 自定义订单状态「已收到您的退货(Uw retour ontvangen)」可正常设置,但切换至该状态时系统不会触发邮件发送,默认订单状态的邮件功能正常。
  • 调试确认网站能识别订单状态变更,但未执行邮件发送逻辑,现已移除调试语句。
  • 当前环境:Hello Elementor主题、PHP 8.3、WooCommerce 9.1.2。

调试日志片段

[23-Jul-2024 06:54:18 UTC] Order status changed: Order ID 6568 from checkout-draft to uwretourontvangen
[23-Jul-2024 06:54:18 UTC] WC_uwretourontvangen_Status_Email class constructed
[23-Jul-2024 06:54:18 UTC] WC_uwretourontvangen_Status_Email class registered

尝试的第一段代码

function register_uwretourontvangen_order_status() {
    register_post_status( 'wc-uwretourontvangen', array(
        'label'                     => 'Uw retour ontvangen',
        'public'                    => true,
        'exclude_from_search'       => false,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'label_count'               => _n_noop( 'Uw retour ontvangen (%s)', 'Uw retour ontvangen (%s)' )
    ) );
}
add_action( 'init', 'register_uwretourontvangen_order_status' );

add_filter( 'wc_order_statuses', function( $order_statuses ) {
    $order_statuses['wc-uwretourontvangen'] = _x( 'Uw retour ontvangen', 'Order status', 'woocommerce' );
    return $order_statuses;
});

add_action( 'woocommerce_email_classes', function ( $email_classes ) {
    class WC_uwretourontvangen_Status_Email extends WC_Email {

        public function __construct() {
            $this->id               = 'wc-uwretourontvangen';
            $this->title            = 'Uw Retour ontvangen';
            $this->description      = 'An email sent when order status changes to retour.';
            $this->heading          = 'Wij hebben uw retour ontvangen';
            $this->subject          = 'Uw retour ontvangen';
            $this->template_html    = 'emails/admin-retourontvangen-status.php';
            $this->template_plain   = 'emails/plain/admin-retourontvangen-statusplain.php';

            parent::__construct();

            add_action( 'woocommerce_order_status_uwretourontvangen', array( $this, 'trigger' ), 10, 2 );
        }

        public function trigger( $order_id, $order = false ) {
            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();
        
                if ( $this->is_enabled() && $this->get_recipient() ) {
                    $this->send($this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments());
                }
            }
        }
        
        
        public function get_content_html() {
            ob_start();
            wc_get_template( $this->template_html, array(
                'order'         => $this->object,
                'email_heading' => $this->get_heading(),
                'sent_to_admin' => false,
                'plain_text'    => false,
                'email'         => $this
            ) );
            return ob_get_clean();
        }
        
        public function get_content_plain() {
            ob_start();
            wc_get_template( $this->template_plain, array(
                'order'         => $this->object,
                'email_heading' => $this->get_heading(),
                'sent_to_admin' => false,
                'plain_text'    => true,
                'email'         => $this
            ) );
            return ob_get_clean();
        }
    }
        
    $email_classes['WC_uwretourontvangen_Status_Email'] = new WC_uwretourontvangen_Status_Email();
    return $email_classes;
});

尝试的第二段参考Stack Overflow修改后的代码

add_action( 'init', 'register_uwretourontvangen_order_status', 20 );
function register_uwretourontvangen_order_status() {
    register_post_status( 'wc-uwretourontvangen', array(
        'label'                     => _x( 'Uw retour ontvangen', '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( 'Uw retour ontvangen <span class="count">(%s)</span>', 'Uw retour ontvangen <span class="count">(%s)</span>', 'woocommerce' )
    ) );
}

add_filter( 'wc_order_statuses', 'custom_wc_order_statuses' );
function custom_wc_order_statuses( $order_statuses ) {
    $order_statuses['wc-uwretourontvangen'] = _x( 'Uw retour ontvangen', 'Order status', 'woocommerce' );
    return $order_statuses;
}

add_filter( 'bulk_actions-edit-shop_order', 'custom_dropdown_bulk_actions_shop_order', 20, 1 );
function custom_dropdown_bulk_actions_shop_order( $actions ) {
    $actions['mark_uwretourontvangen'] = __( 'Uw retour ontvangen', 'woocommerce' );
    return $actions;
}

add_filter( 'woocommerce_email_actions', 'custom_email_actions', 20, 1 );
function custom_email_actions( $actions ) {
    $actions[] = 'woocommerce_order_status_uwretourontvangen';
    return $actions;
}

add_action( 'plugins_loaded', 'initialize_custom_email_class' );
function initialize_custom_email_class() {

    if ( ! class_exists( 'WC_Email_Uwretourontvangen' ) ) {
        class WC_Email_Uwretourontvangen extends WC_Email {

            public function __construct() {
                $this->id               = 'uwretourontvangen';
                $this->title            = 'Uw Retour ontvangen';
                $this->description      = 'This is an order notification sent to the customer when their order is marked as Uw Retour ontvangen.';
                $this->heading          = 'Wij hebben uw retour ontvangen';
                $this->subject          = 'Uw retour ontvangen';
                $this->template_html    = 'emails/admin-retourontvangen-status.php';
                $this->template_plain   = 'emails/plain/admin-retourontvangen-statusplain.php';
                $this->customer_email   = true;

                add_action( 'woocommerce_order_status_uwretourontvangen_notification', array( $this, 'trigger' ), 10, 2 );

                parent::__construct();

                $this->template_base = plugin_dir_path( __FILE__ ) . 'templates/';
            }

            public function trigger( $order_id, $order = false ) {
                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();

                    if ( $this->is_enabled() && $this->get_recipient() ) {
                        $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
                    }
                }
            }

            public function get_content_html() {
                ob_start();
                wc_get_template( $this->template_html, array(
                    'order'         => $this->object,
                    'email_heading' => $this->get_heading(),
                    'sent_to_admin' => false,
                    'plain_text'    => false,
                    'email'         => $this
                ), '', $this->template_base );
                return ob_get_clean();
            }

            public function get_content_plain() {
                ob_start();
                wc_get_template( $this->template_plain, array(
                    'order'         => $this->object,
                    'email_heading' => $this->get_heading(),
                    'sent_to_admin' => false,
                    'plain_text'    => true,
                    'email'         => $this
                ), '', $this->template_base );
                return ob_get_clean();
            }

            public function get_default_subject() {
                return __( 'Uw retour ontvangen', 'woocommerce' );
            }

            public function get_default_heading() {
                return __( 'Wij hebben uw retour ontvangen', 'woocommerce' );
            }
        }
    }

    add_filter( 'woocommerce_email_classes', 'add_custom_email_class' );
    function add_custom_email_class( $email_classes ) {
        $email_classes['WC_Email_Uwretourontvangen'] = new WC_Email_Uwretourontvangen();
        return $email_classes;
    }

    add_action( 'woocommerce_order_status_uwretourontvangen', 'trigger_custom_email_notification', 20, 2 );
    function trigger_custom_email_notification( $order_id, $order ) {
        $mailer = WC()->mailer();
        $mails = $mailer->get_emails();
        if ( ! empty( $mails ) && isset( $mails['WC_Email_Uwretourontvangen'] ) ) {
            $mails['WC_Email_Uwretourontvangen']->trigger( $order_id );
        }
    }
}

add_filter( 'woocommerce_email_settings', 'add_custom_email_setting' );
function add_custom_email_setting( $settings ) {
    $settings[] = array(
        'title'       => __( 'Uw Retour ontvangen', 'woocommerce' ),
        'id'          => 'uwretourontvangen',
        'type'        => 'checkbox',
        'description' => __( 'Enable this email notification for Uw Retour ontvangen status.', 'woocommerce' ),
        'default'     => 'yes',
    );

    return $settings;
}

内容的提问来源于stack exchange,提问作者aym an

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 17:39:54