WooCommerce checkout – how to give free shipping for a specific address

The following code will allow free shipping for a specific product:

    function wcs_my_free_shipping( $is_available ) {
    global $woocommerce;

    // set the product ids that are eligible
    $eligible = array( '360' );

    // get cart contents
    $cart_items = $woocommerce->cart->get_cart();

    // loop through the items looking for one in the eligible array
    foreach ( $cart_items as $key => $item ) {
        if( in_array( $item['product_id'], $eligible ) ) {
            return true;
        }
    }

    // nothing found return the default value
    return $is_available;
   }
   add_filter( 'woocommerce_shipping_free_shipping_is_available', 'wcs_my_free_shipping', 20 );

What I would like to do is allow free shipping not for a product, but for a specific street and zip code combination in the delivery address. I found out how to check this for a logged-in user, but can’t seem to find the right variables that have this information at checkout. Any help would be greatly appreciated.

Read More

Thanks in advance,
-Ben

Related posts

2 comments

  1. There are Shipping zones available in Woocommerce already. For each specific zone you can set the shipping method either “Flat rate” or “Free Shipping”. You can check it under Woocommerce->Settings. Find Shipping tab.

    Screenshot for shipping tab

  2. Yes you can, there is an extra parameter you can pass into this hook names $package.
    for eg.

       add_filter( 'woocommerce_shipping_free_shipping_is_available', 'wcs_my_free_shipping', 20,2 );
    
    function wcs_my_free_shipping( $is_available, $package){
    //Your code for free shipping  for a selected address found in $package
    return $is_available
    }
    

    $package contains the address you entered so you can use this to apply free shipping for selected zip codes or streets

Comments are closed.