NB: Make sure you do a back up of your theme, files and database before attempting the tutorials

Intermediate

---- Last modified : Sep 08 2022

Estimated reading time : 1 minute, 5 seconds - 87 words

There are different ways to configure free shipping for specific WooCommerce produits. We recommend using a filter that will assign free shipping to WooCommerce via their IDs.

We will use the woocommerce_shipping_free_shipping_is_available function with ours.

Free Shipping for WooCommerce Products

/////////////////////////////////////////////////////////////////
// Forcer la livraison gratuite /////////////////////////////////
/////////////  /////////////////
/////////////////////////////////////////////////////////////////


function templify_free_shipping( $is_available ) {
	global $woocommerce;
 
	// set the product ids that are eligible
	$eligible = array( '5429', '5425', '5282' );
 
	// 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', 'templify_free_shipping', 20 );

You can see this row in the table: ‘5429’, ‘5425’, ‘5282’. These are the product IDs I wish to assign free shipping to. You can replace them with your own IDs and remember to use a comma between each of them.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>