{"id":954,"date":"2020-04-18T08:41:00","date_gmt":"2020-04-18T08:41:00","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2020\/04\/18\/add-quantity-increment-and-decrement-button-in-magento-2\/"},"modified":"2025-05-22T13:29:40","modified_gmt":"2025-05-22T07:59:40","slug":"add-quantity-increment-and-decrement-button-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/add-quantity-increment-and-decrement-button-in-magento-2\/","title":{"rendered":"How To Add Quantity Increment And Decrement Button In Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The E-commerce store owners try to make the online shopping platform as convenient as possible to shop and make it preferable over brick and mortar stores. As a part of this aim, features like size charts, easy returns, quantity change buttons, WhatsApp sharing buttons, order tracking, and many more are implemented on the product page.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Today I am posting one such solution to&nbsp;<em><strong>add quantity increment and decrement button in Magento 2<\/strong><\/em>.&nbsp; It allows the customers to increase or decrease the quantity of products in the cart on the product page.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The default Magento 2 allows a simple text button to add the number of products to be purchased. However, the increment and decrement buttons on product page make it easier and quicker.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The below code is implemented with the Knockout JS as it is lightweight:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to add quantity increment and decrement button in Magento 2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<em><strong>requirejs-config.js<\/strong><\/em>&nbsp;in&nbsp;<strong>app\/design\/frontend\/[NameSpace]\/[theme]\/Magento_Catalog\/<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Add the below code:<\/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=\"\">var config = {\n    map: {\n        '*': {\n            'qty-counter': 'Magento_Catalog\/js\/qty-counter'\n        }\n    }\n};<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Copy file from&nbsp;<strong>vendor\/magento\/module-catalog\/view\/frontend\/templates\/product\/view\/addtocart.phtml<\/strong><br>to current theme&nbsp;<strong>app\/design\/frontend\/[NameSpace]\/[theme]\/Magento_Catalog\/templates\/product\/view\/<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Change code:<\/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;div class=\"box-tocart\">\n    &lt;div class=\"fieldset\">\n        &lt;?php if ($block->shouldRenderQuantity()): ?>\n        &lt;div class=\"field qty\">\n            &lt;label class=\"label\" for=\"qty\">&lt;span>&lt;?php \/* @escapeNotVerified *\/ echo __('Quantity:') ?>&lt;\/span>&lt;\/label>\n            &lt;div id=\"custom-qty\" class=\"control\" data-bind=\"scope: 'qty-counter'\">\n                &lt;!-- ko template: getTemplate() -->&lt;!-- \/ko -->\n                &lt;script type=\"text\/x-magento-init\">\n                    {\n                        \"#custom-qty\": {\n                            \"Magento_Ui\/js\/core\/app\": {\n                                \"components\": {\n                                    \"qty-counter\": {\n                                        \"component\": \"qty-counter\",\n                                        \"config\": {\n                                            \"qty\": &lt;?php echo $block->getProductDefaultQty() * 1 ?>,\n                                            \"dataValidate\": &lt;?php echo json_encode($block->getQuantityValidators()) ?>\n                                        }\n                                    }\n                                 }\n                            }\n                        }\n                    }\n                &lt;\/script>\n            &lt;\/div>\n        &lt;\/div>\n        &lt;?php endif; ?>\n        &lt;div class=\"actions\">\n            &lt;button type=\"submit\"\n                    title=\"&lt;?php \/* @escapeNotVerified *\/ echo $buttonTitle ?>\"\n                    class=\"action primary tocart\"\n                    id=\"product-addtocart-button\">\n                &lt;span>&lt;?php \/* @escapeNotVerified *\/ echo $buttonTitle ?>&lt;\/span>\n            &lt;\/button>\n            &lt;?php echo $block->getChildHtml('', true) ?>\n        &lt;\/div>\n    &lt;\/div>\n&lt;\/div><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<em><strong>qty-counter.js<\/strong><\/em>&nbsp;in&nbsp;<strong>app\/design\/frontend\/[NameSpace]\/[theme]\/Magento_Catalog\/web\/js<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Add the below code:<\/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=\"\">'use strict';\n\ndefine([\n    'ko',\n    'uiElement'\n], function (ko, Element) {\n    return Element.extend({\n        defaults: {\n            template: 'Magento_Catalog\/input-counter'\n        },\n\n        initObservable: function () {\n            this._super()\n                .observe('qty');\n\n            return this;\n        },\n\n        getDataValidator: function() {\n            return JSON.stringify(this.dataValidate);\n        },\n\n        decreaseQty: function() {\n            var qty;\n\n            if (this.qty() > 1) {\n                qty = this.qty() - 1;\n            } else {\n                qty = 1;\n            }\n\n            this.qty(qty);\n        },\n\n        increaseQty: function() {\n            var qty = this.qty() + 1;\n\n            this.qty(qty);\n        }\n    });\n});<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<em><strong>input-counter.html<\/strong><\/em>&nbsp;in&nbsp;<strong>app\/design\/frontend\/[NameSpace]\/[theme]\/Magento_Catalog\/web\/template<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Add the below code:<\/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;div class=\"input-group\">\n    &lt;span class=\"input-group__addon\">\n        &lt;button click=\"decreaseQty\" class=\"input-group__button input-group__button--decrease\">\n            &lt;span class=\"input-group__icon input-group__icon--decrease\">&lt;\/span>\n        &lt;\/button>\n    &lt;\/span>\n    &lt;input data-bind=\"value: qty(), attr: {'data-validate': getDataValidator(), 'title': $t('Qty')}\"\n           type=\"number\"\n           name=\"qty\"\n           id=\"qty\"\n           maxlength=\"12\"\n           class=\"input-group__input\" \/>\n    &lt;span class=\"input-group__addon\">\n        &lt;button click=\"increaseQty\" class=\"input-group__button input-group__button--increase\">\n            &lt;span class=\"input-group__icon input-group__icon--increase\">&lt;\/span>\n        &lt;\/button>\n    &lt;\/span>\n&lt;\/div><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it to add the increment and decrement button on the product page in Magento 2.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Please share the solution with fellow developers via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The E-commerce store owners try to make the online shopping platform as convenient as possible to shop and make it preferable over brick and mortar&#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-954","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/954","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=954"}],"version-history":[{"count":2,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/954\/revisions"}],"predecessor-version":[{"id":14960,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/954\/revisions\/14960"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=954"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=954"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=954"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}