Ecommerce Shopify WordPress Discussion

Hide multi WooCommerce shipping methods based on product attributes not used for variations

On my WooCommerce website, each product has four attributes (Size, Colour, Season and Brand). The attributes Size and Colour are used for variations, while Season and Brand are not used for variations. I set up 3 shipping methods: Free Shipping (ID 17) with no minimum order, Free Shipping (ID 8) with a minimum order of €100, and finally Paid Shipping (ID 1) of €9.99. I would like to have Free Shipping (ID 17) with a minimum order of €100 and Paid Shipping (ID 1) of €9.99 when purchasing a product from the S24 season, whereas if you purchase a product from the F23 season, Free Shipping (ID 8) with a minimum order of €100 and Paid Shipping (ID 1) of €9.99 must be activated. In the last case, where you buy at least one product from the S24 season and at least one product from the F23 season, only the free shipping (ID 17) without a minimum order must be activated . Here is the starting code, but it only works with the attributes used for variations: add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_product_attribute', 10, 2 ); function hide_shipping_method_based_on_product_attribute( $rates, $package ) { if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; // Product Attribute taxonomy (start always with "pa_") $taxonomy = 'pa_season'; // Example for "Season" // Array of Shipping method rate ID and term(s) slug(s) pairs $data_array = array( 'free_shipping:8' => array('s24'), 'free_shipping:17' => array('f23'), 'flat_rate:1' => array('f23'), ); // Loop through items in the current shipping package foreach( $package['contents'] as $item ){ // Get the parent variable product if it's a product variation $product = $item['variation_id'] > 0 ? wc_get_product($item['product_id']) : $item['data']; // Loop through our data array foreach( $data_array as $rate_id => $term_slugs ) { // loop through product attributes foreach($product->get_attributes() as $attribute ) { if( $attribute->get_taxonomy() === $taxonomy && isset($rates[$rate_id]) && count( array_intersect($attribute->get_slugs(), $term_slugs) ) > 0 ) { unset($rates[$rate_id]); } } } } return $rates; }
The following will handle all your required cases: "S24" term found: Free Shipping (ID 17 with min subtotal of €100) and Paid Shipping activated, "F23" term found: Free Shipping (ID 8 with min subtotal of €100) and Paid Shipping activated, "S24" and "F23" terms found: Free Shipping (ID 17 with min subtotal of €100) activated. Important: You will need to remove from both Free shipping methods settings, the minimum order amount restriction, as it is handled in the code. Try the following: add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_product_attribute', 90000, 2 ); function hide_shipping_method_based_on_product_attribute( $rates, $package ) { $free_min_subtotal = 100; // Set the minimal amount for free shipping $taxonomy = 'pa_season'; // Product Attribute taxonomy (start always with "pa_") $has_s24 = $has_f23 = false; // Initializing $cart_subtotal = $package['cart_subtotal']; // Or use $package['contents_cost'] to exclude taxes // Loop through items in the current shipping package foreach( $package['contents'] as $item ){ // Get the parent variable product if it's a product variation $product = $item['variation_id'] > 0 ? wc_get_product($item['product_id']) : $item['data']; // Loop through product attributes foreach($product->get_attributes() as $attribute ) { // Check for "Season" PA if( $attribute->get_taxonomy() === $taxonomy ) { $term_slugs = $attribute->get_slugs(); if ( in_array( 's24', $attribute->get_slugs() ) ) { $has_s24 = true; } elseif ( in_array( 'f23', $attribute->get_slugs() ) ) { $has_f23 = true; } } } } // Case 1 (term "s24" found) if ( isset($rates['free_shipping:17']) && isset($rates['flat_rate:1']) && $has_s24 && ! $has_f23 ) { if ( isset($rates['free_shipping:8']) ) { unset($rates['free_shipping:8']); // remove Free Shipping 8 } // Handle minimum cart amount for free shipping if ( isset($rates['free_shipping:17']) && floatval($cart_subtotal) < $free_min_subtotal ) { unset($rates['free_shipping:17']); // remove Free Shipping 17 (required subtotal is lower) } } // Case 2 (term "f23" found) elseif ( isset($rates['free_shipping:8']) && isset($rates['flat_rate:1']) && $has_f23 && ! $has_s24 ) { if ( isset($rates['free_shipping:17']) ) { unset($rates['free_shipping:17']); // remove Free Shipping 17 } // Handle minimum cart amount for free shipping if ( isset($rates['free_shipping:8']) && floatval($cart_subtotal) < $free_min_subtotal ) { unset($rates['free_shipping:8']); // remove Free Shipping 8 (required subtotal is lower) } } // Case 3 (terms "s24" and "f23" found) elseif ( isset($rates['free_shipping:17']) && $has_s24 && $has_f23 ) { if ( isset($rates['free_shipping:8']) ) { unset($rates['free_shipping:8']); // remove Free Shipping 8 } if ( isset($rates['flat_rate:1']) ) { unset($rates['flat_rate:1']); // remove Flat rate 1 } } // Fallback: For security regarding free shipping and minimal required subtotal elseif ( floatval($cart_subtotal) < $free_min_subtotal ) { if ( isset($rates['free_shipping:8']) ) { unset($rates['free_shipping:8']); // remove Free Shipping 8 } if ( isset($rates['free_shipping:17']) ) { unset($rates['free_shipping:17']); // remove Free Shipping 17 } } return $rates; } Code goes in functions.php file of your child theme (or in a plugin). It should work. Note: Don't forget to empty the cart, to refresh shipping cached data.

January 20, 2024

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.