I am adding quantity increment/decrement buttons on shopping cart page. How can I reload cart summery (subtotal/shipping rate/grand total/discount/tax section) via ajax ?
I have added below code in app/degign/frontend/Theme Vendor/Theme/Magento_checkout/templates/cart/item/default.phtml
to add +/-
buttons
<div class="control qty">
<button type="button" id="<?= /* @escapeNotVerified */ $_item->getId() ?>-dec" class="decreaseQty"></button>
<input id="cart-<?= /* @escapeNotVerified */ $_item->getId() ?>-qty"
name="cart[<?= /* @escapeNotVerified */ $_item->getId() ?>][qty]"
data-cart-item-id="<?= /* @escapeNotVerified */ $_item->getSku() ?>"
value="<?= /* @escapeNotVerified */ $block->getQty() ?>"
type="number"
size="4"
title="<?= $block->escapeHtml(__('Qty')) ?>"
class="input-text qty"
data-validate="{required:true,'validate-greater-than-zero':true}"
data-role="cart-item-qty"/>
<button type="button" id="<?= /* @escapeNotVerified */ $_item->getId() ?>-upt" class="increaseQty"></button>
</div>
And added below javascript in cart.phtml
<script>
require([
'jquery',
'Magento_Customer/js/customer-data',
'jquery/jquery-storageapi',
'Magento_Checkout/js/action/get-totals'
], function ($, customerData, storageApi, getTotalsAction) {
// $("#submitbutton").hide();
var form = $('form#form-validate');
var qtyfields = $('input.qty');
$('.page.messages').each(function () {
var thismessage = $(this);
thismessage.attr('id', 'messages');
});
form.find(qtyfields).each(function (e) {
var thisfield = $(this);
$(this).change(function () {
console.log('change detected');
form.submit();
});
});
form.on('submit', function (e) {
e.preventDefault();
$.ajax({
url: form.attr('action'),
data: form.serialize(),
type: 'post',
success: function (res) {
//code to reload cart item section
var parsedResponse = $.parseHTML(res);
var result = $(parsedResponse).find("#form-validate");
$("#form-validate").replaceWith(result);
//below two lines refershing cart summary
var deferred = $.Deferred();
getTotalsAction([], deferred);
},
error: function () {
console.log('error');
}
});
console.log('form submitted');
});
});
</script>
But cart summary is not reloading after quantity change.