WooCommerce 不会触发自定义订单状态的 "woocommerce_order_status_changed" 挂钩

WooCommerce won't trigger "woocommerce_order_status_changed" hook for custom order statuses

我开始在操作 woocommerce_order_status_changed 时遇到问题,并且它从未因我的任何自定义状态而触发。

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

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 管理页面上。为了添加我使用的状态:

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 之前,此功能一直有效。我还尝试了所有其他用于状态更改或转换的挂钩 none,其中的工作正常。任何建议表示赞赏。

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

  • init 挂钩已替换为 woocommerce_register_shop_order_post_statuses 以注册自定义订单状态。
  • 增加
  • wc_order_statuses在下拉列表中显示订单状态@单笔订单
  • 添加
  • bulk_actions-edit-shop_order以在下拉列表中显示订单状态@bulk actions
// 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 变量 包含您的自定义订单状态,因此您知道挂钩已触发

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 );