cancel
Showing results for 
Search instead for 
Did you mean: 

Check maximum configure product qty before add to cart in magento 2

Check maximum configure product qty before add to cart in magento 2

Hi Guys,

How can we stop the customer to buy maximum qty of configure product at the time of add to cart in magento 2.

Thanks
Sanjeev

13 REPLIES 13

Re: Check maximum configure product qty before add to cart in magento 2

You need to set Max qty for shopping cart from Product level.

Go To Admin,

Catalog -> Manage Products 

Edit product,

Click On Advanced Inventory,

Set Maximum Qty Allowed in Shopping Cart as you wish.

Save Product.

Above setting is for product level.

 

For ALL the product,

You can globally setting by

Store -> Configuration -> Catalog -> Inventory

Product stock option tab,

Set Maximum Qty Allowed in Shopping Cart as you wish.

Save Configuration.

 

Clear Cache.

If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

Re: Check maximum configure product qty before add to cart in magento 2

Hi @webkeonsanjeev

 

Well AKAIK the max qty allowed is for a simple product and can be controlled through Magento 2 by changing the Maximum Qty Allowed in Shopping Cart in the Advanced Inventory panel at the product level.

 

However, in your case since you wanted to limit the max number of qty of configurable products you can add to the cart.

 

I know that when a product is added to the cart and is considered to be a configurable, the simple product that expresses the options selected is added to the cart, not the configurable product itself.

 

For this, you had to create an observer that listened to the checkout_cart_product_add_after event.

 

That way when a new product is attempted to be added to the cart, it will check if the product that is attempted to be added belongs to the configurable product that is only allowed to be added to the cart once, if so error out in this case.

 

Refer this link for more details - https://magento.stackexchange.com/questions/125971/magento-2-how-to-set-a-maximum-order-qty-of-indiv...

 

Hope it helps !

if issue solved,Click Kudos & Accept as Solution

Re: Check maximum configure product qty before add to cart in magento 2

Hi ManthanDave

 

I created a events.xml below and Observer but I seems my event observer is not working below is my code.

 

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="checkout_cart_product_add_after">
<observer name="customprice" instance="Webkeon\Hello\Observer\CustomPrice" />
</event>
</config>

 

 

 

namespace Webkeon\Hello\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;

class CustomPrice implements ObserverInterface
{
public function execute(\Magento\Framework\Event\Observer $observer) {
// $item = $observer->getEvent()->getData('quote_item');
$item = $observer->getEvent()->getQuoteItem();
$item = ( $item->getParentItem() ? $item->getParentItem() : $item );

}
}

Re: Check maximum configure product qty before add to cart in magento 2

Hi @webkeonsanjeev

 

I understand - where you have puted your events.xml file ?

 

Also in your execute function - just write below code first :

 

 

echo "goes here";
exit;

 

if this prints goes here then you need to write code for the maximum quantity !

 

Try this and let me know your observer called or not ?

 

if issue solved,Click Kudos & Accept as Solution

Re: Check maximum configure product qty before add to cart in magento 2

My events.xml location is app\code\Webkeon\Hello\etc

 

But its confirm my observer is not calling because echo text is not printing 

Re: Check maximum configure product qty before add to cart in magento 2

No event observer is working how can I check where is the issue?

Re: Check maximum configure product qty before add to cart in magento 2

Hi @webkeonsanjeev

 

Well - first you wanted to identify - why your observer is not calling !

 

Put exact code with file location and directory structure.

 

Then its help us to troubleshoot the issue 

if issue solved,Click Kudos & Accept as Solution

Re: Check maximum configure product qty before add to cart in magento 2

please check 

 

Only 2 files I put

1) events.xml

2) CustomPrice.php

 

app\code\Webkul\Hello\etc

events.xml 

 

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="checkout_cart_product_add_after">
<observer name="customprice" instance="Webkul\Hello\Observer\CustomPrice" />
</event>
</config>

 

--------------------------

app\code\Webkul\Hello\Observer

CustomPrice.php

 

<?php
/**
* Webkul Hello CustomPrice Observer
*
* @category Webkul
* @package Webkul_Hello
* @author Webkul Software Private Limited
*
*/
namespace Webkul\Hello\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;

class CustomPrice implements ObserverInterface
{
public function execute(\Magento\Framework\Event\Observer $observer) {

echo "goes here";

exit;

/* // $item = $observer->getEvent()->getData('quote_item');
$item = $observer->getEvent()->getQuoteItem();
$item = ( $item->getParentItem() ? $item->getParentItem() : $item );
$price = 100; //set your price here
$item->setCustomPrice($price);
$item->setOriginalCustomPrice($price);
$item->getProduct()->setIsSuperMode(true);*/


}
}

 

 

Re: Check maximum configure product qty before add to cart in magento 2

Hi @webkeonsanjeev

 

Well you also required Registration.php and module.xml file to register your module !

 

Here i am sharing link for how to create a custom module in magento 2 - https://devdocs.magento.com/videos/fundamentals/create-a-new-module/

 

Hope it helps

if issue solved,Click Kudos & Accept as Solution