Aller au contenu
FR

Free Shipping for WooCommerce

Version audio de cet article

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.