How is the mini-cart AJAX update implemented?

\Magento\Checkout\Block\Cart\Sidebar.php::getUpdateItemQtyUrl()

\Magento\Checkout\Block\Cart\Sidebar.php::getConfig()

minicart.sidebar.url.update

minicart.sidebar.item.button


Magento/Checkout/view/frontend/web/template/minicart/item/default.html


Magento_Checkout/sidebar::_updateItemQty()

Magento_Checkout/sidebar::_ajax()

/**
 * @param {String} url - ajax url
 * @param {Object} data - post data for ajax call
 * @param {Object} elem - element that initiated the event
 * @param {Function} callback - callback method to execute after AJAX success
 */
_ajax: function (url, data, elem, callback) {
	$.extend(data, {
		'form_key': $.mage.cookies.get('form_key')
	});

	$.ajax({
		url: url,
		data: data,
		type: 'post',
		dataType: 'json',
		context: this,
		beforeSend: function () {
			elem.attr('disabled', 'disabled');
		},
		complete: function () {
			elem.attr('disabled', null);
		}
	})
		.done(function (response) {
			if (response.success) {
				callback.call(this, elem, response);
			} else {
				var msg = response.error_message;

				if (msg) {
					alert({
						content: $.mage.__(msg)
					});
				}
			}
		})
		.fail(function (error) {
			console.log(JSON.stringify(error));
		});
},

\Magento\Checkout\Controller\Sidebar\UpdateItemQty::execute()

1 Like

A post was split to a new topic: Is it refresh right side total block also?