Skip to content

Commit

Permalink
magento#108: Clear Shopping Cart - Implemented Clear Shopping Cart bu…
Browse files Browse the repository at this point in the history
…tton display configuration with modal confirm widget
  • Loading branch information
John Carlo Octabio committed Jun 5, 2020
1 parent f571e90 commit 53a22be
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 19 deletions.
18 changes: 18 additions & 0 deletions app/code/Magento/Checkout/Block/Cart/Grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class Grid extends \Magento\Checkout\Block\Cart
*/
const XPATH_CONFIG_NUMBER_ITEMS_TO_DISPLAY_PAGER = 'checkout/cart/number_items_to_display_pager';

/**
* Default display setting for clear shopping cart button
*/
const XPATH_CONFIG_ENABLE_CLEAR_SHOPPING_CART = 'checkout/cart/enable_clear_shopping_cart';

/**
* @var \Magento\Quote\Model\ResourceModel\Quote\Item\Collection
*/
Expand Down Expand Up @@ -174,4 +179,17 @@ private function isPagerDisplayedOnPage()
}
return $this->isPagerDisplayed;
}

/**
* Check if clear shopping cart button is enabled
*
* @return bool
*/
public function isClearShoppingCartEnabled()
{
return (bool) $this->_scopeConfig->getValue(
self::XPATH_CONFIG_ENABLE_CLEAR_SHOPPING_CART,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
}
}
4 changes: 4 additions & 0 deletions app/code/Magento/Checkout/etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
<label>Show Cross-sell Items in the Shopping Cart</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="enable_clear_shopping_cart" translate="label" type="select" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Enable Clear Shopping Cart</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
</group>
<group id="cart_link" translate="label" sortOrder="3" showInDefault="1" showInWebsite="1">
<label>My Cart Link</label>
Expand Down
1 change: 1 addition & 0 deletions app/code/Magento/Checkout/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<redirect_to_cart>0</redirect_to_cart>
<number_items_to_display_pager>20</number_items_to_display_pager>
<crosssell_enabled>1</crosssell_enabled>
<enable_clear_shopping_cart>0</enable_clear_shopping_cart>
</cart>
<cart_link>
<use_qty>1</use_qty>
Expand Down
2 changes: 2 additions & 0 deletions app/code/Magento/Checkout/i18n/en_US.csv
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ Shipping,Shipping
"Maximum Number of Items to Display in Order Summary","Maximum Number of Items to Display in Order Summary"
"Quote Lifetime (days)","Quote Lifetime (days)"
"After Adding a Product Redirect to Shopping Cart","After Adding a Product Redirect to Shopping Cart"
"Enable Clear Shopping Cart","Enable Clear Shopping Cart"
"Are you sure you want to remove all items from your Shopping Cart?","Are you sure you want to remove all items from your Shopping Cart?"
"Number of Items to Display Pager","Number of Items to Display Pager"
"My Cart Link","My Cart Link"
"Display Cart Summary","Display Cart Summary"
Expand Down
21 changes: 13 additions & 8 deletions app/code/Magento/Checkout/view/frontend/templates/cart/form.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,16 @@
<span><?= $block->escapeHtml(__('Continue Shopping')) ?></span>
</a>
<?php endif; ?>
<button type="button"
name="update_cart_action"
data-cart-empty=""
value="empty_cart"
title="<?= $block->escapeHtml(__('Clear Shopping Cart')) ?>"
class="action clear" id="empty_cart_button">
<span><?= $block->escapeHtml(__('Clear Shopping Cart')) ?></span>
</button>
<?php if ($block->isClearShoppingCartEnabled()) :?>
<button type="button"
name="update_cart_action"
data-cart-empty=""
value="empty_cart"
title="<?= $block->escapeHtml(__('Clear Shopping Cart')) ?>"
class="action clear" id="empty_cart_button">
<span><?= $block->escapeHtml(__('Clear Shopping Cart')) ?></span>
</button>
<?php endif ?>
<button type="submit"
name="update_cart_action"
data-cart-item-update=""
Expand All @@ -78,3 +80,6 @@
<?= $block->getChildHtml('checkout.cart.order.actions') ?>
<?= $block->getChildHtml('shopping.cart.table.after') ?>

<script>

</script>
27 changes: 18 additions & 9 deletions app/code/Magento/Checkout/view/frontend/web/js/shopping-cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,32 @@

define([
'jquery',
'jquery-ui-modules/widget'
], function ($) {
'Magento_Ui/js/modal/confirm',
'jquery-ui-modules/widget',
'mage/translate'
], function ($, confirm) {
'use strict';

$.widget('mage.shoppingCart', {
/** @inheritdoc */
_create: function () {
var items, i, reload;
var items, i, reload, self = this;

$(this.options.emptyCartButton).on('click', $.proxy(function () {
$(this.options.emptyCartButton).attr('name', 'update_cart_action_temp');
$(this.options.updateCartActionContainer)
.attr('name', 'update_cart_action').attr('value', 'empty_cart');
confirm({
content: $.mage.__('Are you sure you want to remove all items from your Shopping Cart?'),
actions: {
confirm: function () {
$(self.options.emptyCartButton).attr('name', 'update_cart_action_temp');
$(self.options.updateCartActionContainer)
.attr('name', 'update_cart_action').attr('value', 'empty_cart');

if ($(this.options.emptyCartButton).parents('form').length > 0) {
$(this.options.emptyCartButton).parents('form').submit();
}
if ($(self.options.emptyCartButton).parents('form').length > 0) {
$(self.options.emptyCartButton).parents('form').submit();
}
}
}
});
}, this));
items = $.find('[data-role="cart-item-qty"]');

Expand Down
10 changes: 8 additions & 2 deletions app/design/frontend/Magento/luma/web/css/source/_extends.less
Original file line number Diff line number Diff line change
Expand Up @@ -1570,10 +1570,16 @@
margin-bottom: @indent__base;

.actions.main {
.continue,
.clear {
.continue {
display: none;
}

.clear {
.lib-button-as-link(
@_margin: 0 @indent__base 0 0
);
font-weight: @font-weight__regular;
}
}
}
}
Expand Down

0 comments on commit 53a22be

Please sign in to comment.