Skip to content
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

Take required product attributes into account when caching used products #32698

Closed
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 @@ -1290,13 +1290,19 @@ public function isPossibleBuyFromList($product)
*/
public function getUsedProducts($product, $requiredAttributeIds = null)
{
if (!$product->hasData($this->_usedProducts)) {
$requiredAttributeIds = $requiredAttributeIds ?: [];
array_unshift($requiredAttributeIds, $product->getId());

$cacheKey = $this->getUsedProductsCacheKey($requiredAttributeIds);
if (!$product->hasData($this->_usedProducts) || !isset($product->getData($this->_usedProducts)[$cacheKey])) {
$collection = $this->getConfiguredUsedProductCollection($product, false, $requiredAttributeIds);
$usedProducts = array_values($collection->getItems());
$product->setData($this->_usedProducts, $usedProducts);
$usedProductsCache = $product->getData($this->_usedProducts);
$usedProductsCache[$cacheKey] = $usedProducts;
$product->setData($this->_usedProducts, $usedProductsCache);
}

return $product->getData($this->_usedProducts);
return $product->getData($this->_usedProducts)[$cacheKey];
Comment on lines +1293 to +1305
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe that this method could be reverted to 2487db1b#diff-d9457baecd561c1374a935c202ad3a4ebf82a9c26b80c486dd97ac44da81d383L1245-L1257 with some changes:

  • use $this->customerSession instead of deprecated getCustomerSession()
  • add $requiredAttributeIds into loadUsedProducts call

And refactoring loadUsedProducts method

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,24 @@ public function testGetUsedProductsWithoutRequiredAttributes()
}
}

/**
* @magentoAppIsolation enabled
* @magentoDataFixture Magento/ConfigurableProduct/_files/product_configurable_with_metadescription.php
*/
public function testGetUsedProductsWithoutAndWithRequiredAttributes()
{
$products = $this->model->getUsedProducts($this->product);
foreach ($products as $product) {
self::assertNull($product->getData('meta_description'));
}

$requiredAttributeIds = [86];
$products = $this->model->getUsedProducts($this->product, $requiredAttributeIds);
foreach ($products as $product) {
self::assertNotNull($product->getData('meta_description'));
}
}

/**
* Test getUsedProducts returns array with same indexes regardless collections was cache or not.
*
Expand Down