Page 1 of 1

[HOWTO] Opencart 4 Disable Add to cart / Paypal Buttons when zero stock.

Posted: Thu Sep 01, 2022 4:29 pm
by simondoc
This is a simple mod that takes 2 minutes.
All we are doing is pulling the quantity from the database into a data parameter,
Then switching on the add to cart buttons of that value is not zero.

First we pull the quantity value from the database and into a data parameter:
In file:/catalog/controller/product/product.php
Find:

Code: Select all

if ($product_info['quantity'] <= 0) {
Add this line before it:

Code: Select all

$data['stockb'] = $product_info['quantity'];
Save and close the file.

Then we switch the buttons on and off depending on if the value is zero or not with a simple 'if' statement
In file /catalog/view/template/product/product.twig
Find:

Code: Select all

<button type="submit" id="button-cart" class="btn btn-primary btn-lg btn-block">{{ button_cart }}</button>
Add this line before it:

Code: Select all

{% if stockb > 0 %}
Add this line after it:

Code: Select all

{% endif %}
I would have posted it in the OC4 section but it looks like I don't have permissions to post there.
Cheers.
Psi