【问题标题】:Set custom status if WooCommerce order has items from a certain category如果 WooCommerce 订单包含来自某个类别的商品,则设置自定义状态
【发布时间】:2021-04-30 02:05:03
【问题描述】:

通过以下代码,我在 WooCommerce 中添加了自定义订单状态(Abonnement)

function register_abonnement_order_status() {
    register_post_status( 'wc-abonnement', array(
        'label'                     => 'Abonnement',
        'public'                    => true,
        'exclude_from_search'       => false,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'label_count'               => _n_noop( 'abonnement (%s)', 'abonnement (%s)' )
    ) );
}
add_action( 'init', 'register_abonnement_order_status' );

// Add to list of WC Order statuses
function add_abonnement_to_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 ) {
            $new_order_statuses['wc-abonnement'] = 'Abonnement';
        }
    }
 
    return $new_order_statuses;
}
add_filter( 'wc_order_statuses', 'add_abonnement_to_order_statuses' );

然后我使用Change WooCommerce order status based on approved status and specific order item 回答代码进一步帮助我。

我修改了该答案以进行下一步:

    function action_woocommerce_order_status_changed( $order_id, $old_status, $new_status, $order ) {
// Compare
    if( $old_status === 'processing' ) {
        // Get items
        $items = $order->get_items();

        foreach ( $items as $item ) {
            // Get products categories
           global $post;
            $terms = get_the_terms( $post->ID, 'product_cat' );
                foreach ($terms as $term) {
                $product_cat_id = $term->term_id;
            break;
            }
            $product_cat = $item->get_product_cat();

            if ($product_cat == 249 ) {
                $order->update_status( 'abonnement' );
                break;
            }
        }
    }
}
add_action( 'woocommerce_order_status_changed', 'action_woocommerce_order_status_changed');

但这似乎不起作用。所以我不确定我错过了什么?

【问题讨论】:

    标签: php wordpress woocommerce status orders


    【解决方案1】:
    • 通过代码中添加的注释标签进行解释

    要在每个订单后自动更改订单状态,当订单包含属于特定类别的商品时,请使用:

    /**
     * Change Order Status
     */
    function action_woocommerce_thankyou( $order_id ) {
        if ( ! $order_id ) return;
    
        // Get order object
        $order = wc_get_order( $order_id );
    
        // Specific categories: the term name/term_id/slug. Several could be added, separated by a comma
        $categories = array( 'categorie-1', 'categorie-2', 15, 16 );
        
        // Flag
        $found = false;
        
        // Loop through order items
        foreach ( $order->get_items() as $item ) {
            // Product ID
            $product_id = $item->get_variation_id() > 0 ? $item->get_variation_id() : $item->get_product_id();
    
            // Has term (product category)
            if ( has_term( $categories, 'product_cat', $product_id ) ) {
                $found = true;
                break;
            }
        }
        
        // True
        if ( $found ) {
            // Status without the "wc-" prefix || Some options: pending, processing, on-hold, completed, cancelled, refunded, failed, etc...
            $order->update_status( 'abonnement' );
        }
    }
    add_action( 'woocommerce_thankyou', 'action_woocommerce_thankyou', 10, 1 );
    

    使用woocommerce_order_status_changed钩子,您可以在其中定位您的订单状态转换fromto,将订单状态更改为任何其他状态。

    function action_woocommerce_order_status_changed( $order_id, $old_status, $new_status, $order ) {
        // Compare
        if ( $old_status === 'processing' ) {
            // Specific categories: the term name/term_id/slug. Several could be added, separated by a comma
            $categories = array( 'categorie-1', 'categorie-2', 15, 16 );
            
            // Flag
            $found = false;
            
            // Loop through order items
            foreach ( $order->get_items() as $item ) {
                // Product ID
                $product_id = $item->get_variation_id() > 0 ? $item->get_variation_id() : $item->get_product_id();
    
                // Has term (product category)
                if ( has_term( $categories, 'product_cat', $product_id ) ) {
                    $found = true;
                    break;
                }
            }
            
            // True
            if ( $found ) {
                // Status without the "wc-" prefix || Some options: pending, processing, on-hold, completed, cancelled, refunded, failed, etc...
                $order->update_status( 'abonnement' );
            }
        }
    }
    add_action( 'woocommerce_order_status_changed', 'action_woocommerce_order_status_changed', 10, 4 );
    


    可选:要注册新的订单状态,您可以用更新后的代码替换当前代码

    /**
     * Register Order Status
     */
    function filter_woocommerce_register_shop_order_post_statuses( $order_statuses ) {
        // Status must start with "wc-"
        $order_statuses['wc-abonnement'] = array(
            'label'                     => _x( 'Abonnement', '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( 'Abonnement <span class="count">(%s)</span>', 'Abonnement <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 ) {
        // Status must start with "wc-"
        $order_statuses['wc-abonnement'] = _x( 'Abonnement', 'Order status', 'woocommerce' );
        
        return $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_abonnement'] = __( 'Change status to abonnement', 'woocommerce' );
        return $bulk_actions;
    }
    add_filter( 'bulk_actions-edit-shop_order', 'filter_bulk_actions_edit_shop_order', 10, 1 );
    

    【讨论】:

    • 我无法自动更改订单状态。这就是为什么我尝试使用 Cat Id 而不是 name 数组。但这似乎不起作用。
    • @cd_BE 您可以将类别名称替换为猫 ID,请参阅 has_term() 和我的更新答案。
    • 试过了,但没有用。
    • 你使用的是woocommerce_thankyou钩子代码还是woocommerce_order_status_changed钩子?我已经测试了这两个代码以及在 WooCommerce 4.9.2 中添加新订单状态的代码,我可以确认它有效。
    • 只有通过调试才能找到它不工作的地方,例如你可以将flag变量默认设置为true,这样你就可以判断代码是否自己做某事没有类别条件。
    猜你喜欢
    • 1970-01-01
    • 2018-09-23
    • 2022-01-19
    • 2020-12-20
    • 2021-09-04
    • 2018-12-30
    • 2017-04-12
    • 1970-01-01
    • 2018-09-10
    相关资源
    最近更新 更多