Skip to content

Commit f33efa8

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-48790' into pr-379
2 parents 049f719 + 40a0123 commit f33efa8

File tree

5 files changed

+49
-23
lines changed

5 files changed

+49
-23
lines changed

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

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ class Product extends AbstractResource
6666
*/
6767
protected $defaultAttributes;
6868

69+
/**
70+
* @var array
71+
*/
72+
protected $availableCategoryIdsCache = [];
73+
6974
/**
7075
* @param \Magento\Eav\Model\Entity\Context $context
7176
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
@@ -465,18 +470,22 @@ public function getAvailableInCategories($object)
465470
{
466471
// is_parent=1 ensures that we'll get only category IDs those are direct parents of the product, instead of
467472
// fetching all parent IDs, including those are higher on the tree
468-
$select = $this->getConnection()->select()->distinct()->from(
469-
$this->getTable('catalog_category_product_index'),
470-
['category_id']
471-
)->where(
472-
'product_id = ? AND is_parent = 1',
473-
(int)$object->getEntityId()
474-
)->where(
475-
'visibility != ?',
476-
\Magento\Catalog\Model\Product\Visibility::VISIBILITY_NOT_VISIBLE
477-
);
478-
479-
return $this->getConnection()->fetchCol($select);
473+
$entityId = (int)$object->getEntityId();
474+
if (!isset($this->availableCategoryIdsCache[$entityId])) {
475+
$this->availableCategoryIdsCache[$entityId] = $this->getConnection()->fetchCol(
476+
$this->getConnection()->select()->distinct()->from(
477+
$this->getTable('catalog_category_product_index'),
478+
['category_id']
479+
)->where(
480+
'product_id = ? AND is_parent = 1',
481+
$entityId
482+
)->where(
483+
'visibility != ?',
484+
\Magento\Catalog\Model\Product\Visibility::VISIBILITY_NOT_VISIBLE
485+
)
486+
);
487+
}
488+
return $this->availableCategoryIdsCache[$entityId];
480489
}
481490

482491
/**

app/code/Magento/CatalogRule/Model/Rule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ public function callbackValidateProduct($args)
337337
protected function _getWebsitesMap()
338338
{
339339
$map = [];
340-
$websites = $this->_storeManager->getWebsites(true);
340+
$websites = $this->_storeManager->getWebsites();
341341
foreach ($websites as $website) {
342342
// Continue if website has no store to be able to create catalog rule for website without store
343343
if ($website->getDefaultStore() === null) {

app/code/Magento/CatalogRule/Test/Unit/Model/RuleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public function testCallbackValidateProduct($validate)
163163
'created_at' => '2014-06-25 13:14:30',
164164
'updated_at' => '2014-06-25 14:37:15'
165165
];
166-
$this->storeManager->expects($this->any())->method('getWebsites')->with(true)
166+
$this->storeManager->expects($this->any())->method('getWebsites')->with(false)
167167
->will($this->returnValue([$this->websiteModel, $this->websiteModel]));
168168
$this->websiteModel->expects($this->at(0))->method('getId')
169169
->will($this->returnValue('1'));

app/code/Magento/CatalogRuleConfigurable/Plugin/CatalogRule/Model/ConfigurableProductsProvider.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ class ConfigurableProductsProvider
1111
/** @var \Magento\Framework\App\ResourceConnection */
1212
private $resource;
1313

14+
/**
15+
* @var array
16+
*/
17+
private $productIds = [];
18+
1419
/**
1520
* @param \Magento\Framework\App\ResourceConnection $resource
1621
*/
@@ -25,13 +30,17 @@ public function __construct(\Magento\Framework\App\ResourceConnection $resource)
2530
*/
2631
public function getIds(array $ids)
2732
{
28-
$connection = $this->resource->getConnection();
29-
return $connection->fetchCol(
30-
$connection
31-
->select()
32-
->from(['e' => $this->resource->getTableName('catalog_product_entity')], ['e.entity_id'])
33-
->where('e.type_id = ?', \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE)
34-
->where('e.entity_id IN (?)', $ids)
35-
);
33+
$key = md5(json_encode($ids));
34+
if (!isset($this->productIds[$key])) {
35+
$connection = $this->resource->getConnection();
36+
$this->productIds[$key] = $connection->fetchCol(
37+
$connection
38+
->select()
39+
->from(['e' => $this->resource->getTableName('catalog_product_entity')], ['e.entity_id'])
40+
->where('e.type_id = ?', \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE)
41+
->where('e.entity_id IN (?)', $ids)
42+
);
43+
}
44+
return $this->productIds[$key];
3645
}
3746
}

app/code/Magento/CatalogRuleConfigurable/Plugin/CatalogRule/Model/Rule/ConfigurableProductHandler.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ class ConfigurableProductHandler
2020
/** @var ConfigurableProductsProvider */
2121
private $configurableProductsProvider;
2222

23+
/**
24+
* @var array
25+
*/
26+
private $childrenProducts = [];
27+
2328
/**
2429
* @param \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable $configurable
2530
* @param ConfigurableProductsProvider $configurableProductsProvider
@@ -42,7 +47,10 @@ public function afterGetMatchingProductIds(\Magento\CatalogRule\Model\Rule $rule
4247
{
4348
$configurableProductIds = $this->configurableProductsProvider->getIds(array_keys($productIds));
4449
foreach ($configurableProductIds as $productId) {
45-
$subProductIds = $this->configurable->getChildrenIds($productId)[0];
50+
if (!isset($this->childrenProducts[$productId])) {
51+
$this->childrenProducts[$productId] = $this->configurable->getChildrenIds($productId)[0];
52+
}
53+
$subProductIds = $this->childrenProducts[$productId];
4654
$parentValidationResult = isset($productIds[$productId])
4755
? array_filter($productIds[$productId])
4856
: [];

0 commit comments

Comments
 (0)