Skip to content

Commit 0c59f5e

Browse files
authored
ENGCOM-6689: #26065 isSaleable cache and optimize result for configurable products #26071
2 parents 0fdec6e + d4503bd commit 0c59f5e

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

app/code/Magento/ConfigurableProduct/Model/Product/Type/Configurable.php

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ class Configurable extends \Magento\Catalog\Model\Product\Type\AbstractType
102102
*/
103103
protected $_canConfigure = true;
104104

105+
/**
106+
* Local cache
107+
*
108+
* @var array
109+
*/
110+
protected $isSaleableBySku = [];
111+
105112
/**
106113
* @var \Magento\Framework\App\Config\ScopeConfigInterface
107114
*/
@@ -585,7 +592,7 @@ protected function getGalleryReadHandler()
585592
* @param \Magento\Catalog\Model\Product $product
586593
* @return \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Product\Collection
587594
*/
588-
public function getUsedProductCollection($product)
595+
protected function getLinkedProductCollection($product)
589596
{
590597
$collection = $this->_productCollectionFactory->create()->setFlag(
591598
'product_children',
@@ -600,6 +607,17 @@ public function getUsedProductCollection($product)
600607
return $collection;
601608
}
602609

610+
/**
611+
* Retrieve related products collection. Extension point for listing
612+
*
613+
* @param \Magento\Catalog\Model\Product $product
614+
* @return \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Product\Collection
615+
*/
616+
public function getUsedProductCollection($product)
617+
{
618+
return $this->getLinkedProductCollection($product);
619+
}
620+
603621
/**
604622
* Before save process
605623
*
@@ -744,15 +762,27 @@ private function saveRelatedProducts(ProductInterface $product)
744762
*/
745763
public function isSalable($product)
746764
{
765+
$storeId = $this->getStoreFilter($product);
766+
if ($storeId instanceof \Magento\Store\Model\Store) {
767+
$storeId = $storeId->getId();
768+
}
769+
770+
$sku = $product->getSku();
771+
if (isset($this->isSaleableBySku[$storeId][$sku])) {
772+
return $this->isSaleableBySku[$storeId][$sku];
773+
}
774+
747775
$salable = parent::isSalable($product);
748776

749777
if ($salable !== false) {
750-
$collection = $this->getUsedProductCollection($product);
751-
$collection->addStoreFilter($this->getStoreFilter($product));
778+
$collection = $this->getLinkedProductCollection($product);
779+
$collection->addStoreFilter($storeId);
752780
$collection = $this->salableProcessor->process($collection);
753781
$salable = 0 !== $collection->getSize();
754782
}
755783

784+
$this->isSaleableBySku[$storeId][$sku] = $salable;
785+
756786
return $salable;
757787
}
758788

app/code/Magento/ConfigurableProduct/Test/Unit/Model/Product/Type/ConfigurableTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -567,12 +567,18 @@ public function testHasOptionsFalse()
567567
public function testIsSalable()
568568
{
569569
$productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
570-
->setMethods(['__wakeup', 'getStatus', 'hasData', 'getData', 'getStoreId', 'setData'])
570+
->setMethods(['__wakeup', 'getStatus', 'hasData', 'getData', 'getStoreId', 'setData', 'getSku'])
571571
->disableOriginalConstructor()
572572
->getMock();
573+
$productMock
574+
->expects($this->at(0))
575+
->method('getData')
576+
->with('_cache_instance_store_filter')
577+
->willReturn(0);
573578
$productMock->expects($this->once())->method('getStatus')->willReturn(1);
574579
$productMock->expects($this->any())->method('hasData')->willReturn(true);
575-
$productMock->expects($this->at(2))->method('getData')->with('is_salable')->willReturn(true);
580+
$productMock->expects($this->at(1))->method('getSku')->willReturn('SKU-CODE');
581+
$productMock->expects($this->at(4))->method('getData')->with('is_salable')->willReturn(true);
576582
$productCollection = $this->getMockBuilder(
577583
\Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Product\Collection::class
578584
)

0 commit comments

Comments
 (0)