skip to Main Content

Product A is added in cart and it has shipping fees. Product B is added in cart and once its added, shipping should be free. However Woocoomerce charges shipping, because Cart includes product with fees and without fees.

I Tried this code but it didn’t work

function wcs_my_free_shipping( $is_available ) {
    global $woocommerce;
 
    // set the product ids that are eligible
    $eligible = array( '560' );
 
    // 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 );

2

Answers


  1. Chosen as BEST ANSWER

    I used two codes

    function wcs_my_free_shipping( $is_available ) {
    global $woocommerce;
    
    // set the product ids that are eligible
    $eligible = array( '560', '562', '563', '564');
    
    
    // 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 );

    and

    function wcs_my_free_shipping( $is_available ) {
    global $woocommerce;
    
    // set the product ids that are eligible
    $eligible = array( '560', '562', '563', '564');
    
    
    // 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 );


  2. Try the following code replacement, hiding other shipping methods when free shipping is available:

    add_filter( 'woocommerce_package_rates', 'hide_other_shipping_when_free_is_available', 100, 2 );
    function hide_other_shipping_when_free_is_available( $rates, $package ) {
        $free = array();
    
        // Loop through shipping rates
        foreach ( $rates as $rate_id => $rate ) {
            // If free shipping is available
            if ( 'free_shipping' === $rate->method_id ) {
                $free[ $rate_id ] = $rate;
                break;
            }
        }
        return ! empty( $free ) ? $free : $rates;
    }
    
    add_filter( 'woocommerce_shipping_free_shipping_is_available', 'filter_free_shipping_is_available', 100, 3 );
    function filter_free_shipping_is_available( $is_available, $package, $shipping_method ) {
     
        // Here set the product IDs that will enable free shipping
        $targeted_product_ids = array( 560 );
    
        // Loop through items for the current shipping package
        foreach ( $package['contents'] as $item ) {
            // Check if any targeted product is in cart
            if( in_array( $item['product_id'], $targeted_product_ids ) ) {
                return true;
            }
        }
        return $is_available;
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). It could work.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search