Skip to content

Commit

Permalink
MAGETWO-58503: [Backport] - [Github] Add Configurable Product To Cart…
Browse files Browse the repository at this point in the history
… from Category Page #2574 #5850 #5882 #6572 #5558 #8470 - for 2.1
  • Loading branch information
OlgaVasyltsun committed Nov 9, 2017
1 parent 8ffe3cc commit 640991b
Show file tree
Hide file tree
Showing 24 changed files with 855 additions and 19 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
12 changes: 12 additions & 0 deletions app/code/Magento/Catalog/Model/Product/Type/AbstractType.php
Original file line number Diff line number Diff line change
Expand Up @@ -1092,4 +1092,16 @@ 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 @@ -195,9 +195,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 @@ -86,6 +86,17 @@ 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
Original file line number Diff line number Diff line change
Expand Up @@ -1361,4 +1361,25 @@ public function getSalableUsedProducts(Product $product, $requiredAttributeIds =

return $usedSalableProducts;
}

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

foreach ($this->getConfigurableAttributes($product) as $attribute) {
/** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $eavAttribute */
$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,15 +7,17 @@
<?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/swatch-renderer"], function ($) {
$('.swatch-opt-<?php /* @escapeNotVerified */ echo $block->getProduct()->getId() ?>').SwatchRenderer({
selectorProduct: '.product-item-details',
onlySwatches: true,
enableControlLabel: false,
numberToShow: <?php /* @escapeNotVerified */ echo $block->getNumberSwatchesPerProduct(); ?>,
jsonConfig: <?php /* @escapeNotVerified */ echo $block->getJsonConfig(); ?>,
jsonSwatchConfig: <?php /* @escapeNotVerified */ echo $block->getJsonSwatchConfig(); ?>,
mediaCallback: '<?php /* @escapeNotVerified */ echo $block->getMediaCallback() ?>'
require(
["jquery", "jquery/ui", "Magento_Swatches/js/swatch-renderer", "Magento_Swatches/js/catalog-add-to-cart"],
function ($) {
$('.swatch-opt-<?php /* @escapeNotVerified */ echo $block->getProduct()->getId() ?>').SwatchRenderer({
selectorProduct: '.product-item-details',
onlySwatches: true,
enableControlLabel: false,
numberToShow: <?php /* @escapeNotVerified */ echo $block->getNumberSwatchesPerProduct(); ?>,
jsonConfig: <?php /* @escapeNotVerified */ echo $block->getJsonConfig(); ?>,
jsonSwatchConfig: <?php /* @escapeNotVerified */ echo $block->getJsonSwatchConfig(); ?>,
mediaCallback: '<?php /* @escapeNotVerified */ echo $block->getMediaCallback() ?>'
});
});
});
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
require([
'jquery'
], function ($) {
'use strict';

$('body').on('catalogCategoryAddToCartRedirect', function (event, data) {
$(data.form).find('[name*="super"]').each(function (index, item) {
var $item = $(item);

data.redirectParameters.push($item.attr('data-attr-name') + '=' + $item.val());
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ protected function prepareConfigurableMatrix(FixtureInterface $product)
$keyIds[] = $attribute['options'][$optionKey]['id'];
$configurableAttribute[] = sprintf(
'"%s":"%s"',
$attribute['attribute_code'],
isset($attribute['attribute_code']) ? $attribute['attribute_code'] : $attribute['frontend_label'],
$attribute['options'][$optionKey]['id']
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
*/
namespace Magento\Swatches\Test\Block\Product;

use Magento\Catalog\Test\Block\Product\ListProduct as CatalogListProduct;
use Magento\Mtf\Client\Locator;
use Magento\Mtf\Fixture\FixtureInterface;
use Magento\Catalog\Test\Block\Product\ListProduct as CatalogListProduct;
use Magento\Swatches\Test\Block\Product\ProductList\ProductItem;

/**
* @inheritdoc
Expand All @@ -22,7 +23,7 @@ public function getProductItem(FixtureInterface $product)
$locator = sprintf($this->productItem, $product->getName());

return $this->blockFactory->create(
\Magento\Swatches\Test\Block\Product\ProductList\ProductItem::class,
ProductItem::class,
['element' => $this->_rootElement->find($locator, Locator::SELECTOR_XPATH)]
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,30 @@

namespace Magento\Swatches\Test\Block\Product\ProductList;

use Magento\Mtf\Client\Locator;
use Magento\Catalog\Test\Block\Product\ProductList\ProductItem as CatalogProductItem;
use Magento\ConfigurableProduct\Test\Fixture\ConfigurableProduct;
use Magento\Mtf\Client\Locator;
use Magento\Swatches\Test\Fixture\SwatchesProductAttribute;

/**
* Product item block on frontend category view.
*/
class ProductItem extends CatalogProductItem
{
/**
* Selector for the swatches of the product.
* Selector for the swatches blocks of the product.
*
* @var string
*/
protected $swatchBlockSelector = '.swatch-attribute-options';

/**
* Selector for the swatches items of the product.
*
* @var string
*/
private $swatchItemSelector = 'div[option-id="%s"]';

/**
* Check swatches visibility.
*
Expand All @@ -30,4 +39,72 @@ public function isSwatchesBlockVisible()
{
return $this->_rootElement->find($this->swatchBlockSelector)->isVisible();
}

/**
* Fill product options on category page.
*
* @param ConfigurableProduct $product
* @return void
*/
public function fillData(ConfigurableProduct $product)
{
/** @var array $checkoutData */
$checkoutData = $product->getCheckoutData();

/** @var array $options */
$options = $checkoutData['options']['configurable_options'];

/** @var array $confAttrData */
$confAttrData = $product->getDataFieldConfig('configurable_attributes_data');

/** @var ConfigurableProduct\ConfigurableAttributesData $confAttrSource */
$confAttrSource = $confAttrData['source'];

/** @var SwatchesProductAttribute[] $attributes */
$attributes = $confAttrSource->getAttributes();

foreach ($options as $option) {
if (!isset($attributes[$option['title']])) {
continue;
}

/** @var array $availableOptions */
$availableOptions = $attributes[$option['title']]->getOptions();

/** @var string $optionKey */
$optionKey = str_replace('option_key_', '', $option['value']);

if (!isset($availableOptions[$optionKey])) {
continue;
}

/** @var array $optionForSelect */
$optionForSelect = $availableOptions[$optionKey];

$this->clickOnSwatch($optionForSelect['id']);
}
}

/**
* Click on swatch.
*
* @param $optionId
*/
private function clickOnSwatch($optionId)
{
/** @var string $selector */
$selector = sprintf($this->swatchItemSelector, $optionId);

$this->_rootElement->find($selector, Locator::SELECTOR_CSS)->click();
}

/**
* @inheritdoc
*/
public function clickAddToCart()
{
$this->_rootElement->hover();

parent::clickAddToCart();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Swatches\Test\Block\Product;

use Magento\Catalog\Test\Block\Product\View;
use Magento\Mtf\Fixture\FixtureInterface;

/**
* Configurable product view block with swatch attributes on frontend product page.
*/
class ViewWithSwatches extends View
{
/**
* Selector for swatch attribute value.
*
* @var string
*/
private $swatchAttributeSelector = '.swatch-attribute.%s .swatch-attribute-selected-option';

/**
* Get chosen options from the product view page.
*
* @param FixtureInterface $product
* @return array
*/
public function getSelectedSwatchOptions(FixtureInterface $product)
{
/** @var array $checkoutData */
$checkoutData = $product->getCheckoutData();

/** @var array $availableAttributes */
$availableAttributes = $product->getConfigurableAttributesData();

/** @var array $attributesData */
$attributesData = $availableAttributes['attributes_data'];

/** @var array $formData */
$formData = [];

/** @var array $item */
foreach ($checkoutData['options']['configurable_options'] as $item) {
/** @var string $selector */
$selector = sprintf(
$this->swatchAttributeSelector,
$attributesData[$item['title']]['attribute_code']
);

$this->waitForElementVisible($selector);

/** @var string $selected */
$selected = $this->_rootElement->find($selector)->getText();

$formData[$item['title']] = $selected;
}

return $formData;
}
}
Loading

0 comments on commit 640991b

Please sign in to comment.