Skip to content

Commit 8ce0b8d

Browse files
committed
Merge pull request #81 from magento-mpi/MAGETWO-33035
[MPI] Multi-tenant mode and DI configuration optimization
2 parents baadf05 + e456fc6 commit 8ce0b8d

File tree

69 files changed

+2010
-642
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+2010
-642
lines changed

app/code/Magento/Catalog/Block/Product/AbstractProduct.php

+13
Original file line numberDiff line numberDiff line change
@@ -585,4 +585,17 @@ public function getProductPriceHtml(
585585
}
586586
return $price;
587587
}
588+
589+
/**
590+
* Whether redirect to cart enabled
591+
*
592+
* @return bool
593+
*/
594+
public function isRedirectToCartEnabled()
595+
{
596+
return $this->_scopeConfig->getValue(
597+
'checkout/cart/redirect_to_cart',
598+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
599+
);
600+
}
588601
}

app/code/Magento/Catalog/Block/Product/ListProduct.php

+7-4
Original file line numberDiff line numberDiff line change
@@ -329,11 +329,14 @@ public function getIdentities()
329329
public function getAddToCartPostParams(\Magento\Catalog\Model\Product $product)
330330
{
331331
$url = $this->getAddToCartUrl($product);
332-
$data = [
333-
'product' => $product->getEntityId(),
334-
\Magento\Framework\App\Action\Action::PARAM_NAME_URL_ENCODED => $this->_postDataHelper->getEncodedUrl($url),
332+
return [
333+
'action' => $url,
334+
'data' => [
335+
'product' => $product->getEntityId(),
336+
\Magento\Framework\App\Action\Action::PARAM_NAME_URL_ENCODED =>
337+
$this->_postDataHelper->getEncodedUrl($url),
338+
]
335339
];
336-
return $this->_postDataHelper->getPostData($url, $data);
337340
}
338341

339342
/**

app/code/Magento/Catalog/Controller/Product/View.php

+8
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ public function execute()
9393
$notice = $product->getTypeInstance()->getSpecifyOptionMessage();
9494
$this->messageManager->addNotice($notice);
9595
}
96+
if ($this->getRequest()->isAjax()) {
97+
$this->getResponse()->representJson(
98+
$this->_objectManager->get('Magento\Core\Helper\Data')->jsonEncode([
99+
'backUrl' => $this->_redirect->getRedirectUrl()
100+
])
101+
);
102+
return;
103+
}
96104
$resultRedirect = $this->resultRedirectFactory->create();
97105
$resultRedirect->setRefererOrBaseUrl();
98106
return $resultRedirect;

app/code/Magento/Catalog/view/frontend/requirejs-config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ var config = {
1717
priceOptionDate: 'Magento_Catalog/js/price-option-date',
1818
priceOptionFile: 'Magento_Catalog/js/price-option-file',
1919
priceOptions: 'Magento_Catalog/js/price-options',
20-
priceUtils: 'Magento_Catalog/js/price-utils'
20+
priceUtils: 'Magento_Catalog/js/price-utils',
21+
catalogAddToCart: 'Magento_Catalog/js/catalog-add-to-cart'
2122
}
2223
}
2324
};

app/code/Magento/Catalog/view/frontend/templates/product/compare/list.phtml

+16-4
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,11 @@
6464
<div class="product-item-actions">
6565
<div class="actions-primary">
6666
<?php if ($_item->isSaleable()): ?>
67-
<button type="button" class="action tocart primary"
68-
data-url="<?php echo $this->helper('Magento\Catalog\Helper\Product\Compare')->getAddToCartUrl($_item) ?>">
69-
<span><?php echo __('Add to Cart') ?></span>
70-
</button>
67+
<form data-role="tocart-form" action="<?php echo $this->helper('Magento\Catalog\Helper\Product\Compare')->getAddToCartUrl($_item); ?>" method="post">
68+
<button type="submit" class="action tocart primary">
69+
<span><?php echo __('Add to Cart'); ?></span>
70+
</button>
71+
</form>
7172
<?php else: ?>
7273
<?php if ($_item->getIsSalable()): ?>
7374
<div class="stock available"><span><?php echo __('In stock') ?></span></div>
@@ -129,6 +130,17 @@
129130
</tbody>
130131
</table>
131132
</div>
133+
<?php if (!$block->isRedirectToCartEnabled()) : ?>
134+
<script>
135+
require([
136+
'jquery',
137+
'domReady!',
138+
'Magento_Catalog/js/catalog-add-to-cart'
139+
], function($) {
140+
$('[data-role=tocart-form]').catalogAddToCart();
141+
});
142+
</script>
143+
<?php endif; ?>
132144
<?php else: ?>
133145
<div class="message info empty"><div><?php echo __('You have no items to compare.') ?></div></div>
134146
<?php endif; ?>

app/code/Magento/Catalog/view/frontend/templates/product/list.phtml

+19-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © 2015 Magento. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
use Magento\Framework\App\Action\Action;
67

78
// @codingStandardsIgnoreFile
89

@@ -70,10 +71,14 @@ $imageBlock = $block->getLayout()->createBlock('Magento\Catalog\Block\Product\I
7071
<div class="product actions product-item-actions"<?php echo strpos($pos, $viewMode . '-actions') ? $position : ''; ?>>
7172
<div class="actions-primary"<?php echo strpos($pos, $viewMode . '-primary') ? $position : ''; ?>>
7273
<?php if ($_product->isSaleable()): ?>
73-
<button type="button" title="<?php echo __('Add to Cart') ?>" class="action tocart primary"
74-
data-post='<?php echo $block->getAddToCartPostParams($_product); ?>'>
75-
<span><?php echo __('Add to Cart') ?></span>
76-
</button>
74+
<?php $postParams = $block->getAddToCartPostParams($_product); ?>
75+
<form data-role="tocart-form" action="<?php echo $postParams['action']; ?>" method="post">
76+
<input type="hidden" name="product" value="<?php echo $postParams['data']['product']; ?>">
77+
<input type="hidden" name="<?php echo Action::PARAM_NAME_URL_ENCODED; ?>" value="<?php echo $postParams['data'][Action::PARAM_NAME_URL_ENCODED]; ?>">
78+
<button type="submit" title="<?php echo __('Add to Cart') ?>" class="action tocart primary">
79+
<span><?php echo __('Add to Cart') ?></span>
80+
</button>
81+
</form>
7782
<?php else: ?>
7883
<?php if ($_product->getIsSalable()): ?>
7984
<div class="stock available"><span><?php echo __('In stock') ?></span></div>
@@ -114,4 +119,14 @@ $imageBlock = $block->getLayout()->createBlock('Magento\Catalog\Block\Product\I
114119
</ol>
115120
</div>
116121
<?php echo $block->getToolbarHtml() ?>
122+
<?php if (!$block->isRedirectToCartEnabled()) : ?>
123+
<script>
124+
require([
125+
'jquery',
126+
'Magento_Catalog/js/catalog-add-to-cart'
127+
], function($) {
128+
$('[data-role=tocart-form],.form.map.checkout').catalogAddToCart();
129+
});
130+
</script>
131+
<?php endif; ?>
117132
<?php endif; ?>

app/code/Magento/Catalog/view/frontend/templates/product/view/addtocart.phtml

+27-10
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,32 @@
4343
</fieldset>
4444
</div>
4545
<?php endif; ?>
46-
4746
<script>
48-
require([
49-
"jquery",
50-
"mage/mage",
51-
"Magento_Catalog/product/view/validation"
52-
], function($){
53-
$('#product_addtocart_form').mage('validation', {
54-
radioCheckboxClosest: '.nested'
55-
});
56-
});
47+
<?php if ($block->isRedirectToCartEnabled()) : ?>
48+
require([
49+
"jquery",
50+
"mage/mage",
51+
"Magento_Catalog/product/view/validation"
52+
], function($){
53+
$('#product_addtocart_form').mage('validation', {
54+
radioCheckboxClosest: '.nested'
55+
});
56+
});
57+
<?php else : ?>
58+
require([
59+
"jquery",
60+
"mage/mage",
61+
"Magento_Catalog/product/view/validation",
62+
"Magento_Catalog/js/catalog-add-to-cart"
63+
], function($){
64+
$('#product_addtocart_form').mage('validation', {
65+
radioCheckboxClosest: '.nested',
66+
submitHandler: function(form) {
67+
var widget = $(form).catalogAddToCart({bindSubmit: false});
68+
widget.catalogAddToCart('submitForm', $(form));
69+
return false;
70+
}
71+
});
72+
});
73+
<?php endif; ?>
5774
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* Copyright © 2015 Magento. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
define([
6+
'jquery',
7+
'jquery/ui'
8+
], function($) {
9+
"use strict";
10+
11+
$.widget('mage.catalogAddToCart', {
12+
13+
options: {
14+
processStart: null,
15+
processStop: null,
16+
bindSubmit: true,
17+
minicartSelector: '[data-block="minicart"]',
18+
messagesSelector: '[data-placeholder="messages"]',
19+
productStatusSelector: '.stock.available'
20+
},
21+
22+
_create: function() {
23+
if (this.options.bindSubmit) {
24+
this._bindSubmit();
25+
}
26+
},
27+
28+
_bindSubmit: function() {
29+
var self = this;
30+
this.element.on('submit', function(e) {
31+
e.preventDefault();
32+
self.submitForm($(this));
33+
});
34+
},
35+
36+
isLoaderEnabled: function() {
37+
return this.options.processStart && this.options.processStop;
38+
},
39+
40+
submitForm: function(form) {
41+
var self = this;
42+
$.ajax({
43+
url: form.attr('action'),
44+
data: form.serialize(),
45+
type: 'post',
46+
dataType: 'json',
47+
beforeSend: function() {
48+
if (self.isLoaderEnabled()) {
49+
$('body').trigger(self.options.processStart);
50+
}
51+
},
52+
success: function(res) {
53+
if (self.isLoaderEnabled()) {
54+
$('body').trigger(self.options.processStop);
55+
}
56+
57+
if (res.backUrl) {
58+
window.location = res.backUrl;
59+
return;
60+
}
61+
if (res.messages) {
62+
$(self.options.messagesSelector).html(res.messages);
63+
}
64+
if (res.minicart) {
65+
$(self.options.minicartSelector).replaceWith(res.minicart);
66+
}
67+
if (res.product && res.product.statusText) {
68+
$(self.options.productStatusSelector)
69+
.removeClass('available')
70+
.addClass('unavailable')
71+
.find('span')
72+
.html(res.product.statusText);
73+
}
74+
}
75+
});
76+
}
77+
});
78+
79+
return $.mage.catalogAddToCart;
80+
});

app/code/Magento/Catalog/view/frontend/web/js/list.js

-8
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,6 @@ define([
4242
e.preventDefault();
4343
window.print();
4444
});
45-
46-
$.each(this.options.selectors, function(i, selector) {
47-
$(selector).on('click', function(e) {
48-
e.preventDefault();
49-
window.location.href = $(this).data('url');
50-
});
51-
});
52-
5345
}
5446
});
5547

app/code/Magento/Catalog/view/frontend/web/product/view/validation.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
options: {
2020
radioCheckboxClosest: 'ul, ol',
2121
errorPlacement: function (error, element) {
22-
if (element.attr('data-validate-message-box')) {
23-
var messageBox = $(element.attr('data-validate-message-box'));
22+
if (element.attr('data-errors-message-box')) {
23+
var messageBox = $(element.attr('data-errors-message-box'));
2424
messageBox.html(error);
2525
return;
2626
}

app/code/Magento/Checkout/Controller/Cart.php

+36-14
Original file line numberDiff line numberDiff line change
@@ -74,26 +74,18 @@ public function __construct(
7474
/**
7575
* Set back redirect url to response
7676
*
77+
* @param null|string $backUrl
78+
*
7779
* @return \Magento\Framework\Controller\Result\Redirect
7880
*/
79-
protected function _goBack()
81+
protected function _goBack($backUrl = null)
8082
{
81-
$returnUrl = $this->getRequest()->getParam('return_url');
8283
$resultRedirect = $this->resultRedirectFactory->create();
83-
if ($returnUrl && $this->_isInternalUrl($returnUrl)) {
84-
$this->messageManager->getMessages()->clear();
85-
$resultRedirect->setUrl($returnUrl);
86-
} elseif (!$this->_scopeConfig->getValue('checkout/cart/redirect_to_cart', ScopeInterface::SCOPE_STORE)
87-
&& !$this->getRequest()->getParam('in_cart')
88-
&& ($backUrl = $this->_redirect->getRefererUrl())
89-
) {
84+
85+
if ($backUrl || $backUrl = $this->getBackUrl($this->_redirect->getRefererUrl())) {
9086
$resultRedirect->setUrl($backUrl);
91-
} else {
92-
if ($this->getRequest()->getActionName() == 'add' && !$this->getRequest()->getParam('in_cart')) {
93-
$this->_checkoutSession->setContinueShoppingUrl($this->_redirect->getRefererUrl());
94-
}
95-
$resultRedirect->setPath('checkout/cart');
9687
}
88+
9789
return $resultRedirect;
9890
}
9991

@@ -118,4 +110,34 @@ protected function _isInternalUrl($url)
118110
$secure = strpos($url, $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_LINK, true)) === 0;
119111
return $unsecure || $secure;
120112
}
113+
114+
/**
115+
* Get resolved back url
116+
*
117+
* @param null $defaultUrl
118+
*
119+
* @return mixed|null|string
120+
*/
121+
protected function getBackUrl($defaultUrl = null)
122+
{
123+
$returnUrl = $this->getRequest()->getParam('return_url');
124+
if ($returnUrl && $this->_isInternalUrl($returnUrl)) {
125+
$this->messageManager->getMessages()->clear();
126+
return $returnUrl;
127+
}
128+
129+
$shouldRedirectToCart = $this->_scopeConfig->getValue(
130+
'checkout/cart/redirect_to_cart',
131+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
132+
);
133+
134+
if ($shouldRedirectToCart || $this->getRequest()->getParam('in_cart')) {
135+
if ($this->getRequest()->getActionName() == 'add' && !$this->getRequest()->getParam('in_cart')) {
136+
$this->_checkoutSession->setContinueShoppingUrl($this->_redirect->getRefererUrl());
137+
}
138+
return $this->_url->getUrl('checkout/cart');
139+
}
140+
141+
return $defaultUrl;
142+
}
121143
}

0 commit comments

Comments
 (0)