phpwordpresswoocommerceattributesproduct

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?


Solution

  • 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:

    1. 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 ) {
        // 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.


    1. 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 ) {
        // 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