Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
241 views
in Technique[技术] by (71.8m points)

php - Set custom status if WooCommerce order has items from a certain category

Via the following code I added a custom order status (Abonnement) in WooCommerce

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

I then used Change WooCommerce order status based on approved status and specific order item answer code to help me further.

I modified that answer to go to the next step:

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

But that doesn't seems to work. So i'm not sure what I'm missing?

question from:https://stackoverflow.com/questions/65898803/set-custom-status-if-woocommerce-order-has-items-from-a-certain-category

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  • Explanation via comment tags added in the code

To automatically change the order status after each order, when the order contains items that belong to certain categories, use:

/**
 * 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 using woocommerce_order_status_changed hook where you can target your orders statuses transition from and to, to change the order status to any other.

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


Optional: to register a new order status, you can replace your current code with this updated code

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...