Skip to content

Improve bundle load speeds #20877

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 7 commits into from
Aug 19, 2019
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
55 changes: 33 additions & 22 deletions app/code/Magento/Tax/Observer/GetPriceConfigurationObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
*/
namespace Magento\Tax\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Catalog\Pricing\Price\BasePrice;
use Magento\Catalog\Pricing\Price\RegularPrice;
use Magento\Framework\Event\ObserverInterface;

/**
* Modifies the bundle config for the front end to resemble the tax included price when tax included prices.
*/
class GetPriceConfigurationObserver implements ObserverInterface
{
/**
Expand All @@ -23,6 +26,11 @@ class GetPriceConfigurationObserver implements ObserverInterface
*/
protected $registry;

/**
* @var array Cache of the current bundle selection items
*/
private $selectionCache = [];

/**
* @param \Magento\Framework\Registry $registry
* @param \Magento\Tax\Helper\Data $taxData
Expand All @@ -44,6 +52,7 @@ public function __construct(
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$this->selectionCache = [];
if ($this->taxData->displayPriceIncludingTax()) {
/** @var \Magento\Catalog\Model\Product $product */
$product = $this->registry->registry('current_product');
Expand Down Expand Up @@ -78,12 +87,11 @@ private function recurConfigAndUpdatePrice($input, $searchKey)
if (is_array($el)) {
$holder[$key] =
$this->recurConfigAndUpdatePrice($el, $searchKey);
if ($key === $searchKey) {
if ((array_key_exists('basePrice', $holder[$key]))) {
if (array_key_exists('optionId', $input)) {
$holder = $this->updatePriceForBundle($holder, $key);
}
}
if ($key === $searchKey
&& array_key_exists('optionId', $input)
&& array_key_exists('basePrice', $holder[$key])
) {
$holder = $this->updatePriceForBundle($holder, $key);
}
} else {
$holder[$key] = $el;
Expand All @@ -102,32 +110,35 @@ private function recurConfigAndUpdatePrice($input, $searchKey)
*/
private function updatePriceForBundle($holder, $key)
{
if (array_key_exists($key, $holder)) {
if (array_key_exists('basePrice', $holder[$key])) {
/** @var \Magento\Catalog\Model\Product $product */
$product = $this->registry->registry('current_product');
if ($product->getTypeId() == \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) {
if (array_key_exists($key, $holder)
&& array_key_exists('basePrice', $holder[$key])) {
/** @var \Magento\Catalog\Model\Product $product */
$product = $this->registry->registry('current_product');
if ($product->getTypeId() == \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) {
if (!isset($this->selectionCache[$product->getId()])) {
$typeInstance = $product->getTypeInstance();
$typeInstance->setStoreFilter($product->getStoreId(), $product);

$selectionCollection = $typeInstance->getSelectionsCollection(
$typeInstance->getOptionsIds($product),
$product
);
$this->selectionCache[$product->getId()] = $selectionCollection->getItems();
}
$arrSelections = $this->selectionCache[$product->getId()];

foreach ($selectionCollection->getItems() as $selectionItem) {
if ($holder['optionId'] == $selectionItem->getId()) {
/** @var \Magento\Framework\Pricing\Amount\Base $baseAmount */
$baseAmount = $selectionItem->getPriceInfo()->getPrice(BasePrice::PRICE_CODE)->getAmount();
/** @var \Magento\Framework\Pricing\Amount\Base $oldAmount */
$oldAmount =
foreach ($arrSelections as $selectionItem) {
if ($holder['optionId'] == $selectionItem->getId()) {
/** @var \Magento\Framework\Pricing\Amount\Base $baseAmount */
$baseAmount = $selectionItem->getPriceInfo()->getPrice(BasePrice::PRICE_CODE)->getAmount();
/** @var \Magento\Framework\Pricing\Amount\Base $oldAmount */
$oldAmount =
$selectionItem->getPriceInfo()->getPrice(RegularPrice::PRICE_CODE)->getAmount();
if ($baseAmount->hasAdjustment('tax')) {
$holder[$key]['basePrice']['amount'] =
if ($baseAmount->hasAdjustment('tax')) {
$holder[$key]['basePrice']['amount'] =
$baseAmount->getBaseAmount() + $baseAmount->getAdjustmentAmount('tax');
$holder[$key]['oldPrice']['amount'] =
$holder[$key]['oldPrice']['amount'] =
$oldAmount->getBaseAmount() + $oldAmount->getAdjustmentAmount('tax');
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Tax\Test\Unit\Observer;

use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
Expand Down Expand Up @@ -118,7 +119,7 @@ public function testExecute($testArray, $expectedArray)

$product = $this->createPartialMock(
\Magento\Bundle\Model\Product\Type::class,
['getTypeInstance', 'getTypeId', 'getStoreId', 'getSelectionsCollection']
['getTypeInstance', 'getTypeId', 'getStoreId', 'getSelectionsCollection', 'getId']
);
$product->expects($this->any())
->method('getTypeInstance')
Expand Down