WooCommerce checkout - how to provide free shipping to 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 to allow free shipping not for the product, but for a specific combination of street and postal codes in the delivery address. I learned how to check this for a registered user, but cannot find the correct variables that have this information when placing an order. Any help would be greatly appreciated.

Thanks in advance, Ben

+5
source share
2 answers

Woocommerce already has shipping zones. For each specific zone, you can set the delivery method to either Flat rate or Free delivery. You can check it in the Woocommerce-> Preferences section. Find the Delivery tab.

Screenshot for delivery tab

0
source

Yes, you can, there is an additional parameter that you can pass $ hook to this package. 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 it for free delivery of selected postal codes or streets.

0
source

Source: https://habr.com/ru/post/1238910/


All Articles