Skip to content

Commit cd7f421

Browse files
author
Sergey Semenov
committed
MAGETWO-21349: Advanced Mini Cart.
1 parent 54eeaaa commit cd7f421

File tree

6 files changed

+157
-128
lines changed

6 files changed

+157
-128
lines changed

app/code/Magento/Checkout/Controller/Sidebar/RemoveItem.php

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,55 +5,41 @@
55
*/
66
namespace Magento\Checkout\Controller\Sidebar;
77

8-
use Magento\Checkout\Controller\Cart;
9-
use Magento\Checkout\Model\Cart as CustomerCart;
10-
use Magento\Checkout\Model\Session;
118
use Magento\Checkout\Model\Sidebar;
9+
use Magento\Framework\App\Action\Action;
1210
use Magento\Framework\App\Action\Context;
13-
use Magento\Framework\App\Config\ScopeConfigInterface;
1411
use Magento\Framework\App\Response\Http;
15-
use Magento\Framework\Controller\Result\RedirectFactory;
16-
use Magento\Framework\Data\Form\FormKey\Validator;
1712
use Magento\Framework\Exception\LocalizedException;
18-
use Magento\Store\Model\StoreManagerInterface;
13+
use Magento\Framework\Json\Helper\Data;
14+
use Psr\Log\LoggerInterface;
1915

20-
class RemoveItem extends Cart
16+
class RemoveItem extends Action
2117
{
2218
/**
2319
* @var Sidebar
2420
*/
2521
protected $sidebar;
2622

2723
/**
28-
* @param Context $context
29-
* @param ScopeConfigInterface $scopeConfig
30-
* @param Session $checkoutSession
31-
* @param StoreManagerInterface $storeManager
32-
* @param Validator $formKeyValidator
33-
* @param CustomerCart $cart
34-
* @param RedirectFactory $resultRedirectFactory
35-
* @param Sidebar $sidebar
24+
* @var LoggerInterface
3625
*/
26+
protected $logger;
27+
28+
/**
29+
* @var Data
30+
*/
31+
protected $jsonHelper;
32+
3733
public function __construct(
3834
Context $context,
39-
ScopeConfigInterface $scopeConfig,
40-
Session $checkoutSession,
41-
StoreManagerInterface $storeManager,
42-
Validator $formKeyValidator,
43-
CustomerCart $cart,
44-
RedirectFactory $resultRedirectFactory,
45-
Sidebar $sidebar
35+
Sidebar $sidebar,
36+
LoggerInterface $logger,
37+
Data $jsonHelper
4638
) {
4739
$this->sidebar = $sidebar;
48-
parent::__construct(
49-
$context,
50-
$scopeConfig,
51-
$checkoutSession,
52-
$storeManager,
53-
$formKeyValidator,
54-
$cart,
55-
$resultRedirectFactory
56-
);
40+
$this->logger = $logger;
41+
$this->jsonHelper = $jsonHelper;
42+
parent::__construct($context);
5743
}
5844

5945
/**
@@ -69,7 +55,7 @@ public function execute()
6955
} catch (LocalizedException $e) {
7056
return $this->jsonResponse($e->getMessage());
7157
} catch (\Exception $e) {
72-
$this->_objectManager->get('Psr\Log\LoggerInterface')->critical($e);
58+
$this->logger->critical($e);
7359
return $this->jsonResponse($e->getMessage());
7460
}
7561
}
@@ -80,11 +66,17 @@ public function execute()
8066
* @param string $error
8167
* @return Http
8268
*/
83-
public function jsonResponse($error = '')
69+
protected function jsonResponse($error = '')
8470
{
71+
$response = $this->sidebar->getResponseData($error);
72+
if (isset($response['cleanup']) && (bool)$response['cleanup']) {
73+
$this->_view->loadLayout(['default'], true, true, false);
74+
$layout = $this->_view->getLayout();
75+
$block = $layout->getBlock('minicart.content')->toHtml();
76+
$response['content'] = $block;
77+
}
8578
return $this->getResponse()->representJson(
86-
$this->_objectManager->get('Magento\Framework\Json\Helper\Data')
87-
->jsonEncode($this->sidebar->getResponseData($error))
79+
$this->jsonHelper->jsonEncode($response)
8880
);
8981
}
9082
}

app/code/Magento/Checkout/Model/Sidebar.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ public function getResponseData($error = '')
6060
'success' => empty($error) ? true : false,
6161
];
6262
if ($response['success']) {
63+
if (!$this->getSummaryQty()) {
64+
$response['cleanup'] = true;
65+
}
6366
$response = array_merge($response, [
6467
'data' => [
6568
'summary_qty' => $this->getSummaryQty(),

app/code/Magento/Checkout/view/frontend/layout/default.xml

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@
1515
</referenceBlock>
1616
<referenceContainer name="header-wrapper">
1717
<block class="Magento\Checkout\Block\Cart\Sidebar" name="minicart" as="minicart" after="logo" template="cart/minicart.phtml">
18-
<block class="Magento\Framework\View\Element\RendererList" name="checkout.cart.sidebar.item.renderers" as="renderer.list"/>
19-
<container name="minicart.subtotal.container" as="subtotal" label="My Cart Subtotal">
20-
<block name="minicart.subtotal" class="Magento\Checkout\Block\Cart\Sidebar" template="cart/subtotal.phtml"/>
21-
</container>
22-
<container name="minicart.promotion" as="cart_promotion" label="Mini-cart promotion block"/>
23-
<container name="minicart.extra.info" as="minicart_info" label="My Cart Extra info"/>
24-
<container name="topCart.extra_actions" as="extra_actions" label="My Cart Extra Actions">
25-
<block class="Magento\Catalog\Block\ShortcutButtons" name="topCart.shortcut.buttons"/>
26-
</container>
18+
<block class="Magento\Checkout\Block\Cart\Sidebar" name="minicart.content" as="minicart_content" template="cart/minicart/content.phtml">
19+
<block class="Magento\Framework\View\Element\RendererList" name="checkout.cart.sidebar.item.renderers" as="renderer.list"/>
20+
<container name="minicart.subtotal.container" as="subtotal" label="My Cart Subtotal">
21+
<block name="minicart.subtotal" class="Magento\Checkout\Block\Cart\Sidebar" template="cart/subtotal.phtml"/>
22+
</container>
23+
<container name="minicart.promotion" as="cart_promotion" label="Mini-cart promotion block"/>
24+
<container name="minicart.extra.info" as="minicart_info" label="My Cart Extra info"/>
25+
<container name="topCart.extra_actions" as="extra_actions" label="My Cart Extra Actions">
26+
<block class="Magento\Catalog\Block\ShortcutButtons" name="topCart.shortcut.buttons"/>
27+
</container>
28+
</block>
2729
</block>
2830
</referenceContainer>
2931
</body>

app/code/Magento/Checkout/view/frontend/templates/cart/minicart.phtml

Lines changed: 2 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -33,79 +33,8 @@
3333
"triggerClass":"active",
3434
"parentClass":"active",
3535
"buttons":[]}}'>
36-
<div class="block-title">
37-
<strong>
38-
<span class="text"><?php echo __('My Cart'); ?></span>
39-
<span title="<?php echo $block->escapeHtml(__('Items in Cart')); ?>"
40-
class="qty<?php echo($_cartQty > 0) ? '' : ' empty'; ?>"
41-
><?php echo $_cartQty ?></span>
42-
</strong>
43-
</div>
44-
<div class="block-content">
45-
<?php if ($_cartQty || $block->getAllowCartLink()): ?>
46-
47-
<button type="button"
48-
id="btn-minicart-close"
49-
title="<?php echo $block->escapeHtml(__('Close')); ?>"
50-
class="action close">
51-
<span><?php echo __('Close') ?></span>
52-
</button>
53-
54-
<div class="items-total">
55-
<span class="count"><?php echo $_cartQty; ?></span>
56-
<?php echo $block->getSummaryText($_cartQty); ?>
57-
</div>
58-
<?php $isPossibleOnepageCheckout = $_cartQty && $block->isPossibleOnepageCheckout() ?>
59-
<?php if ($isPossibleOnepageCheckout): ?>
60-
<?php echo $block->getChildHtml('subtotal'); ?>
61-
<?php endif; ?>
62-
<?php echo $block->getChildHtml('minicart_info') ?>
63-
<div class="actions">
64-
<div class="primary">
65-
<?php if ($isPossibleOnepageCheckout): ?>
66-
<button
67-
id="top-cart-btn-checkout"
68-
type="button"
69-
class="action primary checkout"
70-
title="<?php echo $block->escapeHtml(__('Go to Checkout')); ?>">
71-
<span><?php echo __('Go to Checkout') ?></span>
72-
</button>
73-
<?php echo $block->getChildHtml('extra_actions') ?>
74-
<?php endif; ?>
75-
</div>
76-
</div>
77-
<?php endif ?>
78-
<?php $_items = $block->getRecentItems() ?>
79-
<?php if (count($_items)): ?>
80-
<strong class="subtitle"><?php echo __('Recently added item(s)') ?></strong>
81-
<div data-action="scroll" class="minicart-items-wrapper">
82-
<ol id="mini-cart" class="minicart-items">
83-
<?php foreach ($_items as $_item): ?>
84-
<?php echo $block->getItemHtml($_item) ?>
85-
<?php endforeach; ?>
86-
</ol>
87-
</div>
88-
<?php else: ?>
89-
<strong class="subtitle empty">
90-
<?php echo __('You have no items in your shopping cart.') ?>
91-
</strong>
92-
<?php if ($block->getCartEmptyMessage()): ?>
93-
<p class="minicart empty text"><?php echo $block->getCartEmptyMessage(); ?></p>
94-
<?php endif; ?>
95-
<?php endif ?>
96-
97-
<?php if ($_cartQty || $block->getAllowCartLink()): ?>
98-
<div class="actions">
99-
<div class="secondary">
100-
<a class="action viewcart" href="<?php echo $block->getUrl('checkout/cart'); ?>">
101-
<span><?php echo __('View and edit cart') ?></span>
102-
</a>
103-
</div>
104-
</div>
105-
<?php endif ?>
106-
<div id="minicart-widgets" class="minicart-widgets">
107-
<?php echo $block->getChildHtml('cart_promotion') ?>
108-
</div>
36+
<div id="minicart-content-wrapper">
37+
<?php echo $block->getChildHtml('minicart_content') ?>
10938
</div>
11039
</div>
11140
<?php endif ?>
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
// @codingStandardsIgnoreFile
8+
9+
/** @var $block \Magento\Checkout\Block\Cart\Sidebar */
10+
?>
11+
12+
<?php $_cartQty = (float) $block->getSummaryCount() ?>
13+
<div class="block-title">
14+
<strong>
15+
<span class="text"><?php echo __('My Cart'); ?></span>
16+
<span title="<?php echo $block->escapeHtml(__('Items in Cart')); ?>"
17+
class="qty<?php echo($_cartQty > 0) ? '' : ' empty'; ?>"
18+
><?php echo $_cartQty ?></span>
19+
</strong>
20+
</div>
21+
<div class="block-content">
22+
<button type="button"
23+
id="btn-minicart-close"
24+
title="<?php echo $block->escapeHtml(__('Close')); ?>"
25+
class="action close">
26+
<span><?php echo __('Close') ?></span>
27+
</button>
28+
29+
<?php if ($_cartQty || $block->getAllowCartLink()): ?>
30+
<div class="items-total">
31+
<span class="count"><?php echo $_cartQty; ?></span>
32+
<?php echo $block->getSummaryText($_cartQty); ?>
33+
</div>
34+
<?php $isPossibleOnepageCheckout = $_cartQty && $block->isPossibleOnepageCheckout() ?>
35+
<?php if ($isPossibleOnepageCheckout): ?>
36+
<?php echo $block->getChildHtml('subtotal'); ?>
37+
<?php endif; ?>
38+
<?php echo $block->getChildHtml('minicart_info') ?>
39+
<div class="actions">
40+
<div class="primary">
41+
<?php if ($isPossibleOnepageCheckout): ?>
42+
<button
43+
id="top-cart-btn-checkout"
44+
type="button"
45+
class="action primary checkout"
46+
title="<?php echo $block->escapeHtml(__('Go to Checkout')); ?>">
47+
<span><?php echo __('Go to Checkout') ?></span>
48+
</button>
49+
<?php echo $block->getChildHtml('extra_actions') ?>
50+
<?php endif; ?>
51+
</div>
52+
</div>
53+
<?php endif ?>
54+
55+
<?php $_items = $block->getRecentItems() ?>
56+
<?php if (count($_items)): ?>
57+
<strong class="subtitle"><?php echo __('Recently added item(s)') ?></strong>
58+
<div data-action="scroll" class="minicart-items-wrapper">
59+
<ol id="mini-cart" class="minicart-items">
60+
<?php foreach ($_items as $_item): ?>
61+
<?php echo $block->getItemHtml($_item) ?>
62+
<?php endforeach; ?>
63+
</ol>
64+
</div>
65+
<?php else: ?>
66+
<strong class="subtitle empty">
67+
<?php echo __('You have no items in your shopping cart.') ?>
68+
</strong>
69+
<?php if ($block->getCartEmptyMessage()): ?>
70+
<p class="minicart empty text"><?php echo $block->getCartEmptyMessage(); ?></p>
71+
<?php endif; ?>
72+
<?php endif ?>
73+
74+
<?php if ($_cartQty || $block->getAllowCartLink()): ?>
75+
<div class="actions">
76+
<div class="secondary">
77+
<a class="action viewcart" href="<?php echo $block->getUrl('checkout/cart'); ?>">
78+
<span><?php echo __('View and edit cart') ?></span>
79+
</a>
80+
</div>
81+
</div>
82+
<?php endif ?>
83+
84+
<div id="minicart-widgets" class="minicart-widgets">
85+
<?php echo $block->getChildHtml('cart_promotion') ?>
86+
</div>
87+
</div>

app/code/Magento/Checkout/view/frontend/web/js/sidebar.js

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ define([
2020
selectorSubtotal: '.block-content > div.subtotal > div.amount span.price',
2121
selectorShowcartNumber: 'a.showcart > span.counter > span.counter-number',
2222
selectorShowcartLabel: 'a.showcart > span.counter > span.counter-label',
23-
selectorList: '#mini-cart'
23+
selectorShowcart: 'a.showcart > span.counter',
24+
selectorList: '#mini-cart',
25+
selectorContentWrapper: '#minicart-content-wrapper'
2426
},
2527
scrollHeight: 0,
2628

@@ -34,12 +36,6 @@ define([
3436
location.href = this.options.checkoutUrl;
3537
}, this));
3638

37-
// Add event on "Close" button click
38-
$(this.options.closeButton).click(function(event) {
39-
event.stopPropagation();
40-
$(self.options.targetElement).dropdownDialog("close");
41-
});
42-
4339
// Add event on "Remove item" click
4440
$(this.options.removeButton).click(function(event) {
4541
event.stopPropagation();
@@ -60,10 +56,24 @@ define([
6056
self._updateQty($(this))
6157
});
6258

59+
this._initCloseButton();
6360
this._calcHeight();
6461
this._isOverflowed();
6562
},
6663

64+
/**
65+
* Add event on "Close" button click
66+
*
67+
* @private
68+
*/
69+
_initCloseButton: function() {
70+
var self = this;
71+
$(this.options.closeButton).click(function(event) {
72+
event.stopPropagation();
73+
$(self.options.targetElement).dropdownDialog("close");
74+
});
75+
},
76+
6777
_isOverflowed: function() {
6878
var list = $(this.options.selectorList);
6979
if (this.scrollHeight > list.innerHeight()) {
@@ -133,9 +143,15 @@ define([
133143
this._refreshSubtotal(response.data.subtotal);
134144
this._refreshShowcartCounter(response.data.summary_qty, response.data.summary_text);
135145
}
136-
elem.closest('li').remove();
137-
this._calcHeight();
138-
this._isOverflowed();
146+
if (response.cleanup === true) {
147+
$(this.options.selectorContentWrapper).replaceWith($.trim(response.content));
148+
$(this.options.selectorShowcart).addClass('empty');
149+
this._initCloseButton();
150+
} else {
151+
elem.closest('li').remove();
152+
this._calcHeight();
153+
this._isOverflowed();
154+
}
139155
},
140156

141157
/**

0 commit comments

Comments
 (0)