Skip to content

#26065 isSaleable cache and optimize result for configurable products #26071

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ class Configurable extends \Magento\Catalog\Model\Product\Type\AbstractType
*/
protected $_canConfigure = true;

/**
* Local cache
*
* @var array
*/
protected $isSaleableBySku = [];

/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
Expand Down Expand Up @@ -585,7 +592,7 @@ protected function getGalleryReadHandler()
* @param \Magento\Catalog\Model\Product $product
* @return \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Product\Collection
*/
public function getUsedProductCollection($product)
protected function getLinkedProductCollection($product)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello and thank you for your contribution. Just a question: what's the reason behind the introduction of this new method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aleron75
as described in #26065

the swatches plugin is not needed Magento\Swatches\Model\Plugin\Configurable::afterGetUsedProductCollection
so isSaleable can call internal method. It slightly improves performance if swatches are disabled on category page (listing page). Before, when the code called isSaleable it was always called afterGetUsedProductCollection that just adds attributes to select, however they are not needed in this check.
It also helps if isSaleable is called a few times in different places

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, @ilnytskyi I didn't explain myself. I got the point of the optimization, I was just wondering why did you introduce the additional getLinkedProductCollection() method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, to avoid code duplication inside isSalable. (collection building)
getUsedProductCollection is used inside isSalable so i decided to use it as wrapper for the collection building then move its logic to the new method. This way
getUsedProductCollection can be used as previously and
getLinkedProductCollection only internal method that is not affected by plugins

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok great, thank you!

{
$collection = $this->_productCollectionFactory->create()->setFlag(
'product_children',
Expand All @@ -600,6 +607,17 @@ public function getUsedProductCollection($product)
return $collection;
}

/**
* Retrieve related products collection. Extension point for listing
*
* @param \Magento\Catalog\Model\Product $product
* @return \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Product\Collection
*/
public function getUsedProductCollection($product)
{
return $this->getLinkedProductCollection($product);
}

/**
* Before save process
*
Expand Down Expand Up @@ -744,15 +762,27 @@ private function saveRelatedProducts(ProductInterface $product)
*/
public function isSalable($product)
{
$storeId = $this->getStoreFilter($product);
if ($storeId instanceof \Magento\Store\Model\Store) {
$storeId = $storeId->getId();
}

$sku = $product->getSku();
if (isset($this->isSaleableBySku[$storeId][$sku])) {
return $this->isSaleableBySku[$storeId][$sku];
}

$salable = parent::isSalable($product);

if ($salable !== false) {
$collection = $this->getUsedProductCollection($product);
$collection->addStoreFilter($this->getStoreFilter($product));
$collection = $this->getLinkedProductCollection($product);
$collection->addStoreFilter($storeId);
$collection = $this->salableProcessor->process($collection);
$salable = 0 !== $collection->getSize();
}

$this->isSaleableBySku[$storeId][$sku] = $salable;

return $salable;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -567,12 +567,18 @@ public function testHasOptionsFalse()
public function testIsSalable()
{
$productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
->setMethods(['__wakeup', 'getStatus', 'hasData', 'getData', 'getStoreId', 'setData'])
->setMethods(['__wakeup', 'getStatus', 'hasData', 'getData', 'getStoreId', 'setData', 'getSku'])
->disableOriginalConstructor()
->getMock();
$productMock
->expects($this->at(0))
->method('getData')
->with('_cache_instance_store_filter')
->willReturn(0);
$productMock->expects($this->once())->method('getStatus')->willReturn(1);
$productMock->expects($this->any())->method('hasData')->willReturn(true);
$productMock->expects($this->at(2))->method('getData')->with('is_salable')->willReturn(true);
$productMock->expects($this->at(1))->method('getSku')->willReturn('SKU-CODE');
$productMock->expects($this->at(4))->method('getData')->with('is_salable')->willReturn(true);
$productCollection = $this->getMockBuilder(
\Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Product\Collection::class
)
Expand Down