Ecommerce Shopify WordPress Discussion

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

In my WooCommerce web site, each product has four product attributes (Size, Color, Season and Brand). The attributes Size and Color are used for variations, while Season and Brand are not used for variations. The aim is to hide free shipping for products of the "S24" Season. I am starting with the following code, but it only works with the attributes used for variations: add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_variation_product_attribute', 10, 2 ); function hide_shipping_method_based_on_variation_product_attribute( $rates, $package ) { if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; // HERE define the Product Attibute taxonomy (starts always with "pa_") $taxonomy = 'pa_season'; // Example for "Color" // HERE define shipping method rate ID to be removed from product attribute term(s) slug(s) (pairs) in this array $data_array = array( 'free_shipping:8' => array('s24'), 'flat_rate:17' => array('f23'), ); // Loop through cart items foreach( $package['contents'] as $cart_item ){ if( isset($cart_item['variation']['attribute_'.$taxonomy]) ) { // The product attribute selected term slug $term_slug = $cart_item['variation']['attribute_'.$taxonomy]; // Loop through our data array foreach( $data_array as $rate_id => $term_slugs ) { if( in_array($term_slug, $term_slugs) && isset($rates[$rate_id]) ) { // We remove the shipping method corresponding to product attribute term as defined unset($rates[$rate_id]); } } } } return $rates; } How to make it work for product attributes that are not used for variations?
For product attributes that are not used in variations, you need first to get the parent variable product when the cart item is a product variation. Then you can use 2 approaches: Using WC_Product get_attributes() method (the more secure way): 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'), 'flat_rate:17' => 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; } Code goes in functions.php file of your child theme (or in a plugin). It should work. Using WC_Product get_attribute( $attribute ) method, (where $attribute can be the attribute taxonomy or the attribute label name): As the method get_attribute() will return always a string of product term(s) name(s), so we will use it only when the targeted attribute has a unique term set in the product. Then in the code below, we will use term(s) name(s) instead of term(s) slug(s): 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) NAME(s) pairs $data_array = array( 'free_shipping:8' => array('S24'), 'flat_rate:17' => 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_names ) { if( in_array($product->get_attribute($taxonomy), $term_names) && isset($rates[$rate_id]) ) { unset($rates[$rate_id]); } } } return $rates; } Code goes in functions.php file of your child theme (or in a plugin). It should work. Related: Hide shipping methods based on Variation product attribute in WooCommerce

January 17, 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.