Skip to content

Commit 7420329

Browse files
author
Palamar, Mykola
committed
MAGETWO-47017: [Github] Add Configurable Product To Cart from Category Page #2574 #5850 #5882 #6572 #5558
- CR changes
1 parent ac363e4 commit 7420329

27 files changed

+495
-319
lines changed

app/code/Magento/Catalog/view/frontend/web/js/catalog-add-to-cart.js

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -64,30 +64,10 @@ define([
6464
},
6565

6666
ajaxSubmit: function(form) {
67-
var self = this,
68-
swatchAttributesValue = [],
69-
urlHashParams = '';
67+
var self = this;
7068
$(self.options.minicartSelector).trigger('contentLoading');
7169
self.disableAddToCartButton(form);
7270

73-
$(form.prop("elements")).each(function(index, item){
74-
item = $(item);
75-
if (item.attr('name') && item.attr('name').search('super') != -1) {
76-
var r = /\[(\d+)\]/;
77-
var matches = r.exec(item.attr('name'));
78-
79-
if (matches) {
80-
swatchAttributesValue.push(item.attr('data-attr-name') + '=' + item.val());
81-
}
82-
}
83-
});
84-
85-
console.log(form.serialize());
86-
if (swatchAttributesValue.length > 0) {
87-
urlHashParams = '#' + swatchAttributesValue.join('&');
88-
}
89-
90-
9171
$.ajax({
9272
url: form.attr('action'),
9373
data: form.serialize(),
@@ -104,7 +84,18 @@ define([
10484
}
10585

10686
if (res.backUrl) {
107-
window.location = res.backUrl + urlHashParams;
87+
var eventData = {
88+
'form': form,
89+
'redirectParameters': []
90+
}
91+
// trigger global event, so other modules will be able add parameters to redirect url
92+
$('body').trigger('catalogCategoryAddToCartRedirect', eventData);
93+
if (eventData.redirectParameters.length > 0) {
94+
var parameters = res.backUrl.split('#');
95+
parameters.push(eventData.redirectParameters.join('&'));
96+
res.backUrl = parameters.join('#');
97+
}
98+
window.location = res.backUrl;
10899
return;
109100
}
110101
if (res.messages) {

app/code/Magento/Swatches/view/frontend/templates/product/listing/renderer.phtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<?php /** @var $block \Magento\Swatches\Block\Product\Renderer\Configurable */ ?>
88
<div class="swatch-opt-<?php /* @escapeNotVerified */ echo $block->getProduct()->getId() ?>"></div>
99
<script>
10-
require(["jquery", "jquery/ui", "Magento_Swatches/js/swatch-renderer"], function ($) {
10+
require(["jquery", "jquery/ui", "Magento_Swatches/js/swatch-renderer", "Magento_Swatches/js/catalog-add-to-cart"], function ($) {
1111
$('.swatch-opt-<?php /* @escapeNotVerified */ echo $block->getProduct()->getId() ?>').SwatchRenderer({
1212
selectorProduct: '.product-item-details',
1313
onlySwatches: true,
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Copyright © 2016 Magento. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
require([
6+
'jquery'
7+
], function ($) {
8+
'use strict';
9+
10+
$('body').on('catalogCategoryAddToCartRedirect', function (event, data) {
11+
$(data.form).find('[name*="super"]').each(function (index, item) {
12+
var $item = $(item);
13+
data.redirectParameters.push($item.attr('data-attr-name') + '=' + $item.val());
14+
});
15+
})
16+
});

app/code/Magento/Swatches/view/frontend/web/js/swatch-renderer.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,13 @@ define([
636636
$input.trigger('change');
637637
},
638638

639+
/**
640+
* Get human readable attribute code (eg. size, color) by it ID from configuration
641+
*
642+
* @param attributeId
643+
* @returns {*}
644+
* @private
645+
*/
639646
_getAttributeCodeById: function (attributeId) {
640647
var attribute = this.options.jsonConfig.mappedAttributes[attributeId];
641648
return attribute ? attribute.code : attributeId;

dev/tests/functional/tests/app/Magento/Swatches/Test/Block/Product/ProductList/ProductItem.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,40 @@
1515
class ProductItem extends CatalogProductItem
1616
{
1717
/**
18+
* Selector for the swatches of the product.
19+
*
1820
* @var string
1921
*/
2022
protected $swatchSelector = 'div[option-id="%s"]';
2123

2224
/**
23-
* Fill product options on category page
25+
* Fill product options on category page.
2426
*
25-
* @param $product
27+
* @param \Magento\ConfigurableProduct\Test\Fixture\ConfigurableProduct $product
28+
* @return void
2629
*/
27-
public function fillData($product) {
30+
public function fillData(\Magento\ConfigurableProduct\Test\Fixture\ConfigurableProduct $product) {
2831
$checkoutData = $product->getCheckoutData();
2932
$options = $checkoutData['options']['configurable_options'];
3033
$confAttrData = $product->getDataFieldConfig('configurable_attributes_data');
3134
$attributes = ($confAttrData['source'])->getAttributes();
3235

3336
foreach ($options as $option) {
37+
if (!isset($attributes[$option['title']])) {
38+
continue;
39+
}
3440
$availableOptions = $attributes[$option['title']]->getOptions();
35-
$optionForSelect = $availableOptions[str_replace('option_key_', '', $option['value'])];
41+
$optionKey = str_replace('option_key_', '', $option['value']);
42+
if (!isset($availableOptions[$optionKey])) {
43+
continue;
44+
}
45+
$optionForSelect = $availableOptions[$optionKey];
3646
$this->clickOnSwatch($optionForSelect['id']);
3747
}
3848
}
3949

4050
/**
41-
* Click on swatch
51+
* Click on swatch.
4252
*
4353
* @param $optionId
4454
*/
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Swatches\Test\Block\Product;
8+
9+
use Magento\Catalog\Test\Block\Product\View;
10+
use Magento\Mtf\Fixture\InjectableFixture;
11+
12+
/**
13+
* Configurable product view block with swatch attributes on frontend product page
14+
*/
15+
class ViewWithSwatches extends View
16+
{
17+
/**
18+
* Selector for swatch attribute value
19+
*
20+
* @var string
21+
*/
22+
private $swatchAttributeSelector = '.swatch-attribute.%s .swatch-attribute-selected-option';
23+
24+
public function getSelectedSwatchOptions(InjectableFixture $product)
25+
{
26+
$checkoutData = $product->getCheckoutData();
27+
$availableAttributes = $product->getConfigurableAttributesData();
28+
$attributesData = $availableAttributes['attributes_data'];
29+
$formData = [];
30+
foreach ($checkoutData['options']['configurable_options'] as $item) {
31+
$selector = sprintf($this->swatchAttributeSelector, $attributesData[$item['title']]['attribute_code']);
32+
$this->waitForElementVisible($selector);
33+
$selected = $this->_rootElement->find($selector)->getText();
34+
$formData[$item['title']] = $selected;
35+
}
36+
37+
return $formData;
38+
}
39+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Swatches\Test\Constraint;
8+
9+
use Magento\Catalog\Test\Constraint\AssertProductPage;
10+
use Magento\Mtf\Fixture\FixtureInterface;
11+
use Magento\Catalog\Test\Page\Product\CatalogProductView;
12+
use Magento\Mtf\Client\BrowserInterface;
13+
14+
/**
15+
* Assert that product with swatches and regular dropdown redirect can't be add to cart from catalog catergory page.
16+
*/
17+
class AssertSwatchConfigurableProductPage extends AssertProductPage
18+
{
19+
/**
20+
* Assert that configurable product with swatches displays correctly.
21+
*
22+
* @param BrowserInterface $browser
23+
* @param CatalogProductView $catalogProductView
24+
* @param FixtureInterface $product
25+
*/
26+
public function processAssert(
27+
BrowserInterface $browser,
28+
CatalogProductView $catalogProductView,
29+
FixtureInterface $product
30+
)
31+
{
32+
$this->product = $product;
33+
$this->productView = $catalogProductView->getProductViewWithSwatchesBlock();
34+
$this->objectManager->create(
35+
\Magento\Swatches\Test\TestStep\AddProductToCartFromCatalogCategoryPageStep ::class,
36+
[
37+
'product' => $product
38+
]
39+
)->run();
40+
// we need this line for waiti until page will be fully loaded
41+
$this->productView->getSelectedSwatchOptions($this->product);
42+
$errors = $this->verify();
43+
\PHPUnit_Framework_Assert::assertEmpty(
44+
$errors,
45+
"\nFound the following errors:\n" . implode(" \n", $errors)
46+
);
47+
}
48+
49+
/**
50+
* Verify product on product view page.
51+
*
52+
* @return array
53+
*/
54+
protected function verify()
55+
{
56+
$errors = parent::verify();
57+
$errors[] = $this->verifySwatches();
58+
59+
return array_filter($errors);
60+
}
61+
62+
/**
63+
* Verify selected swatches on product view page.
64+
*
65+
* @return array
66+
*/
67+
protected function verifySwatches()
68+
{
69+
$actualData = $this->productView->getSelectedSwatchOptions($this->product);
70+
$expectedData = $this->convertCheckoutData($this->product);
71+
$this->verifyData($expectedData, $actualData);
72+
}
73+
74+
/**
75+
* Get swatch attributes formatter to attributes comparison.
76+
*
77+
* @param FixtureInterface $product
78+
* @return array
79+
*/
80+
public function convertCheckoutData(FixtureInterface $product)
81+
{
82+
$out = [];
83+
$checkoutData = $product->getCheckoutData();
84+
$availableAttributes = $product->getConfigurableAttributesData();
85+
$attributesData = $availableAttributes['attributes_data'];
86+
foreach ($checkoutData['options']['configurable_options'] as $item) {
87+
$out[$item['title']] = $attributesData[$item['title']]['options'][$item['value']]['label'];
88+
}
89+
90+
return $out;
91+
}
92+
93+
/**
94+
* Return string representation of the object.
95+
*
96+
* @return string
97+
*/
98+
public function toString()
99+
{
100+
return 'Swatch attributes displayed as expected on product page';
101+
}
102+
}

dev/tests/functional/tests/app/Magento/Swatches/Test/Fixture/Cart/Item.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@
1313
*/
1414
class Item extends ConfigurableCart
1515
{
16-
16+
//
1717
}

dev/tests/functional/tests/app/Magento/Swatches/Test/Fixture/ConfigurableProduct.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,5 @@
1212
class="Magento\Swatches\Test\Fixture\ConfigurableProduct"
1313
extends="\Magento\ConfigurableProduct\Test\Fixture\ConfigurableProduct"
1414
>
15-
<!-- <field name="configurable_attributes_data" is_required="0" group="variations" source="Magento\Swatches\Test\Fixture\ConfigurableProduct\ConfigurableAttributesData" repository="Magento\ConfigurableProduct\Test\Repository\ConfigurableProduct\ConfigurableAttributesData" />-->
1615
</fixture>
1716
</config>

dev/tests/functional/tests/app/Magento/Swatches/Test/Fixture/ConfigurableProduct/ConfigurableAttributesData.php

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)