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.

 

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' );

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

Leave a Reply