Search code examples
opencart-3

Opencart. Only "Out of Stock" on the "Add to Cart" button when the quantities = 0


Friends, I need your help.

I want to make the "Out of Stock" status display on the "Add to Cart" button when the quantity in the warehouse becomes "0". Should work in the Category and Product.

I only know this solution:

controller/product/category.php

After: $data['products'][] = array(

Add:

'quantity'      => $result['quantity'],   
'my_text'       => $result['stock_status'],

category.twig

Replace: {{ button_cart }}

To:

{% if product.quantity > 0 %}
{{ button_cart }}
{% else %} 
{{ product.my_text }}
{% endif %}

But this method shows all the statuses, but I need only "out of stock"(id=5) and apply disabled="disabled" to it. Other status names should be displayed and without "disabled".

I know what to start with this:

model/catalog/product.php

After: $query->row['special'],

Add: 'stock_status_id' => $query->row['stock_status_id'],

But what's next?

Sorry for my English. I'm using Google Translate.


Solution

  • In catalog/controller/product/category.php:

    add -

    if($result['quantity'] > 0){
                  $cart = 'ena';
                    $cart_text = $this->language->get('button_cart');
    }
    elseif($result['stock_status_id'] = 5 || $result['quantity'] <= 0){
                    $cart = 'dis';
                    $cart_text = 'Out Of Stock';
                }
    

    before -

    $data['products'][] = array(
    

    add -

    'cart_text'   => $cart_text,
                    'cart'        => $cart,
    

    after -

    $data['products'][] = array(
                    'product_id'  => $result['product_id'],
                    'thumb'       => $image,
                    'name'        => $result['name'],
    

    In catalog/view/product/category.tpl

    edit for button cart as below:

     <button type="button" onclick="cart.add('<?php echo $product['product_id']; ?>', '<?php echo $product['minimum']; ?>');" <?php if($product['cart'] == 'dis') { echo 'disabled'; } ?>><i class="fa fa-shopping-cart"></i> <span class="hidden-xs hidden-sm hidden-md"><?php echo $product['cart_text']; ?></span></button>