I'm trying to hide WooCommerce shipping methods based on variations chosen by the user. I have a product attribute named "Versione" and used for variations. This attribute has two terms with slug: "digitale" and "cartaceo". I would like to hide free_shipping:5 and flat_rate:3 shipping method rate IDs, when a product variation with "digitale" attribute term, is in cart. I would like to hide free_shipping:4 shipping method rate ID, when a product variation with "cartaceo" attribute term, is in cart. I've come across this code: add_filter( 'woocommerce_package_rates', 'ts_hide_shipping_method_based_on_variation_product_attribute', 10, 2 ); function ts_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_versione'; // 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( 'flat_rate:3' => array('digitale'), 'free_shipping:4' => array('cartaceo'), ); // 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; } Hide WooCommerce shipping methods based on the variation product attribute, answer to my previous question, didn't work.
Jese Leos
August 19, 2024
Verified user
You can try like this add_filter( 'woocommerce_package_rates', 'ts_hide_shipping_method_based_on_variation_product_attribute', 10, 2 ); function ts_hide_shipping_method_based_on_variation_product_attribute( $rates, $package ) { if ( is_admin() && ! defined( 'DOING_AJAX' ) ) { return $rates; } // Define the product attribute taxonomy (starts with "pa_") $taxonomy = 'pa_versione'; // Define shipping method rate IDs to be removed based on product attribute term(s) slugs $data_array = array( 'flat_rate:3' => array('digitale'), 'free_shipping:5'=> array('digitale'), 'free_shipping:4'=> array('cartaceo'), ); // Loop through cart items foreach ( $package['contents'] as $cart_item ) { if ( isset( $cart_item['variation']['attribute_' . $taxonomy] ) ) { // Get the selected product attribute term slug $term_slug = $cart_item['variation']['attribute_' . $taxonomy]; // Loop through the data array foreach ( $data_array as $rate_id => $term_slugs ) { if ( in_array( $term_slug, $term_slugs ) && isset( $rates[$rate_id] ) ) { // Remove the shipping method if it matches the product attribute term unset( $rates[$rate_id] ); } } } } return $rates; } Admin Check: The function first checks if it's running in the admin area (unless via AJAX) to avoid interfering with backend operations. Taxonomy Definition: The taxonomy pa_versione corresponds to the product attribute "Versione". Ensure that this matches the attribute used for your product variations. Shipping Method Mapping: The $data_array maps the shipping methods to be hidden (flat_rate:3, free_shipping:5, free_shipping:4) based on the variation term slug (digitale, cartaceo). Cart Items Loop: The function loops through each item in the cart to check if it has the specified variation. If a match is found, the corresponding shipping method is removed.