cancel
Showing results for 
Search instead for 
Did you mean: 

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

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

You need to add registration.php file and module.xml file to create module, you have missed both the files,

app\code\Webkul\Hello\registration.php file,

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Webkul_Hello',
    __DIR__
);

module.xml file at,

app\code\Webkul\Hello\etc\module.xml,

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Webkul_Hello" setup_version="1.0.1">    	
    </module>
</config>

Run below command,

php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
php bin/magento setup:di:compile
php bin/magento indexer:reindex
php bin/magento cache:flush

 

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

Thank you for replies.

 

Now my module and event is working But I don't know how to stop add to cart and print custom mess check my observer code

 

 

<?php

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) {
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cart = $objectManager->get('\Magento\Checkout\Model\Cart');
$itemsCollection = $cart->getQuote()->getItemsCollection();
// get array of all items what can be display directly
$itemsVisible = $cart->getQuote()->getAllVisibleItems();
// get quote items array
$items = $cart->getQuote()->getAllItems();
$sqty=0;
$tqty=0;
$teeqty=0;
foreach($items as $item) {
$pid=$item->getProductId();
$qty=$item->getQty();
if($pid==2056){
$sqty=$sqty+$qty;
if($sqty>4){

// Here I want to stop add to cart and print custom message

}
}
}
}
}

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

You can add below message in your observer to stop add to cart,

you have to keep class above the class defination,

use Magento\Framework\Exception\LocalizedException;

Spoiler
 <?php

namespace Webkul\Hello\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Exception\LocalizedException;

class CustomPrice implements ObserverInterface
{
public function execute(\Magento\Framework\Event\Observer $observer) {
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cart = $objectManager->get('\Magento\Checkout\Model\Cart');
$itemsCollection = $cart->getQuote()->getItemsCollection();
// get array of all items what can be display directly
$itemsVisible = $cart->getQuote()->getAllVisibleItems();
// get quote items array
$items = $cart->getQuote()->getAllItems();
$sqty=0;
$tqty=0;
$teeqty=0;
foreach($items as $item) {
$pid=$item->getProductId();
$qty=$item->getQty();
if($pid==2056){
$sqty=$sqty+$qty;
if($sqty>4){

throw new LocalizedException(__('your custom message.'));


}
}
}
}
}
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

You can use the exception as follows into your condition.

throw new \Magento\Framework\Exception\LocalizedException( __( 'Custom Message' ) );

Do not forget to use the Exception class as:

use Magento\Framework\Exception\LocalizedException;