Loading...
Magento 2 : Max Qty Allowed on Items Customization while update cart page

Magento 2 : Max Qty Allowed on Items Customization while update cart page

Max Qty Allowed on Items Customization in Magento 2

In Magento 2, there are often restrictions on the maximum quantity of items a customer can purchase. However, this limit may only sometimes be suitable for some stores. To address this issue, Magento 2 allows Customization of the maximum quantity allowed on items. This feature enables store owners to set a specific limit that meets their unique requirements. In this article, we'll explore how to customize the maximum quantity allowed on items in Magento 2 and its benefits for your online store.

Why is Customization necessary for updating the cart page?

Why Customization is necessary for updating the cart page: The updated cart page plays a crucial role in customers' online shopping experience. It allows them to review and modify their selected items before checkout. However, the default settings of Magento 2 may only sometimes meet the needs and preferences of all store owners or customers. 

As a result, Customization of the update cart page becomes necessary to improve its functionality and enhance customer satisfaction. In this article, we'll discuss the importance of Customization for the update cart page in Magento 2 and how it can benefit your online store.

Benefits of implementing a loyalty program for your e-commerce store: As an e-commerce store owner, have you ever considered implementing a loyalty program for your customers? A well-designed loyalty program can help you not only in retaining customers but also in increasing their lifetime value. It encourages them to make repeat purchases, engage more with your brand and refer others to your store. 

Properly implementing a loyalty program can be a powerful tool to boost your online sales, improve customer experience and ultimately grow your business. This article discusses the benefits of implementing a loyalty program for your e-commerce store and how it can help you achieve your business goals.

Magento 2: Max Qty Allowed on Items Customization while updating the cart page

Here We will learn about Max Qty Allowed on Items Customization while updating the cart page.

We have to create a new module. If you still need to create one after, follow the steps described in the post.

 

#1: We need to register the Plugin in Magento 2.

add the following code in “etc/frontend/di.xml” of your module

xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
  <type name="Magento\Checkout\Model\Cart">
      <plugin name="interceptUpdateProductToCart" type="Namespace\Modulename\Plugin\Qtycheck"/>
  </type>
</config>

#2: Add the following code to your plugin file.

Namespace\Modulename\Plugin\Qtycheck.php

php

<?php

namespace Namespace\Modulename\Plugin;

use Magento\Framework\Message\ManagerInterface;

use Magento\Checkout\Model\Session;

use Magento\Checkout\Model\Cart;


class Qtycheck
{

   /**
    * @var ManagerInterface
    */
   protected $_messageManager;

   /**
    * @var \Magento\Quote\Model\Quote
    */
   protected $quote;

   /**
    * Plugin constructor.
    *
    * @param \Magento\Checkout\Model\Session $checkoutSession
    */
   public function __construct(
       Session $checkoutSession,
       ManagerInterface $messageManager
   ) {
       $this->quote = $checkoutSession->getQuote();
       $this->_messageManager = $messageManager;
   }

   /**
    * @param \Magento\Checkout\Model\Cart $subject
    * @param $data
    * @return array
    */
   public function beforeupdateItems(Cart $subject,$data)
   {
       $quote = $subject->getQuote();
       $matchedProductId   = 55;
       $allowedCartQty  = 2;
       foreach($data as $key => $value){
           $item = $quote->getItemById($key);
           $productId= $item->getProductId();
           $itemQty= $value['qty'];

           if($itemQty>$allowedCartQty && $matchedProductId==$productId){
               $this->_messageManager->addNoticeMessage($item->getName().' cannot be ordered in requested quantity.');
               $data[$key]['qty']=1;
           }

       }
       return [$data];

   }
}

#3: Now you need to run the dependency Setup in your console or terminal command.

php bin/magento setup:di:compile

 

#4: Flush the cache in your console or terminal command

php bin/magento cache:clean && bin/magento cache:flush

 

Contact us:+91 8128405131

Email send us at hello@elightwalk.com

Jayram Prajapati
Full Stack Developer

Jayram Prajapati brings expertise and innovation to every project he takes on. His collaborative communication style, coupled with a receptiveness to new ideas, consistently leads to successful project outcomes.

Most Visited Blog

Blog
Everything You Need to Know About Checkout/Cart Page Summary Definition of checkout/cart page

Uncover a thorough grasp of the Checkout/Cart Page summary. Dive into all you need to know about this e-commerce element, learning its definition and importance for a smooth online purchasing experience.

Read More
Blog
How to add customer total order in the column customer grid in Magento 2?

Improve your client management with Magento 2! Learn how to add the order value to customer grid column step by step. Improve visibility and consumer insights for informed and efficient shopping experience.

Read More
Blog
Shipping Bar In Minicart In Magento

Improve client experience in Magento with a dynamic shipping bar into the minicart. Keep informed and increase transparency by presenting shipping data in the minicart for smoother purchase experience.

Read More