Hide Add to cart button and option if product stock is 0
Hello everyone,
I need a solution to hide "add to cart" button from product page, category page, search page, tag page and other listing pages when the item stock is zero.
Thank you for your support.
To Hide Add to cart button if product stock is 0 do the following...
For Product Page
Open product.twig files from catalog\view\theme\default\template\product. and search for
<button type="button" id="button-cart" data-loading-text="{{ text_loading }}" class="btn btn-primary btn-lg btn-block">{{ button_cart }}</button>
and replace it by
{% if (stock == "In Stock") %}
<button type="button" id="button-cart" data-loading-text="{{ text_loading }}" class="btn btn-primary btn-lg btn-block">{{ button_cart }}</button>
{% endif %}
For Category and Search Page
Open category.php and search.php files from catalog\controller\product search for
'rating' => $result['rating'],
and replace it by
'rating' => $result['rating'],
'product_quantity' => $result['quantity'],
'product_stock' => $result['stock_status'],
'text_stock' => $this->language->get('text_stock'),
Open category.twig and search.twig file from catalog\view\theme\default\template\product. and search for
<button type="button" onclick="cart.add('{{ product.product_id }}', '{{ product.minimum }}');"><i class="fa fa-shopping-cart"></i> <span class="hidden-xs hidden-sm hidden-md">{{ button_cart }}</span></button>
and replace it by
{% if product.product_quantity>0 %}
<button type="button" onclick="cart.add('{{ product.product_id }}', '{{ product.minimum }}');"><i class="fa fa-shopping-cart"></i> <span class="hidden-xs hidden-sm hidden-md">{{ button_cart }}</span></button>
{% endif %}
I hope this answer might help you