{"id":510,"date":"2019-07-17T04:47:35","date_gmt":"2019-07-17T04:47:35","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2019\/07\/17\/restrict-quantity-update-from-mini-cart-in-magento-2\/"},"modified":"2025-05-22T16:17:23","modified_gmt":"2025-05-22T10:47:23","slug":"restrict-quantity-update-from-mini-cart-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/restrict-quantity-update-from-mini-cart-in-magento-2\/","title":{"rendered":"How to Restrict Quantity Update From Mini Cart in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Ever had the requirement in your Magento 2 store of restricting a customer on purchase quantity? I\u2019m sure you had if you were concerned about cost-effective packaging,&nbsp;delivery, shipping, and stock management.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Today, I\u2019ve come up with a method to&nbsp;<em><strong>restrict quantity update from mini cart in Magento 2<\/strong><\/em>. This is helpful when you have set a condition of limiting the order quantity. You can restrict customers to update the quantity in mini cart that violates the limit, and shows a message in the popup with the related restriction information.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Restrict Quantity Update From Mini Cart In Magento 2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Create&nbsp;<strong>di.xml<\/strong>&nbsp;file at&nbsp;<em><strong><em><strong>app\\code\\Vendor\\Extension\\etc\\frontend<\/strong><\/em><\/strong><\/em><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"urn:magento:framework:ObjectManager\/etc\/config.xsd\">\n    &lt;type name=\"Magento\\Checkout\\Controller\\Sidebar\\UpdateItemQty\">\n        &lt;plugin name=\"quantity_update\" type=\"Vendor\\Extension\\Observer\\UpdateItemQty\" sortOrder=\"10\"\/>\n    &lt;\/type>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Create&nbsp;<strong>UpdateItemQty.php<\/strong>&nbsp;file at&nbsp;<em><strong><em><strong>app\\code\\Vendor\\Extension\\Observer<\/strong><\/em><\/strong><\/em><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Vendor\\Extension\\Observer;\n\nuse Magento\\Checkout\\Controller\\Sidebar\\UpdateItemQty as coreUpdateItemQty;\nuse Vendor\\Extension\\Helper\\Data;\nuse Magento\\Framework\\Json\\Helper\\Data as coreData;\nuse Magento\\Checkout\\Model\\Sidebar;\nuse Magento\\Catalog\\Model\\ProductFactory;\nuse Magento\\Checkout\\Model\\Cart;\nuse Magento\\Framework\\Serialize\\SerializerInterface;\n\n\nclass UpdateItemQty\n{\n    protected $helper;\n    protected $jsonHelper;\n    protected $sidebar;\n    protected $quoteItemFactory;\n    protected $productFactory;\n    protected $cart;\n    protected $serializer;\n\n\n    public function __construct(\n        Data $helper,\n        coreData $jsonHelper,\n        Sidebar $sidebar,\n        Cart $cart,\n        SerializerInterface $serializer,\n        ProductFactory $productFactory\n    )\n    {\n        $this->helper = $helper;\n        $this->jsonHelper = $jsonHelper;\n        $this->sidebar = $sidebar;\n        $this->productFactory = $productFactory;\n        $this->serializer = $serializer;\n        $this->cart = $cart;\n    }\n\n    public function aroundExecute(coreUpdateItemQty $subject, \\Closure $proceed)\n    {\n\n        try {\n\n            if(your condition){\n                $errorMsg= 'Error Msg';\n                return $subject->getResponse()->representJson(\n                    $this->jsonHelper->jsonEncode($this->sidebar->getResponseData($errorMsg))\n                );\n            }\n        } catch (\\Exception $e) {\n            return $subject->getResponse()->representJson(\n                $this->jsonHelper->jsonEncode($this->sidebar->getResponseData($e->getMessage()))\n            );\n        }\n        return $proceed();\n\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it. You can also&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/show-additional-data-in-magento-2-mini-cart\/\">show additional data in Magento 2 mini cart<\/a>&nbsp;to show shipping charge, discount or tax information etc.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Limit the quantity or set any conditions for updating quantity right in the mini cart with the above solution!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ever had the requirement in your Magento 2 store of restricting a customer on purchase quantity? I\u2019m sure you had if you were concerned about&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-510","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/510","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=510"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/510\/revisions"}],"predecessor-version":[{"id":15340,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/510\/revisions\/15340"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=510"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=510"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=510"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}