In Woocommerce, if we want to offer free shipping based on the number of cart items.
If we want to do is: buy 2 of anything and get free shipping.
We can do this using below steps
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | add_filter( 'woocommerce_shipping_free_shipping_is_available' , 'free_shipping_for_x_cart_items' , 10, 3 ); function free_shipping_for_x_cart_items( $is_available , $package , $shipping_method ) { $item_count = WC()->cart->get_cart_contents_count(); if ( $item_count == 1 ) { $notice = __( "Add one more for free shipping" ); $is_available = false; } elseif ( $item_count > 1) { $notice = __( "You get free shipping" ); $is_available = true; } if ( isset( $notice ) ) { wc_add_notice( $notice , 'notice' ); } return $is_available ; } |
This code goes in function.php file of your active child theme (or active theme). Tested and works.
The WC_Cart
method get_cart_contents_count()
get the count of all items (including quantities).
To get the count of different cart items (without including quantities), replace the line:
1 | $item_count = WC()->cart->get_cart_contents_count(); |
with this one:
1 | $item_count = sizeof( $package [ 'contents' ]); |
No comments:
Post a Comment