Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Enable Shipping for Certain Products Based on Product Quantity in WooCommerce?

Have control over the availability of shipping based on the quantity of certain products using this handy code snippet. If the total quantity of the specified products reaches or exceeds 12, the specified shipping method is removed from the available options during checkout.

add_filter( 'woocommerce_package_rates', 'ts_specific_products_shipping_methods', 10, 2 );
function ts_specific_products_shipping_methods( $rates, $package ) {

    $product_ids = array( 500 , 100, 470 , 71 ); // HERE set the product IDs in the array
    $method_id   = 'flat_rate:4'; // HERE set the shipping method ID
    $quantity    = 0;
    

    // Get cart items for the current shipping package
    foreach( $package['contents'] as $cart_item ) {
        if ( in_array( $cart_item['product_id'], $product_ids ) ){
            $quantity += $cart_item['quantity'];
        }
    }
    
    if ( $quantity >= 12 && isset($rates[$method_id]) ) {
        unset($rates[$method_id]);
    }
        

    return $rates;
}

Output

If a product with the ID specified in the code (for example, a sunglass product) is added to the cart, and the total quantity of that sunglass product reaches or exceeds 12 units, the code will hide a specific flat-rate shipping method during the checkout process.

How to Enable Shipping for Certain Products Based on Product Quantity in WooCommerce? - Tyche Softwares

Read Related Article: How to Enable Shipping for Certain Products Based on Product Quantity in WooCommerce?

Also, you can adjust shipping costs for minimum and maximum quantity in WooCommerce which will help store owners to set different rates for customers who buy less and for those who buy more items.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x