Search code examples
woocommercehidecartshipping-method

Hiding shipping method from Woocommerce cart page ISSUE


I tried to use different codes to hide shipping method from Woocommerce cart page. The codes work until I delete a product from the cart. Once I delete a product the shipping section returns. After I refresh the page its work again.

One of the codes I tried:

add_filter( 'woocommerce_cart_needs_shipping', 'filter_cart_needs_shipping' );
function filter_cart_needs_shipping( $needs_shipping ) {
    if ( is_cart() ) {
        $needs_shipping = false;
    }
    return $needs_shipping;
}

I tried this code also:

function disable_shipping_calc_on_cart( $show_shipping ) {
    if( is_cart() ) {
        return false;
    }
    return $show_shipping;
}
add_filter( 'woocommerce_cart_ready_to_calc_shipping', 'disable_shipping_calc_on_cart', 99 );

I put the code in my theme function.php file. The problem is the same for both codes.

I use WordPress 6.5.2, WooCommerce 8.8.3 and Twenty Twenty-Four theme.

How can this problem be solved?


Solution

  • To disable shipping on woocommerce cart page, the following code works for woocommerce template based cart page.

    add_filter( 'woocommerce_cart_needs_shipping', 'filter_cart_needs_shipping' );
    function filter_cart_needs_shipping( $needs_shipping ) {
        if ( is_cart() ) {
            $needs_shipping = false;
        }
        return $needs_shipping;
    }
    

    But for woocommerce block based cart page, this code does not work, when cart items are updated on cart page. This is due to following reason: On woocommerce block based cart page, the cart items are updated, using woocommerce store API. So, the return value from is_cart() function call is "false", during cart update request on woocommerce block based cart page. To disable shipping on woocommerce block based cart page, the following code can be used.

    add_filter( 'woocommerce_cart_needs_shipping', 'filter_cart_needs_shipping' );
    function filter_cart_needs_shipping( $needs_shipping ) {
        if ( is_cart() ) {
            $needs_shipping = false;
        }
        return $needs_shipping;
    }
    add_filter( 'rest_pre_dispatch', function( $result, $server, $request ) {
        if ( str_contains( $request->get_route(), '/wc/store/v1/cart/remove-item' ) || str_contains( $request->get_route(), '/wc/store/v1/cart/update-item' ) ) {
            add_filter( 'woocommerce_cart_needs_shipping', '__return_false');
        }
        return $result;
    }, 10, 3 );
    

    Updated, dated 9-May-2024,

    To disable shipping on woocommerce block based cart page, including this condition, "If the cart is empty, there is option to add products to cart on woocommerce block based cart page.", the following code can be used.

    add_filter( 'woocommerce_cart_needs_shipping', 'filter_cart_needs_shipping' );
    function filter_cart_needs_shipping( $needs_shipping ) {
        if ( is_cart() ) {
            $needs_shipping = false;
        }
        return $needs_shipping;
    }
    add_filter( 'rest_pre_dispatch', function( $result, $server, $request ) {
        if ( str_contains( $request->get_route(), '/wc/store/v1/cart/remove-item' ) || str_contains( $request->get_route(), '/wc/store/v1/cart/update-item' )) {
            add_filter( 'woocommerce_cart_needs_shipping', '__return_false' );
        }
        else if( str_contains($request->get_route(), '/wc/store/v1/cart' )) {
            $headers = $request->get_headers();
            if( isset($headers['referer'][0]) && ($headers['referer'][0] === wc_get_cart_url() ))
            {
                add_filter( 'woocommerce_cart_needs_shipping', '__return_false' );
            }
        }
        return $result;
    }, 10, 3 );