如果 WooCommerce 订单包含来自某个类别的商品,请设置自定义状态

问题描述

通过以下代码,我在 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');

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

解决方法

  • 通过代码中添加的注释标签进行说明

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

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

OR 使用 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',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_id ) ) {
                $found = true;
                break;
            }
        }
        
        // True
        if ( $found ) {
            // Status without the "wc-" prefix || Some options: pending,etc...
            $order->update_status( 'abonnement' );
        }
    }
}
add_action( 'woocommerce_order_status_changed','action_woocommerce_order_status_changed',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>',);
    
    return $order_statuses;
}
add_filter( 'woocommerce_register_shop_order_post_statuses','filter_woocommerce_register_shop_order_post_statuses',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','woocommerce' );
    
    return $order_statuses;
}
add_filter( 'wc_order_statuses','filter_wc_order_statuses',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',1 );