php Woocommerce挂钩自定义状态更改不会触发时,在transsational电子邮件中使用

f4t66c6m  于 2022-12-21  发布在  PHP
关注(0)|答案(1)|浏览(78)

我无法获得状态更改的交易电子邮件。我有自定义的状态和自定义电子邮件设置在我编程的吴宇森商务插件我的需要。
该代码遵循与here所述的ootb事务性电子邮件相同的逻辑

<?php
/*
Plugin Name:  WooCommerce Custom Statuses

*/



if (!defined('ABSPATH')) {
    return;
}

/**
 * Class Custom_WC_Email
 */
class WC_LR_custom_order_statuses
{

    /**
     * Custom_WC_Email constructor.
     */
    public function __construct()
    {
        // Filtering the emails and adding our own email.
        add_filter('woocommerce_email_classes', array($this, 'lr_register_emails'), 10, 1);
        add_filter('woocommerce_register_shop_order_post_statuses', [$this, 'lr_register_statuses'], 10, 1);
        add_filter('wc_order_statuses', [$this, 'lr_custom_wc_order_statuses']);
        add_filter('bulk_actions-edit-shop_order', [$this, 'lr_custom_dropdown_bulk_actions_shop_order'], 20, 1);

        foreach (['post', 'shop_order'] as $hook)
            add_filter("views_edit-$hook", [$this, 'lr_shop_order_modified_views']);

        add_action('admin_head', [$this, 'lr_change_order_processing_row_color']);
        //add_action('init', [$this, 'register_statuses']);



        // add_action( 'add_meta_boxes', [$this,'littlerug_add_custom_box'] );
        // add_action( 'save_post', [$this,'littlerug_save_postdata'], 1, 2 );

        // Absolute path to the plugin folder.
        define('CUSTOM_WC_EMAIL_PATH', plugin_dir_path(__FILE__));
    }
    function lr_shop_order_modified_views($views)
    {
        // if( isset( $views['wc-completed'] ) )
        //     $views['wc-completed'] = str_replace( 'Completed', __( 'Order Received', 'woocommerce'), $views['wc-completed'] );

        if (isset($views['wc-processing']))
            $views['wc-processing'] = str_replace('Spracováva sa', 'Čakajúce na platbu', $views['wc-processing']);

        if (isset($views['wc-on-hold'])) {
            if (preg_match('/Čaká/', $views['wc-on-hold']))
                $views['wc-on-hold'] = str_replace('Čaká', 'Prijatá', $views['wc-on-hold']);
            else
                $views['wc-on-hold'] = str_replace('Čakajúce', 'Prijaté', $views['wc-on-hold']);
        }
        // if( isset( $views['wc-pending'] ) )
        //     $views['wc-pending'] = str_replace( 'Pending', __( 'Stucked', 'woocommerce'), $views['wc-pending'] );

        return $views;
    }
    function lr_custom_dropdown_bulk_actions_shop_order($actions)
    {
        unset($actions['mark_processing']);
        unset($actions['mark_pending']);
        unset($actions['mark_cancelled']);
        unset($actions['mark_completed']);

        $actions['mark_on-hold']    = 'Zmeniť stav na "Prijatá"';
        $actions['mark_order-confirmed']    = 'Zmeniť stav na "Potvrdená"';
        $actions['mark_payment-await'] = 'Zmeniť stav na "Čaká na platbu"';
        $actions['mark_payment-received']    = 'Zmeniť stav na "Platba prijatá"';
        $actions['mark_order-shipped']    = 'Zmeniť stav na "Odoslaná"';
        $actions['mark_completed']    = 'Zmeniť stav na "Vybavená"';
        $actions['mark_order-done']    = 'Zmeniť stav na "Hotová"';
        $actions['mark_cancelled'] = 'Zmeniť stav na "Zrušená"';

        return $actions;
    }
    function lr_change_order_processing_row_color()
    {

        echo sprintf(
            '
            <style type="text/css">
                .order-status.status-order-shipped {
                    background: #c8d7e1;
                    color: #2e4453;
                }
                .order-status.status-order-confirmed {
                    background: #edc77a;
                    color: #a36e03;
                }
                .order-status.status-payment-received {
                    background: #8cd98c;
                    color: #507418;
                }
                .order-status.status-order-done {
                    background: #fdfd96;
                    color: #8f8f12;
                }.order-status.status-payment-await {
                    background: #c6e1c6;
                    color: #5b841b;
                }
            </style>'
        );
    }
    function lr_custom_wc_order_statuses($order_statuses)
    {
        //change default status names add new
        unset($order_statuses['wc-processing']);
        unset($order_statuses['wc-pending']);
        unset($order_statuses['wc-completed']);
        unset($order_statuses['wc-cancelled']);
        unset($order_statuses['wc-refunded']);
        unset($order_statuses['wc-failed']);

        $order_statuses['wc-on-hold'] = _x('Prijatá', 'woocommerce');
        $order_statuses['wc-order-confirmed'] = _x('Potvrdená', 'Order status', 'woocommerce');
        $order_statuses['wc-payment-await'] = _x('Čaká na platbu', 'woocommerce');
        $order_statuses['wc-payment-received'] = _x('Platba prijatá', 'Order status', 'woocommerce');
        $order_statuses['wc-order-shipped'] = _x('Odoslaná', 'Order status', 'woocommerce');
        $order_statuses['wc-completed'] = _x('Vybavená', 'Order status', 'woocommerce');
        $order_statuses['wc-order-done'] = _x('Hotová', 'Order status', 'woocommerce');
        $order_statuses['wc-cancelled'] = _x('Zrušená', 'Order status', 'woocommerce');
        $order_statuses['wc-refunded'] = _x('Refundovaná', 'Order status', 'woocommerce');

        return $order_statuses;
    }
    /**
     * @param array $emails
     *
     * @return array
     */
    public function lr_register_emails($emails)

    {

        $emails['WC_Customer_Cancel_Order'] = include __DIR__ . '/emails/class-wc-customer-cancel-order.php';
        $emails['WC_Customer_Payment_Received'] =  include __DIR__ . '/emails/class-wc-customer-payment-received.php';
        $emails['WC_Customer_Order_Shipped'] =  include __DIR__ . '/emails/class-wc-customer-order-shipped.php';
        $emails['WC_Customer_Order_Confirmed'] =  include __DIR__ . '/emails/class-wc-customer-order-confirmed.php';
        $emails['WC_Customer_Payment_Await'] =  include __DIR__ . '/emails/class-wc-customer-payment-await.php';
        //customer_processing_order
        // $emails['WC_Customer_Order_Placed'] = new WC_Customer_Order_Placed();
        return $emails;
    }
    public function lr_register_statuses($order_statuses)
    {
        // Status must start with "wc-"
        $order_statuses['wc-payment-await'] = array(
            'label'                     => 'Čaká na platbu', //_x( 'Back Order', 'Order status', 'woocommerce' ),
            'public'                    => false,
            'exclude_from_search'       => false,
            'show_in_admin_all_list'    => true,
            'show_in_admin_status_list' => true,
            'label_count'               => _n_noop('Čaká na platbu <span class="count">(%s)</span>', 'Čaká na platbu <span class="count">(%s)</span>', 'woocommerce') //_n_noop( 'Back Order <span class="count">(%s)</span>', 'Back Order <span class="count">(%s)</span>', 'woocommerce' )
        );
        $order_statuses['wc-payment-received'] =  array(
            'label'                     => 'Platba prijatá', //_x( 'Back Order', 'Order status', 'woocommerce' ),
            'public'                    => false,
            'exclude_from_search'       => false,
            'show_in_admin_all_list'    => true,
            'show_in_admin_status_list' => true,
            'label_count'               => _n_noop('Platba prijatá <span class="count">(%s)</span>', 'Platieb prijatých <span class="count">(%s)</span>', 'woocommerce') //_n_noop( 'Back Order <span class="count">(%s)</span>', 'Back Order <span class="count">(%s)</span>', 'woocommerce' )
        );
        $order_statuses['wc-order-shipped'] =  array(
            'label'                     => 'Odoslaná', //_x( 'Back Order', 'Order status', 'woocommerce' ),
            'public'                    => false,
            'exclude_from_search'       => false,
            'show_in_admin_all_list'    => true,
            'show_in_admin_status_list' => true,
            'label_count'               => _n_noop('Odoslaná <span class="count">(%s)</span>', 'Odoslané <span class="count">(%s)</span>', 'woocommerce') //_n_noop( 'Back Order <span class="count">(%s)</span>', 'Back Order <span class="count">(%s)</span>', 'woocommerce' )
        );
        $order_statuses['wc-order-confirmed'] = array(
            'label'                     => 'Potvrdená', //_x( 'Back Order', 'Order status', 'woocommerce' ),
            'public'                    => false,
            'exclude_from_search'       => false,
            'show_in_admin_all_list'    => true,
            'show_in_admin_status_list' => true,
            'label_count'               => _n_noop('Potvrdená <span class="count">(%s)</span>', 'Potvrdené <span class="count">(%s)</span>', 'woocommerce') //_n_noop( 'Back Order <span class="count">(%s)</span>', 'Back Order <span class="count">(%s)</span>', 'woocommerce' )
        );
        $order_statuses['wc-order-done'] = array(
            'label'                     => 'Hotová', //_x( 'Back Order', 'Order status', 'woocommerce' ),
            'public'                    => false,
            'exclude_from_search'       => false,
            'show_in_admin_all_list'    => true,
            'show_in_admin_status_list' => true,
            'label_count'               => _n_noop('Hotová <span class="count">(%s)</span>', 'Hotové <span class="count">(%s)</span>', 'woocommerce') //_n_noop( 'Back Order <span class="count">(%s)</span>', 'Back Order <span class="count">(%s)</span>', 'woocommerce' )
        );

        return $order_statuses;
    }
}

new WC_LR_custom_order_statuses();

here描述了相同的问题,但我正确地使用了未使用wc-前缀的状态
任何建议如何调试这个问题将不胜感激。

mnowg1ta

mnowg1ta1#

发现我需要添加电子邮件操作。不知道为什么,没有看到任何文档围绕这一点,但这是突破点:)

add_filter('woocommerce_email_actions', [$this, 'filter_woocommerce_email_actions'], 10, 1);

function filter_woocommerce_email_actions($actions)
{
    $actions[] = 'woocommerce_order_status_confirmed';
    $actions[] = 'woocommerce_order_status_payment-await';
    $actions[] = 'woocommerce_order_status_payment-received';
    $actions[] = 'woocommerce_order_status_shipped';
    $actions[] = 'woocommerce_order_status_on-hold_to_cancelled';
    return $actions;
}

在具有与上面相同的动作钩子名称的扩展电子邮件开始触发并且电子邮件被成功发送之后。

相关问题