Skip to content

Commit aa4b734

Browse files
authored
Merge pull request #2725 from magento-tango/MAGETWO-74021
MAGETWO-74021: "Catalog Products List" widget does not displays on frontend
2 parents e80727a + 38c6c7f commit aa4b734

File tree

7 files changed

+130
-52
lines changed

7 files changed

+130
-52
lines changed

app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,14 +1079,15 @@ public function getAllAttributeValues($attribute)
10791079
$select = clone $this->getSelect();
10801080
$attribute = $this->getEntity()->getAttribute($attribute);
10811081

1082-
$aiField = $this->getConnection()->getAutoIncrementField($this->getMainTable());
1082+
$fieldMainTable = $this->getConnection()->getAutoIncrementField($this->getMainTable());
1083+
$fieldJoinTable = $attribute->getEntity()->getLinkField();
10831084
$select->reset()
10841085
->from(
10851086
['cpe' => $this->getMainTable()],
10861087
['entity_id']
10871088
)->join(
10881089
['cpa' => $attribute->getBackend()->getTable()],
1089-
'cpe.' . $aiField . ' = cpa.' . $aiField,
1090+
'cpe.' . $fieldMainTable . ' = cpa.' . $fieldJoinTable,
10901091
['store_id', 'value']
10911092
)->where('attribute_id = ?', (int)$attribute->getId());
10921093

app/code/Magento/CatalogWidget/Model/Rule/Condition/Product.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public function addToCollection($collection)
118118
{
119119
$attribute = $this->getAttributeObject();
120120

121-
if ($collection->isEnabledFlat()) {
121+
if ($attribute->getUsedInProductListing() && $collection->isEnabledFlat()) {
122122
$alias = array_keys($collection->getSelect()->getPart('from'))[0];
123123
$this->joinedAttributes[$attribute->getAttributeCode()] = $alias . '.' . $attribute->getAttributeCode();
124124
return $this;

app/code/Magento/Rule/Model/Condition/Sql/Builder.php

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,10 @@ protected function _joinTablesToCollection(
127127
*
128128
* @param AbstractCondition $condition
129129
* @param string $value
130-
* @param bool $isDefaultStoreUsed
130+
* @param bool $isDefaultStoreUsed no longer used because caused an issue about not existing table alias
131131
* @return string
132132
* @throws \Magento\Framework\Exception\LocalizedException
133+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
133134
*/
134135
protected function _getMappedSqlCondition(AbstractCondition $condition, $value = '', $isDefaultStoreUsed = true)
135136
{
@@ -147,12 +148,6 @@ protected function _getMappedSqlCondition(AbstractCondition $condition, $value =
147148
}
148149

149150
$defaultValue = 0;
150-
// Check if attribute has a table with default value and add it to the query
151-
if ($this->canAttributeHaveDefaultValue($condition->getAttribute(), $isDefaultStoreUsed)) {
152-
$defaultField = 'at_' . $condition->getAttribute() . '_default.value';
153-
$defaultValue = $this->_connection->quoteIdentifier($defaultField);
154-
}
155-
156151
$sql = str_replace(
157152
':field',
158153
$this->_connection->getIfNullSql($this->_connection->quoteIdentifier($argument), $defaultValue),
@@ -204,45 +199,10 @@ public function attachConditionToCollection(
204199
) {
205200
$this->_connection = $collection->getResource()->getConnection();
206201
$this->_joinTablesToCollection($collection, $combine);
207-
$isDefaultStoreUsed = $this->checkIsDefaultStoreUsed($collection);
208-
$whereExpression = (string)$this->_getMappedSqlCombination($combine, '', $isDefaultStoreUsed);
202+
$whereExpression = (string)$this->_getMappedSqlCombination($combine);
209203
if (!empty($whereExpression)) {
210204
// Select ::where method adds braces even on empty expression
211205
$collection->getSelect()->where($whereExpression);
212206
}
213207
}
214-
215-
/**
216-
* Check is default store used
217-
*
218-
* @param AbstractCollection $collection
219-
* @return bool
220-
*/
221-
private function checkIsDefaultStoreUsed(AbstractCollection $collection): bool
222-
{
223-
return (int)$collection->getStoreId() === (int)$collection->getDefaultStoreId();
224-
}
225-
226-
/**
227-
* Check if attribute can have default value
228-
*
229-
* @param string $attributeCode
230-
* @param bool $isDefaultStoreUsed
231-
* @return bool
232-
*/
233-
private function canAttributeHaveDefaultValue(string $attributeCode, bool $isDefaultStoreUsed): bool
234-
{
235-
if ($isDefaultStoreUsed) {
236-
return false;
237-
}
238-
239-
try {
240-
$attribute = $this->attributeRepository->get(Product::ENTITY, $attributeCode);
241-
} catch (NoSuchEntityException $e) {
242-
// It's not exceptional case as we want to check if we have such attribute or not
243-
return false;
244-
}
245-
246-
return !$attribute->isScopeGlobal();
247-
}
248208
}

app/code/Magento/Rule/Test/Unit/Model/Condition/Sql/BuilderTest.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,6 @@ public function testAttachConditionToCollection()
6161
$collection->expects($this->any())
6262
->method('getSelect')
6363
->will($this->returnValue($select));
64-
$collection->expects($this->once())
65-
->method('getStoreId')
66-
->willReturn(1);
67-
$collection->expects($this->once())
68-
->method('getDefaultStoreId')
69-
->willReturn(1);
7064

7165
$resource->expects($this->once())
7266
->method('getConnection')
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
/**
8+
* Create multiselect attribute
9+
*/
10+
require __DIR__ . '/dropdown_attribute.php';
11+
12+
/** Create products with attribute option of dropdown type */
13+
14+
/** @var $installer \Magento\Catalog\Setup\CategorySetup */
15+
$installer = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
16+
\Magento\Catalog\Setup\CategorySetup::class
17+
);
18+
19+
/** @var $options \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection */
20+
$options = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
21+
\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection::class
22+
);
23+
$options->setAttributeFilter($attribute->getId());
24+
$optionIds = $options->getAllIds();
25+
26+
/** @var $product \Magento\Catalog\Model\Product */
27+
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
28+
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
29+
->setId($optionIds[0] * 10)
30+
->setAttributeSetId($installer->getAttributeSetId('catalog_product', 'Default'))
31+
->setWebsiteIds([1])
32+
->setName('With Option 1')
33+
->setSku('simple_op_1')
34+
->setPrice(10)
35+
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
36+
->setDropdownAttribute($optionIds[0])
37+
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
38+
->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
39+
->save();
40+
41+
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
42+
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
43+
->setId($optionIds[1] * 10)
44+
->setAttributeSetId($installer->getAttributeSetId('catalog_product', 'Default'))
45+
->setWebsiteIds([1])
46+
->setName('With Option 2')
47+
->setSku('simple_op_2')
48+
->setPrice(10)
49+
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
50+
->setDropdownAttribute($optionIds[1])
51+
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
52+
->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
53+
->save();
54+
55+
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
56+
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
57+
->setId($optionIds[2] * 10)
58+
->setAttributeSetId($installer->getAttributeSetId('catalog_product', 'Default'))
59+
->setWebsiteIds([1])
60+
->setName('With Option 3')
61+
->setSku('simple_op_3')
62+
->setPrice(10)
63+
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
64+
->setDropdownAttribute($optionIds[2])
65+
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
66+
->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
67+
->save();
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
/**
8+
* Remove all products as strategy of isolation process
9+
*/
10+
$registry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\Framework\Registry');
11+
$registry->unregister('isSecureArea');
12+
$registry->register('isSecureArea', true);
13+
14+
/** @var $productCollection \Magento\Catalog\Model\ResourceModel\Product */
15+
$productCollection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
16+
->create('Magento\Catalog\Model\Product')
17+
->getCollection();
18+
19+
foreach ($productCollection as $product) {
20+
$product->delete();
21+
}
22+
23+
$registry->unregister('isSecureArea');
24+
$registry->register('isSecureArea', false);

dev/tests/integration/testsuite/Magento/CatalogWidget/Block/Product/ProductListTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,38 @@ public function testCreateCollectionWithMultipleSkuCondition()
8888
$this->performAssertions(2);
8989
}
9090

91+
/**
92+
* Test product list widget can process condition with dropdown type of attribute which has Store Scope
93+
*
94+
* @magentoDbIsolation disabled
95+
* @magentoDataFixture Magento/Catalog/_files/products_with_dropdown_attribute.php
96+
*/
97+
public function testCreateCollectionWithDropdownAttributeStoreScope()
98+
{
99+
/** @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
100+
$attribute = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
101+
\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class
102+
);
103+
$attribute->load('dropdown_attribute', 'attribute_code');
104+
$dropdownAttributeOptionIds = [];
105+
foreach ($attribute->getOptions() as $option) {
106+
if ($option->getValue()) {
107+
$dropdownAttributeOptionIds[] = $option->getValue();
108+
}
109+
}
110+
$encodedConditions = '^[`1`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Combine`,' .
111+
'`aggregator`:`any`,`value`:`1`,`new_child`:``^],`1--1`:^[`type`:`Magento||CatalogWidget||Model||Rule|' .
112+
'|Condition||Product`,`attribute`:`dropdown_attribute`,`operator`:`==`,`value`:`'
113+
. $dropdownAttributeOptionIds[0] . '`^],`1--2`:^[`type`:`Magento||CatalogWidget||Model||Rule|' .
114+
'|Condition||Product`,`attribute`:`dropdown_attribute`,`operator`:`==`,`value`:`'
115+
. $dropdownAttributeOptionIds[1] . '`^]^]';
116+
$this->block->setData('conditions_encoded', $encodedConditions);
117+
$this->performAssertions(2);
118+
$attribute->setUsedInProductListing(0);
119+
$attribute->save();
120+
$this->performAssertions(2);
121+
}
122+
91123
/**
92124
* Check product collection includes correct amount of products.
93125
*

0 commit comments

Comments
 (0)