首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >WooCommerce不会为自定义订单状态触发"woocommerce_order_status_changed“钩子

WooCommerce不会为自定义订单状态触发"woocommerce_order_status_changed“钩子
EN

Stack Overflow用户
提问于 2022-03-06 19:22:54
回答 1查看 366关注 0票数 1

我开始出现操作woocommerce_order_status_changed的问题,它从未触发过我的任何自定义状态。

我发现只有当class-wc-order.php不是false时才会在$status_transition中使用钩子。

代码语言:javascript
运行
复制
if ( $status_transition ) {
    try {
        do_action( 'woocommerce_order_status_' . $status_transition['to'], $this->get_id(), $this );

        if ( ! empty( $status_transition['from'] ) ) {
            /* translators: 1: old order status 2: new order status */
            $transition_note = sprintf( __( 'Order status changed from %1$s to %2$s.', 'woocommerce' ), wc_get_order_status_name( $status_transition['from'] ), wc_get_order_status_name( $status_transition['to'] ) );

            // Note the transition occurred.
            $this->add_status_transition_note( $transition_note, $status_transition );

            do_action( 'woocommerce_order_status_' . $status_transition['from'] . '_to_' . $status_transition['to'], $this->get_id(), $this );
            do_action( 'woocommerce_order_status_changed', $this->get_id(), $status_transition['from'], $status_transition['to'], $this );
            // Etc...

我创建的所有自定义状态都在WooCommerce管理页面上显示。用于添加我使用的状态:

代码语言:javascript
运行
复制
function register_statuses()
    {
        register_post_status('wc-payment-await', array(
            'label'                     => 'Awaiting payment',
            'public'                    => true,
            'exclude_from_search'       => false,
            'show_in_admin_all_list'    => true,
            'show_in_admin_status_list' => true,
            'label_count'               => _n_noop('Awaiting payment <span class="count">(%s)</span>', 'Awaiting payment <span class="count">(%s)</span>', 'woocommerce')
        )); 
// more statuses here

}
add_action('init', 'register_statuses');

我发现,每当我用新状态保存顺序时,$status_transition总是错误的。直到我最近升级了WooCommerce,这个特性才起作用。此外,我尝试了所有其他挂钩的状态变化或转换,没有任何一个工作。如有任何建议,敬请见谅。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-07 09:55:05

要添加自定义订单状态,我重写了您的代码,因为:

  • init钩子已被woocommerce_register_shop_order_post_statuses替换,以注册自定义订单状态。
  • 添加wc_order_statuses以显示下拉@单个订单中的订单状态
  • 添加bulk_actions-edit-shop_order是为了在下拉@ bulk操作中显示订单状态
代码语言:javascript
运行
复制
// Register order status
function filter_woocommerce_register_shop_order_post_statuses( $order_statuses ) {
    // Status must start with "wc-"
    $order_statuses['wc-payment-await'] = array(
        'label'                     => _x( 'Awaiting payment', 'Order status', 'woocommerce' ),
        'public'                    => false,
        'exclude_from_search'       => false,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        /* translators: %s: number of orders */
        'label_count'               => _n_noop( 'Awaiting payment <span class="count">(%s)</span>', 'Awaiting payment <span class="count">(%s)</span>', 'woocommerce' ),   
    );
    
    return $order_statuses;
}
add_filter( 'woocommerce_register_shop_order_post_statuses', 'filter_woocommerce_register_shop_order_post_statuses', 10, 1 );

// Show order status in the dropdown @ single order
function filter_wc_order_statuses( $order_statuses ) {  
    $new_order_statuses = array();

    // Add new order status after processing
    foreach ( $order_statuses as $key => $status ) {

        $new_order_statuses[ $key ] = $status;

        if ( 'wc-processing' === $key ) {
            // Status must start with "wc-"
            $new_order_statuses['wc-payment-await'] = _x( 'Awaiting payment', 'Order status', 'woocommerce' );
        }
    }

    return $new_order_statuses;
}
add_filter( 'wc_order_statuses', 'filter_wc_order_statuses', 10, 1 );

// Show order status in the dropdown @ bulk actions
function filter_bulk_actions_edit_shop_order( $bulk_actions ) {
    // Note: "mark_" must be there instead of "wc"
    $bulk_actions['mark_payment-await'] = __( 'Awaiting payment', 'woocommerce' );
    return $bulk_actions;
}
add_filter( 'bulk_actions-edit-shop_order', 'filter_bulk_actions_edit_shop_order', 10, 1 );

关于您的问题:“我的任何定制订单状态都不会触发woocommerce_order_status_changed。”

如果使用以下代码,您将看到$new_status变量包含自定义订单状态,因此您知道钩子是被触发的

代码语言:javascript
运行
复制
function action_woocommerce_order_status_changed( $order_id, $old_status, $new_status, $order ) {   
    var_dump( $old_status );
    var_dump( $new_status );
    die();
}
add_action( 'woocommerce_order_status_changed', 'action_woocommerce_order_status_changed', 10, 4 );
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71373492

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档