Skip to content

Commit baf3cf0

Browse files
committed
Improve bundle load speeds
When a bundle has a lot of selection, the tax GetPriceConfigurationObserver becomes a real performance hog because it loads the selections over and over again. This fixes that by caching the selection in memory
1 parent 8dfe26a commit baf3cf0

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

app/code/Magento/Tax/Observer/GetPriceConfigurationObserver.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class GetPriceConfigurationObserver implements ObserverInterface
2323
*/
2424
protected $registry;
2525

26+
private $selectionCache = [];
27+
2628
/**
2729
* @param \Magento\Framework\Registry $registry
2830
* @param \Magento\Tax\Helper\Data $taxData
@@ -107,15 +109,19 @@ private function updatePriceForBundle($holder, $key)
107109
/** @var \Magento\Catalog\Model\Product $product */
108110
$product = $this->registry->registry('current_product');
109111
if ($product->getTypeId() == \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) {
110-
$typeInstance = $product->getTypeInstance();
111-
$typeInstance->setStoreFilter($product->getStoreId(), $product);
112+
if (!isset($this->selectionCache[$product->getId()])) {
113+
$typeInstance = $product->getTypeInstance();
114+
$typeInstance->setStoreFilter($product->getStoreId(), $product);
112115

113-
$selectionCollection = $typeInstance->getSelectionsCollection(
114-
$typeInstance->getOptionsIds($product),
115-
$product
116-
);
116+
$selectionCollection = $typeInstance->getSelectionsCollection(
117+
$typeInstance->getOptionsIds($product),
118+
$product
119+
);
120+
$this->selectionCache[$product->getId()] = $selectionCollection->getItems();
121+
}
122+
$arrSelections = $this->selectionCache[$product->getId()];
117123

118-
foreach ($selectionCollection->getItems() as $selectionItem) {
124+
foreach ($arrSelections as $selectionItem) {
119125
if ($holder['optionId'] == $selectionItem->getId()) {
120126
/** @var \Magento\Framework\Pricing\Amount\Base $baseAmount */
121127
$baseAmount = $selectionItem->getPriceInfo()->getPrice(BasePrice::PRICE_CODE)->getAmount();

0 commit comments

Comments
 (0)