Skip to content

Commit

Permalink
Merge pull request #81 from magento-mpi/MAGETWO-33035
Browse files Browse the repository at this point in the history
[MPI] Multi-tenant mode and DI configuration optimization
  • Loading branch information
vpelipenko committed Feb 16, 2015
2 parents baadf05 + e456fc6 commit 8ce0b8d
Show file tree
Hide file tree
Showing 69 changed files with 2,010 additions and 642 deletions.
13 changes: 13 additions & 0 deletions app/code/Magento/Catalog/Block/Product/AbstractProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -585,4 +585,17 @@ public function getProductPriceHtml(
}
return $price;
}

/**
* Whether redirect to cart enabled
*
* @return bool
*/
public function isRedirectToCartEnabled()
{
return $this->_scopeConfig->getValue(
'checkout/cart/redirect_to_cart',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
}
}
11 changes: 7 additions & 4 deletions app/code/Magento/Catalog/Block/Product/ListProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,11 +329,14 @@ public function getIdentities()
public function getAddToCartPostParams(\Magento\Catalog\Model\Product $product)
{
$url = $this->getAddToCartUrl($product);
$data = [
'product' => $product->getEntityId(),
\Magento\Framework\App\Action\Action::PARAM_NAME_URL_ENCODED => $this->_postDataHelper->getEncodedUrl($url),
return [
'action' => $url,
'data' => [
'product' => $product->getEntityId(),
\Magento\Framework\App\Action\Action::PARAM_NAME_URL_ENCODED =>
$this->_postDataHelper->getEncodedUrl($url),
]
];
return $this->_postDataHelper->getPostData($url, $data);
}

/**
Expand Down
8 changes: 8 additions & 0 deletions app/code/Magento/Catalog/Controller/Product/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ public function execute()
$notice = $product->getTypeInstance()->getSpecifyOptionMessage();
$this->messageManager->addNotice($notice);
}
if ($this->getRequest()->isAjax()) {
$this->getResponse()->representJson(
$this->_objectManager->get('Magento\Core\Helper\Data')->jsonEncode([
'backUrl' => $this->_redirect->getRedirectUrl()
])
);
return;
}
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setRefererOrBaseUrl();
return $resultRedirect;
Expand Down
3 changes: 2 additions & 1 deletion app/code/Magento/Catalog/view/frontend/requirejs-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ var config = {
priceOptionDate: 'Magento_Catalog/js/price-option-date',
priceOptionFile: 'Magento_Catalog/js/price-option-file',
priceOptions: 'Magento_Catalog/js/price-options',
priceUtils: 'Magento_Catalog/js/price-utils'
priceUtils: 'Magento_Catalog/js/price-utils',
catalogAddToCart: 'Magento_Catalog/js/catalog-add-to-cart'
}
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@
<div class="product-item-actions">
<div class="actions-primary">
<?php if ($_item->isSaleable()): ?>
<button type="button" class="action tocart primary"
data-url="<?php echo $this->helper('Magento\Catalog\Helper\Product\Compare')->getAddToCartUrl($_item) ?>">
<span><?php echo __('Add to Cart') ?></span>
</button>
<form data-role="tocart-form" action="<?php echo $this->helper('Magento\Catalog\Helper\Product\Compare')->getAddToCartUrl($_item); ?>" method="post">
<button type="submit" class="action tocart primary">
<span><?php echo __('Add to Cart'); ?></span>
</button>
</form>
<?php else: ?>
<?php if ($_item->getIsSalable()): ?>
<div class="stock available"><span><?php echo __('In stock') ?></span></div>
Expand Down Expand Up @@ -129,6 +130,17 @@
</tbody>
</table>
</div>
<?php if (!$block->isRedirectToCartEnabled()) : ?>
<script>
require([
'jquery',
'domReady!',
'Magento_Catalog/js/catalog-add-to-cart'
], function($) {
$('[data-role=tocart-form]').catalogAddToCart();
});
</script>
<?php endif; ?>
<?php else: ?>
<div class="message info empty"><div><?php echo __('You have no items to compare.') ?></div></div>
<?php endif; ?>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
use Magento\Framework\App\Action\Action;

// @codingStandardsIgnoreFile

Expand Down Expand Up @@ -70,10 +71,14 @@ $imageBlock = $block->getLayout()->createBlock('Magento\Catalog\Block\Product\I
<div class="product actions product-item-actions"<?php echo strpos($pos, $viewMode . '-actions') ? $position : ''; ?>>
<div class="actions-primary"<?php echo strpos($pos, $viewMode . '-primary') ? $position : ''; ?>>
<?php if ($_product->isSaleable()): ?>
<button type="button" title="<?php echo __('Add to Cart') ?>" class="action tocart primary"
data-post='<?php echo $block->getAddToCartPostParams($_product); ?>'>
<span><?php echo __('Add to Cart') ?></span>
</button>
<?php $postParams = $block->getAddToCartPostParams($_product); ?>
<form data-role="tocart-form" action="<?php echo $postParams['action']; ?>" method="post">
<input type="hidden" name="product" value="<?php echo $postParams['data']['product']; ?>">
<input type="hidden" name="<?php echo Action::PARAM_NAME_URL_ENCODED; ?>" value="<?php echo $postParams['data'][Action::PARAM_NAME_URL_ENCODED]; ?>">
<button type="submit" title="<?php echo __('Add to Cart') ?>" class="action tocart primary">
<span><?php echo __('Add to Cart') ?></span>
</button>
</form>
<?php else: ?>
<?php if ($_product->getIsSalable()): ?>
<div class="stock available"><span><?php echo __('In stock') ?></span></div>
Expand Down Expand Up @@ -114,4 +119,14 @@ $imageBlock = $block->getLayout()->createBlock('Magento\Catalog\Block\Product\I
</ol>
</div>
<?php echo $block->getToolbarHtml() ?>
<?php if (!$block->isRedirectToCartEnabled()) : ?>
<script>
require([
'jquery',
'Magento_Catalog/js/catalog-add-to-cart'
], function($) {
$('[data-role=tocart-form],.form.map.checkout').catalogAddToCart();
});
</script>
<?php endif; ?>
<?php endif; ?>
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,32 @@
</fieldset>
</div>
<?php endif; ?>

<script>
require([
"jquery",
"mage/mage",
"Magento_Catalog/product/view/validation"
], function($){
$('#product_addtocart_form').mage('validation', {
radioCheckboxClosest: '.nested'
});
});
<?php if ($block->isRedirectToCartEnabled()) : ?>
require([
"jquery",
"mage/mage",
"Magento_Catalog/product/view/validation"
], function($){
$('#product_addtocart_form').mage('validation', {
radioCheckboxClosest: '.nested'
});
});
<?php else : ?>
require([
"jquery",
"mage/mage",
"Magento_Catalog/product/view/validation",
"Magento_Catalog/js/catalog-add-to-cart"
], function($){
$('#product_addtocart_form').mage('validation', {
radioCheckboxClosest: '.nested',
submitHandler: function(form) {
var widget = $(form).catalogAddToCart({bindSubmit: false});
widget.catalogAddToCart('submitForm', $(form));
return false;
}
});
});
<?php endif; ?>
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
define([
'jquery',
'jquery/ui'
], function($) {
"use strict";

$.widget('mage.catalogAddToCart', {

options: {
processStart: null,
processStop: null,
bindSubmit: true,
minicartSelector: '[data-block="minicart"]',
messagesSelector: '[data-placeholder="messages"]',
productStatusSelector: '.stock.available'
},

_create: function() {
if (this.options.bindSubmit) {
this._bindSubmit();
}
},

_bindSubmit: function() {
var self = this;
this.element.on('submit', function(e) {
e.preventDefault();
self.submitForm($(this));
});
},

isLoaderEnabled: function() {
return this.options.processStart && this.options.processStop;
},

submitForm: function(form) {
var self = this;
$.ajax({
url: form.attr('action'),
data: form.serialize(),
type: 'post',
dataType: 'json',
beforeSend: function() {
if (self.isLoaderEnabled()) {
$('body').trigger(self.options.processStart);
}
},
success: function(res) {
if (self.isLoaderEnabled()) {
$('body').trigger(self.options.processStop);
}

if (res.backUrl) {
window.location = res.backUrl;
return;
}
if (res.messages) {
$(self.options.messagesSelector).html(res.messages);
}
if (res.minicart) {
$(self.options.minicartSelector).replaceWith(res.minicart);
}
if (res.product && res.product.statusText) {
$(self.options.productStatusSelector)
.removeClass('available')
.addClass('unavailable')
.find('span')
.html(res.product.statusText);
}
}
});
}
});

return $.mage.catalogAddToCart;
});
8 changes: 0 additions & 8 deletions app/code/Magento/Catalog/view/frontend/web/js/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,6 @@ define([
e.preventDefault();
window.print();
});

$.each(this.options.selectors, function(i, selector) {
$(selector).on('click', function(e) {
e.preventDefault();
window.location.href = $(this).data('url');
});
});

}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
options: {
radioCheckboxClosest: 'ul, ol',
errorPlacement: function (error, element) {
if (element.attr('data-validate-message-box')) {
var messageBox = $(element.attr('data-validate-message-box'));
if (element.attr('data-errors-message-box')) {
var messageBox = $(element.attr('data-errors-message-box'));
messageBox.html(error);
return;
}
Expand Down
50 changes: 36 additions & 14 deletions app/code/Magento/Checkout/Controller/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,26 +74,18 @@ public function __construct(
/**
* Set back redirect url to response
*
* @param null|string $backUrl
*
* @return \Magento\Framework\Controller\Result\Redirect
*/
protected function _goBack()
protected function _goBack($backUrl = null)
{
$returnUrl = $this->getRequest()->getParam('return_url');
$resultRedirect = $this->resultRedirectFactory->create();
if ($returnUrl && $this->_isInternalUrl($returnUrl)) {
$this->messageManager->getMessages()->clear();
$resultRedirect->setUrl($returnUrl);
} elseif (!$this->_scopeConfig->getValue('checkout/cart/redirect_to_cart', ScopeInterface::SCOPE_STORE)
&& !$this->getRequest()->getParam('in_cart')
&& ($backUrl = $this->_redirect->getRefererUrl())
) {

if ($backUrl || $backUrl = $this->getBackUrl($this->_redirect->getRefererUrl())) {
$resultRedirect->setUrl($backUrl);
} else {
if ($this->getRequest()->getActionName() == 'add' && !$this->getRequest()->getParam('in_cart')) {
$this->_checkoutSession->setContinueShoppingUrl($this->_redirect->getRefererUrl());
}
$resultRedirect->setPath('checkout/cart');
}

return $resultRedirect;
}

Expand All @@ -118,4 +110,34 @@ protected function _isInternalUrl($url)
$secure = strpos($url, $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_LINK, true)) === 0;
return $unsecure || $secure;
}

/**
* Get resolved back url
*
* @param null $defaultUrl
*
* @return mixed|null|string
*/
protected function getBackUrl($defaultUrl = null)
{
$returnUrl = $this->getRequest()->getParam('return_url');
if ($returnUrl && $this->_isInternalUrl($returnUrl)) {
$this->messageManager->getMessages()->clear();
return $returnUrl;
}

$shouldRedirectToCart = $this->_scopeConfig->getValue(
'checkout/cart/redirect_to_cart',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);

if ($shouldRedirectToCart || $this->getRequest()->getParam('in_cart')) {
if ($this->getRequest()->getActionName() == 'add' && !$this->getRequest()->getParam('in_cart')) {
$this->_checkoutSession->setContinueShoppingUrl($this->_redirect->getRefererUrl());
}
return $this->_url->getUrl('checkout/cart');
}

return $defaultUrl;
}
}
Loading

0 comments on commit 8ce0b8d

Please sign in to comment.