Prevent gift cards from counting towards the Free Shipping amount
Some stores will offer Free Shipping once a certain amount has been spent.
You can prevent the purchase of a gift cards from counting towards the order total when calculating free shipping. Follow these steps:
1. Download the free Code Snippets plugin: https://wordpress.org/plugins/code-snippets/
2. Create a Snippet with the following code:
function woocommerce_cart_get_subtotal_minus_giftcards( $subtotal ) {
$giftcard_subtotal = 0;
foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = wc_get_product( $cart_item['product_id'] );
if ( is_a( $product, 'WC_Product_PW_Gift_Card' ) ) {
$giftcard_subtotal += $cart_item['line_subtotal'];
}
}
$subtotal -= $giftcard_subtotal;
return $subtotal;
}
function free_shipping_recalculation( $is_available, $package, $wc_shipping_method ) {
if ( true === $is_available ) {
remove_filter( 'woocommerce_shipping_free_shipping_is_available', 'free_shipping_recalculation', 10, 3 );
add_filter( 'woocommerce_cart_get_subtotal', 'woocommerce_cart_get_subtotal_minus_giftcards' );
$is_available = $wc_shipping_method->is_available( $package );
add_filter( 'woocommerce_shipping_free_shipping_is_available', 'free_shipping_recalculation', 10, 3 );
remove_filter( 'woocommerce_cart_get_subtotal', 'custom_woocommerce_cart_get_subtotal' );
}
return $is_available;
}
add_filter( 'woocommerce_shipping_free_shipping_is_available', 'free_shipping_recalculation', 10, 3 );