在交易电子邮件中使用时,Woocommerce 挂钩自定义状态更改不会触发
Woocommerce hook on custom status change wont trigger when used in trannsactional email
我无法收到有关状态更改的交易电子邮件。我在根据需要编程的 woo 商务插件上设置了自定义状态和自定义电子邮件。
代码遵循与 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();
描述了同样的问题,但我在没有使用 wc- 前缀的状态下正确地做到了
如能提供任何调试此问题的建议,我们将不胜感激。
发现我需要添加电子邮件操作。不知道为什么,没有看到任何关于此的文档,但这是突破点:)
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;
}
与上述动作钩子名称相同的扩展邮件开始触发后邮件发送成功。
我无法收到有关状态更改的交易电子邮件。我在根据需要编程的 woo 商务插件上设置了自定义状态和自定义电子邮件。
代码遵循与 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();
描述了同样的问题
如能提供任何调试此问题的建议,我们将不胜感激。
发现我需要添加电子邮件操作。不知道为什么,没有看到任何关于此的文档,但这是突破点:)
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;
}
与上述动作钩子名称相同的扩展邮件开始触发后邮件发送成功。