define([
'jquery',
'Magento_Checkout/js/view/minicart',
'Magento_Ui/js/modal/alert',
'Magento_Ui/js/modal/confirm'
], function ($ ,Component, alert, confirm) {
'use strict';
return Component.extend({
confirmMessage: $.mage.__('Are you sure you would like to remove all items from the shopping cart?'),
emptyCartUrl: window.checkout.emptyMiniCart,
emptyCartAction: function (element) {
var self = this,
href = self.emptyCartUrl;
$(element).on('click', function () {
var el = this;
confirm({
content: self.confirmMessage,
actions: {
confirm: function () {
self._removeAllItems(href, el);
},
always: function (event) {
event.stopImmediatePropagation();
}
}
});
});
},
_removeAllItems: function (href, elem) {
$.ajax({
url: href,
type: 'post',
dataType: 'json',
beforeSend: function () {
$(elem).attr('disabled', 'disabled');
},
complete: function () {
$(elem).attr('disabled', null);
}
}).done(function (response) {
if (!response.errors) {
} else {
var msg = response.message;
if (msg) {
alert({
content: msg
});
}
}
}).fail(function (error) {
console.log(JSON.stringify(error));
});
}
});
});
emptyCartUrl: window.checkout.emptyMiniCart, we will get the empty link from checkout config: app/code/Vendor/MiniCart/Plugin/Cart/ConfigPlugin.php (declare later).
We need to copy the minicart content from the original file: vendor/magento/module-checkout/view/frontend/web/template/minicart/content.html. And then, in our custom file, we need to add the action text:
Best Answer
I have just created a proper module for your question:
We need to declare the custom js component and html template:
app/code/Vendor/MiniCart/view/frontend/layout/default.xml
app/code/Vendor/MiniCart/view/frontend/web/js/view/minicart.js
emptyCartUrl: window.checkout.emptyMiniCart,
we will get the empty link from checkout config:app/code/Vendor/MiniCart/Plugin/Cart/ConfigPlugin.php
(declare later).We need to copy the minicart content from the original file:
vendor/magento/module-checkout/view/frontend/web/template/minicart/content.html
. And then, in our custom file, we need to add the action text:app/code/Vendor/MiniCart/view/frontend/web/template/minicart/content.html
Declare DI to add the empty link to checkout config:
app/code/Vendor/MiniCart/etc/frontend/di.xml
app/code/Vendor/MiniCart/Plugin/Cart/ConfigPlugin.php
Now, we need to build the controller:
app/code/Vendor/MiniCart/etc/frontend/routes.xml
app/code/Vendor/MiniCart/Controller/Cart/EmptyCart.php
Need to reload cart section:
app/code/Vendor/MiniCart/etc/frontend/sections.xml
Remember to create
registration.php
andmodule.xml
app/code/Vendor/MiniCart/registration.php
app/code/Vendor/MiniCart/etc/module.xml
[IMAGES DEMO]
Mini Cart:
Message Confirmation: