mario
mario

Reputation: 147

Shipping costs and restrictions based on location and product category in WooCommerce

I'm using Woocommerce and I'd like to restrict the shipping class 'alcoholics' and allow it just for just Italy and no other countries, can you help me with this code?

function( $is_available ) {
 
    $cart_items = WC()->cart->get_cart();
     
    foreach ( $cart_items as $cart_item ) {
 
        $product = $cart_item['data'];
        $class   = $product->get_shipping_class();
         
        if ( $class === 'alcoholics' ) {
            return false;
        }
    }
     
    return $is_available;
}

add_filter( 'woocommerce_states', 'AL_woocommerce_states' );
 

function AL_woocommerce_states( $states ) {
 
      $states['IT'] = array(
      'AG' => __( 'Agrigento', 'woocommerce' ),
      'AL' => __( 'Alessandria', 'woocommerce' ),
      'AN' => __( 'Ancona', 'woocommerce' ),
      'AO' => __( 'Aosta', 'woocommerce' ),
      'AR' => __( 'Arezzo', 'woocommerce' ),
      'AP' => __( 'Ascoli Piceno', 'woocommerce' ),
      'AT' => __( 'Asti', 'woocommerce' ),
      'AV' => __( 'Avellino', 'woocommerce' ),
      'BA' => __( 'Bari', 'woocommerce' ),
      'BT' => __( 'Barletta-Andria-Trani', 'woocommerce' ),
      'BL' => __( 'Belluno', 'woocommerce' ),
      'BN' => __( 'Benevento', 'woocommerce' ),
      'BG' => __( 'Bergamo', 'woocommerce' ),
      'BI' => __( 'Biella', 'woocommerce' ),
      'BO' => __( 'Bologna', 'woocommerce' ),
       );
 
       return $states;
}

Upvotes: 1

Views: 2668

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253949

You can't restrict Shipping Classes themselves. The country restriction is made by the Shipping Zones in witch you can set specific shipping methods for each of them. Shipping classes allow to manipulate the rates in each "flat rate" shipping method.

Finally You can set a shipping class for a product (or a product variation), that will allow you to have a custom shipping method rate for the chosen products.

Instead you can conditionally manipulate each Shipping method set in your Shipping zones by its unique rate ID using woocommerce_package_rates filter hook:

add_filter( 'woocommerce_package_rates', 'manipulating_shipping_flat_rates', 10, 2 );
function custom_delivery_flat_rate_cost_calculation( $rates, $package )
{
    // Initializing variables
    $has_products = false;
    $has_cat = false;
    
    // Iterating through all cart items
    foreach(WC()->cart->get_cart() as $cart_item ):
    
        $product_id = $cart_item['product_id']; // product ID
        $variation_id = $cart_item['variation_id']; // variation ID
        
        // Detecting a targeted product category
        if( has_term( 'clothing', 'product_cat', $product_id ) )
            $has_cat = true;
            
        // Detecting specific product IDs
        if ( in_array( $cart_item['product_id'], array(20, 22, 28, 47, 58)) )
            $has_products = true;
    endforeach;
    

    foreach($rates as $rate_key => $rate_values){
        $method_id = $rate_values->method_id;
        $rate_id = $rate_values->id;
        
        // Targetting Flat rate Shipping Method
        if ( 'flat_rate' === $rate_values->method_id ) {

            
            ## 1) UNSETTING RATE IDs
            
            // for a product category
            if ( $has_cat ) {
                unset( $rates['flat_rate:5'] );
            }
            // for a product Ids
            if ( $has_products ) {
                unset( $rates['flat_rate:14'] );
            }
                
            ## 2) Manipulating specif Rates IDs prices  
            
            // for a product category and a specific rate ID
            if ( $has_cat && 'flat_rate:8' == $rate_id ) {
                $rates[$rate_id]->cost = $rates[$rate_id]->cost * 0.1;
                $rates[$rate_id]->taxes[1] = $rates[$rate_id]->taxes[1] * 0.1;
            }
            // for a product Ids and a specific rate ID
            if ( $has_products && 'flat_rate:11' == $rate_id ) {
                $rates[$rate_id]->cost = $rates[$rate_id]->cost * 0.1;
                $rates[$rate_id]->taxes[1] = $rates[$rate_id]->taxes[1] * 0.1;
            }
        }
    }

    return $rates;
}

You will need to change the code setting your own product categories, product IDs, Shipping rates IDs and rates costs, to make it working for your needs …

Code goes in functions.php file of your active child theme (or theme) or also in any plugin file.


Related answer: WooCommerce: update custom fields after checkout validation failure

Upvotes: 1

Related Questions