3

I want to disable adding a product to cart beyond its stock limit through ajax in opencart 2.x,Now opencart only shows a message on header "Products marked with *** are not available in the desired quantity or not in stock!". But i want products not to be added to cart if more is ordered than what's in stock,now openacart's stuff is time consuming as well as not advance for buyer to make changes again and again,

I tried but not sure where should i make changes ,whether it start in catalog/controller/api/cart.php or in common.js or system/lirary/cart.php ,i try this code-

     if ((int)$qty && ((int)$qty > 0)) {
        if( ($this->session->data['cart'][$key])==(int)$product['stock']){

        }
        else{
            if (!isset($this->session->data['cart'][$key])) {
            $this->session->data['cart'][$key] = (int)$qty;
        } else {
            $this->session->data['cart'][$key] += (int)$qty;
        }
        }

    }
Ankit
  • 87
  • 4
  • 14

2 Answers2

3

If I understand your question, you want to prevent the customer from adding more to their cart than is remaining in stock?

EDIT I've updated the code after checking in OpenCart 2.0.2.0.

OK So first in your controller/checkout/cart.php before the line

if ($json)

in the add() function.

you need the following:

$quantity_in_cart = 0;

$products = $this->cart->getProducts();
foreach ($products as $product) {
    if ($product['product_id'] == $product_id) {
        $quantity_in_cart = $product['quantity'];
        break;
    }
}

if (($quantity + (int)$quantity_in_cart) > $product_info['quantity']) {
    $json['error']['stock'] = $this->language->get('error_not_enough_stock');
}

Then in product.tpl you need code added to the Javascript to display the error if the add fails. It should be placed in the function

$('#button-cart').on('click', function() {

after the lines...

if (json['error']['recurring']) {
    $('select[name=\'recurring_id\']').after('<div class="text-danger">' + json['error']['recurring'] + '</div>');
}

The added code is...

if (json['error']['stock']) {
    $('.breadcrumb').after('<div class="alert alert-danger">' + json['error']['stock'] + '<button type="button" class="close" data-dismiss="alert">&times;</button></div>');
}

Of course you'll have to add similar code to any other tpl that adds products to the cart, and things get more complex when you've got products with options, but this is the basics of it.

komodosp
  • 3,316
  • 2
  • 30
  • 59
  • Yes you understand it right, but opencart is divided into MVC-L,If you can download opencart ,check it and thanks .@colmde – Ankit Oct 12 '15 at 10:13
  • @AnkitAgarwal - I understand that Opencart has MVC-L. I only commented on the "warn the user" part as it wasn't part of your original question. I believe you would set an element in a `$json` array or something which gets sent back to the Ajax call, and then in the Javascript you'd warn the user. I can't do it now, but this evening (GMT) I will be able to check if you haven't resolved it by then. – komodosp Oct 12 '15 at 10:40
  • @ankit - user5419232 had given a better answer than me so I didn't get a chance to look at it yet. I will try later, I think my answer will be the same though. You should provide your XML as s/he requested so we can determine why yours isn't working. – komodosp Oct 15 '15 at 07:06
  • To be totally honest though it looks just like user5419232 above – komodosp Oct 16 '15 at 00:13
  • Worked Thank you so much. – Ankit Oct 16 '15 at 05:33
2

Opencart add/ update product to cart using this controller / function
catalog > controller > checkout > cart.php
function - add / edit

So you have to add conditions to these functions

add this before your add function code

        if (!$json) {

in add function

        if($quantity > $product_info['quantity'])
            $json['warning'] = $this->language->get('error_stock');

it will check if customer added quantity is not more than available one.

then add js code for error in catalog > view > theme > your theme (mine default) > product > product.tpl file

else if(json['warning']) {
    $('#content').parent().before('<div class="alert alert-danger"><i class="fa fa-check-circle"></i> ' + json['warning'] + '<button type="button" class="close" data-dismiss="alert">&times;</button></div>');

    $('html, body').animate({ scrollTop: 0 }, 'slow');
  }

in line last of this ajax

$('#button-cart').on('click', function() {

Bazinga, you are ready to go, Product will not add to cart :)

Note - please use vqmod/ ocmod for it.

Nikhil Chaudhary
  • 478
  • 4
  • 16
  • 1
    Very nice, but should it not be a vQmod :) – Chaoley Oct 13 '15 at 02:57
  • Yes, it must be done by vqmod/ ocmod. Direct changes in core file is bad idea, here I just show you a way. – Nikhil Chaudhary Oct 13 '15 at 03:53
  • I'm planning to set up a maximum quantity for products in the cart so I'll use this as a starting point, thanks for an elegant solution. – Chaoley Oct 13 '15 at 05:04
  • @Ankit not working ? more description will be helpful like showing any js error or response form ajax call or product is already in cart or not ? – Nikhil Chaudhary Oct 13 '15 at 06:36
  • Nothing is happening ,no js error . I tried it many times like adding product one by one or by giving quantity at product page,I made a vqmod for this,and also create modification files as per you told in your answer,but no changes – Ankit Oct 13 '15 at 06:41
  • can you add your xml file here and what OC version you have ? – Nikhil Chaudhary Oct 13 '15 at 07:03
  • I would just add that you should check if the quantity ordered plus the quantity already in the cart is more than the quantity in stock. – komodosp Oct 13 '15 at 07:43
  • using version 2.0.2.0 @user5419232 – Ankit Oct 13 '15 at 09:36
  • @ankit i tested it, [link] (https://box.everhelper.me/attachment/305732/AA7pzd3m49VSMOQbPIBrj1fZjZj7u9LM/424043-lrh6XhdvhwOOZzgA/screen.png) , show me your xml file and i will correct that, if there is any problem in it – Nikhil Chaudhary Oct 13 '15 at 10:15
  • @user5419232 i think you are not getting my problem, i have no problem with error message, like you attached image, i want exaclty that there will be no adding in cart after product is added to cart equal to product stock quantity ,somewhat like disable in background . – Ankit Oct 13 '15 at 11:05
  • This solution will only work if you are allowing customer to mention the quantity. But if you have provided dropdown to select quantity then this will no work at all.. – Gags Jul 09 '16 at 19:09