how to add increment and decrements button on shopping cart quantity block and refresh product price on increment or decrements of price using buttons
Magento – shopping cart page increment and decrement button
qty-incrementquantityshopping-cart
Related Solutions
I have completed by using this code and working perfect.
Find below code from file - template\checkout\cart\item\default.phtml
<input type="text" pattern="\d*" name="cart[<?php echo $_item->getId() ?>][qty]" value="<?php echo $this->getQty() ?>" size="4" data-cart-item-id="<?php echo $this->jsQuoteEscape($_item->getSku()) ?>" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Qty')) ?>" class="input-text qty" maxlength="12" />
Replace it with below code
<a class="decrement_qty_<?php echo $_item->getId(); ?>" href="javascript:void(0)">-</a>
<input type="text" pattern="\d*" name="cart[<?php echo $_item->getId() ?>][qty]" value="<?php echo $this->getQty() ?>" size="4" data-cart-item-id="<?php echo $this->jsQuoteEscape($_item->getSku()) ?>" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Qty')) ?>" class="input-text qty" maxlength="12" />
<a class="increment_qty_<?php echo $_item->getId(); ?>" href="javascript:void(0)">+</a>
Add below code to end of file
<script type="text/javascript">
//<![CDATA[
jQuery(document).ready(function(){
var itemId = '<?php echo $_item->getId(); ?>';
jQuery('.increment_qty_'+itemId).click(function() {
jQuery('.btn-update').css('display','block');
var oldVal = jQuery(this).parent().find("input").val();
if ( parseFloat(oldVal) >= 1 ) {
var newVal = parseFloat(oldVal) + 1;
jQuery(this).parent().find("input").val(newVal);
}
});
jQuery('.decrement_qty_'+itemId).click(function() {
jQuery('.btn-update').css('display','block');
var oldVal = jQuery(this).parent().find("input").val();
if ( parseFloat(oldVal) >= 2 ) {
var newVal = parseFloat(oldVal) - 1;
jQuery(this).parent().find("input").val(newVal);
}
});
});
//]]>
</script>
=> Go to your theme Folder and create file called addtocart.phtml
and
past below code.
Quantity Increment and Decrement Button working fine by using below code
E.g :
app/design/frontend/Vendor/theme/Magento_Catalog/templates/product/view/addtocart.phtml
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
// @codingStandardsIgnoreFile
/** @var $block \Magento\Catalog\Block\Product\View */
?>
<?php $_product = $block->getProduct(); ?>
<?php $buttonTitle = __('Add to Cart'); ?>
<?php if ($_product->isSaleable()): ?>
<div class="box-tocart">
<div class="fieldset">
<?php if ($block->shouldRenderQuantity()): ?>
<div class="field qty">
<label class="label" for="qty"><span><?php /* @escapeNotVerified */ echo __('Qty') ?></span></label>
<?php /*Add Plus Button */ ?>
<input type='button' value='+' class='qtyplus' field='qty' />
<div class="control">
<input type="number"
name="qty"
id="qty"
maxlength="12"
value="<?php /* @escapeNotVerified */ echo $block->getProductDefaultQty() * 1 ?>"
title="<?php /* @escapeNotVerified */ echo __('Qty') ?>" class="input-text qty"
data-validate="<?php echo $block->escapeHtml(json_encode($block->getQuantityValidators())) ?>"
/>
<?php /*Add Minus Button */ ?>
<input type='button' value='-' class='qtyminus' field='qty' />
</div>
</div>
<?php endif; ?>
<div class="actions">
<button type="submit"
title="<?php /* @escapeNotVerified */ echo $buttonTitle ?>"
class="action primary tocart"
id="product-addtocart-button">
<span><?php /* @escapeNotVerified */ echo $buttonTitle ?></span>
</button>
<?php echo $block->getChildHtml('', true) ?>
</div>
</div>
</div>
<?php endif; ?>
<script type="text/x-magento-init">
{
"#product_addtocart_form": {
"Magento_Catalog/product/view/validation": {
"radioCheckboxClosest": ".nested"
}
}
}
</script>
<?php if (!$block->isRedirectToCartEnabled()) : ?>
<script type="text/x-magento-init">
{
"#product_addtocart_form": {
"catalogAddToCart": {
"bindSubmit": false
}
}
}
</script>
<?php endif; ?>
<?php /*JS FOR +-*/ ?>
<script type="text/javascript">
//<![CDATA[
require(['jquery'], function ($) {
setTimeout(function () {
// This button will increment the value
jQuery('.qtyplus').click(function (e) {
// Stop acting like a button
e.preventDefault();
// Get the field name
fieldName = jQuery(this).attr('field');
// Get its current value
var currentVal = parseInt(jQuery('input[name=' + fieldName + ']').val());
// If is not undefined
if (!isNaN(currentVal)) {
// Increment
jQuery('input[name=' + fieldName + ']').val(currentVal + 1);
} else {
// Otherwise put a 0 there
jQuery('input[name=' + fieldName + ']').val(0);
}
});
// This button will decrement the value till 0
jQuery(".qtyminus").click(function (e) {
// Stop acting like a button
e.preventDefault();
// Get the field name
fieldName = jQuery(this).attr('field');
// Get its current value
var currentVal = parseInt(jQuery('input[name=' + fieldName + ']').val());
// If it isn't undefined or its greater than 0
if (!isNaN(currentVal) && currentVal > 0) {
// Decrement one
jQuery('input[name=' + fieldName + ']').val(currentVal - 1);
} else {
// Otherwise put a 0 there
jQuery('input[name=' + fieldName + ']').val(0);
}
});
}, 100);
});
//]]>
</script>
Best Answer
try to use this code:- insert of all we need to add the jQuery code with noConflict. ( have a look here ) Add the following to your custom js files (or create a new one if you don’t have one, aka best practice).
This code ensures that the plus and minus buttons are only present if the user has javascript enabled providing graceful degradation for non-javascript browsers. Here also used input’s rather than paragraph tags for the plus and minus buttons which is a more semantic solution.
You now simply need to wrap the quantity input inside a div with the class “quantity”. Move the file app/design/frontend/base/default/template/catalog/product/view/addtocart.phtml
You should see the code for the input which will look something like:
Thats it friends, and upload the files. You’ll need to style the plus and minus buttons with some CSS if you wish but the functionality will be there.