WooCommerce Disable “Out of Stock” Message on Cart Page

WooCommerce is a very powerful tool because it gives its users the ability to make changes according to
their demands. Usually, WooCommerce cart displays out of stock error message even though you have changed
the stock display format to “Never show quantity remaining in stock” from the settings.

This issue can be easily solved by adding a simple code to the existing code of the website, Follow the
steps below,
Go to Dashboard > Appearance > Theme Editor > Theme Functions

Then simply add the following code at the end of the existing code.

 

[codesyntax lang=”php”]

function remove_stock_info_error($error){

    global $woocommerce;

    foreach ($woocommerce->cart->cart_contents as $item) {

        $product_id = isset($item['variation_id']) ? $item['variation_id'] : $item['product_id'];

        $product = new \WC_Product_Factory();

        $product = $product->get_product($product_id);



        if ($item['quantity'] > $product->get_stock_quantity()){

            $name = $product->get_name();

            $error = 'Sorry, we do not have enough "'.$name.'" in stock to fulfill your order. Please edit your cart and try again. We apologize for any inconvenience caused.';

            return $error;

        }

    }

}add_filter( 'woocommerce_add_error', 'remove_stock_info_error' );

[/codesyntax]

After adding the code, click Update File and that’s all.

Waqas

I hope you enjoy reading this blog post. If you want my team to do WooCommerce Maintenance for you, click here.

Leave a Comment

Your email address will not be published. Required fields are marked *