In this blog, we validate product quantity while clicking add to cart in Magento 2.
On the product page, we can validate the maximum qty of products to add to the cart.
To achieve it we will follow some steps:
1. Create a custom module in Magento 2
We have created a module WebkulCustomModule.
To create a module you can follow the blog – How to Create Hello World Module in Magento
2. Create module configuration
We have created a module configuration for set product max qty for add to cart.
You can follow this to know more about the system.xml file in magento 2
https://devdocs.magento.com/guides/v2.3/config-guide/prod/config-reference-systemxml.html
etc/adminhtml/system.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd"> <system> <tab id="webkul" translate="label" sortOrder="10"> <label>Webkul</label> </tab> <section id="Custom" translate="label" type="text" sortOrder="300" showInDefault="1" showInWebsite="1" showInStore="1"> <label>Custom Module Settings</label> <!-- Assign section to tab --> <tab>webkul</tab> <resource>Webkul_CustomModule::custommodule_config</resource> <!-- create group for fields in section --> <group id="general_settings" translate="label" type="text" sortOrder="0" showInDefault="1" showInWebsite="1" showInStore="1"> <label>General Settings</label> <field id="max_qty" translate="label" sortOrder="1" type="text" showInDefault="1" showInWebsite="1" showInStore="1"> <label>Max Qty</label> </field> </group> </section> </system> </config>
3. Need to create a plugin in our CustomModule in Magento 2
We have created a Plugin in our module.
There is a design pattern called ‘Interception’ used in Plugins.
Interception is inserting code dynamically without changing the original class behavior. In this process, code is dynamically inserted between the calling code and the target object.
To know more about plugins follow the blog – Magento2 – Create and Use Plugins
we have added the plugin code name and path in the di.xml file
etc/di.xml
<?xml version="1.0"?> <!-- /** * Webkul Software. * * @category Webkul * @package Webkul_CustomModule * @author Webkul * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com) * @license https://store.webkul.com/license.html */ --> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Catalog\Block\Product\View"> <plugin name="Webkul_CustomModule::checkSellerMaxQty" type="Webkul\CustomModule\Plugin\Block\ProductView" /> </type> </config>
4. Need to create a Plugin file
We have created a plugin file
Plugin/Block/ProductView.php
<?php /** * Webkul Software. * * @category Webkul * @package Webkul_CustomModule * @author Webkul * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com) * @license https://store.webkul.com/license.html */ namespace Webkul\CustomModule\Plugin\Block; use Magento\CatalogInventory\Api\StockRegistryInterface; class ProductView { /** * @var StockRegistryInterface */ private $stockRegistry; /** * @var \Magento\Framework\App\Config\ScopeConfigInterface */ protected $scopeConfig; /** * Construct * * @param StockRegistryInterface $stockRegistry * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig */ public function __construct( StockRegistryInterface $stockRegistry, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig ) { $this->stockRegistry = $stockRegistry; $this->scopeConfig = $scopeConfig; } /** * Set Max limit on cart * * @param \Magento\Catalog\Block\Product\View $block * @param array $validators * @return array */ public function afterGetQuantityValidators( \Magento\Catalog\Block\Product\View $block, array $validators ) { $stockItem = $this->stockRegistry->getStockItem( $block->getProduct()->getId(), $block->getProduct()->getStore()->getWebsiteId() ); $productCartLimit = $this->scopeConfig->getValue('custom/general_settings/max_qty'); $params = []; $params['minAllowed'] = (float)$stockItem->getMinSaleQty(); if ($productCartLimit && $productCartLimit != "") { $params['maxAllowed'] = (float)$productCartLimit; } elseif ($stockItem->getMaxSaleQty()) { $params['maxAllowed'] = (float)$stockItem->getMaxSaleQty(); } if ($stockItem->getQtyIncrements() > 0) { $params['qtyIncrements'] = (float)$stockItem->getQtyIncrements(); } $validators['validate-item-quantity'] = $params; return $validators; } }
Run these commands
php bin/magento setup:upgrade php bin/magento setup:di:compile php bin/magento cache:flush
Now we can check on the product page
Be the first to comment.