Skip to content

Commit

Permalink
MAGETWO-58504: Add Configurable Product To Cart from Category Page #2574
Browse files Browse the repository at this point in the history
 #5850 #5882 #6572 #5558 - for 2.0
  • Loading branch information
lestare committed Nov 29, 2016
1 parent 4ac2029 commit b90cf48
Show file tree
Hide file tree
Showing 33 changed files with 1,566 additions and 213 deletions.
2 changes: 1 addition & 1 deletion app/code/Magento/Catalog/Block/Product/AbstractProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function __construct(\Magento\Catalog\Block\Product\Context $context, arr
*/
public function getAddToCartUrl($product, $additional = [])
{
if ($product->getTypeInstance()->hasRequiredOptions($product)) {
if (!$product->getTypeInstance()->isPossibleBuyFromList($product)) {
if (!isset($additional['_escape'])) {
$additional['_escape'] = true;
}
Expand Down
11 changes: 11 additions & 0 deletions app/code/Magento/Catalog/Model/Product/Type/AbstractType.php
Original file line number Diff line number Diff line change
Expand Up @@ -1070,4 +1070,15 @@ public function getAssociatedProducts($product)
{
return [];
}

/**
* Check if product can be potentially buyed from the category page or some other list.
*
* @param \Magento\Catalog\Model\Product $product
* @return bool
*/
public function isPossibleBuyFromList($product)
{
return !$this->hasRequiredOptions($product);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ public function testGetAddToCartPostParams()
];

$this->typeInstanceMock->expects($this->once())
->method('hasRequiredOptions')
->method('isPossibleBuyFromList')
->with($this->equalTo($this->productMock))
->will($this->returnValue(false));
->will($this->returnValue(true));
$this->cartHelperMock->expects($this->any())
->method('getAddUrl')
->with($this->equalTo($this->productMock), $this->equalTo([]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ define([
}

if (res.backUrl) {
var eventData = {
'form': form,
'redirectParameters': []
};

// trigger global event, so other modules will be able add parameters to redirect url
$('body').trigger('catalogCategoryAddToCartRedirect', eventData);
if (eventData.redirectParameters.length > 0) {
var parameters = res.backUrl.split('#');
parameters.push(eventData.redirectParameters.join('&'));
res.backUrl = parameters.join('#');
}
window.location = res.backUrl;
return;
}
Expand Down Expand Up @@ -120,4 +132,4 @@ define([
});

return $.mage.catalogAddToCart;
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -1185,4 +1185,23 @@ public function setImageFromChildProduct(\Magento\Catalog\Model\Product $product
}
return parent::setImageFromChildProduct($product);
}

/**
* @inheritdoc
*/
public function isPossibleBuyFromList($product)
{
$isAllCustomOptionsDisplayed = true;

foreach ($this->getConfigurableAttributes($product) as $attribute) {
$eavAttribute = $attribute->getProductAttribute();

$isAllCustomOptionsDisplayed = (
$isAllCustomOptionsDisplayed
&& $eavAttribute->getData('used_in_product_listing')
);
}

return $isAllCustomOptionsDisplayed;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<?php /** @var $block \Magento\Swatches\Block\Product\Renderer\Configurable */ ?>
<div class="swatch-opt-<?php /* @escapeNotVerified */ echo $block->getProduct()->getId() ?>"></div>
<script>
require(["jquery", "jquery/ui", "Magento_Swatches/js/swatchRenderer"], function ($) {
require(["jquery", "jquery/ui", "Magento_Swatches/js/SwatchRenderer", "Magento_Swatches/js/catalog-add-to-cart"], function ($) {
$('.swatch-opt-<?php /* @escapeNotVerified */ echo $block->getProduct()->getId() ?>').SwatchRenderer({
selectorProduct: '.product-item-details',
onlySwatches: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<script type="text/x-magento-init">
{
"[data-role=swatch-options]": {
"Magento_Swatches/js/swatchRenderer": {
"Magento_Swatches/js/SwatchRenderer": {
"jsonConfig": <?php /* @escapeNotVerified */ echo $block->getJsonConfig(); ?>,
"jsonSwatchConfig": <?php /* @escapeNotVerified */ echo $block->getJsonSwatchConfig(); ?>,
"mediaCallback": "<?php /* @escapeNotVerified */ echo $block->getMediaCallback() ?>"
Expand Down
Loading

0 comments on commit b90cf48

Please sign in to comment.