|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2016 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Tax\Observer; |
| 7 | + |
| 8 | +use Magento\Framework\Event\ObserverInterface; |
| 9 | +use Magento\Catalog\Pricing\Price\BasePrice; |
| 10 | +use Magento\Catalog\Pricing\Price\RegularPrice; |
| 11 | + |
| 12 | +class GetPriceConfigurationObserver implements ObserverInterface |
| 13 | +{ |
| 14 | + /** |
| 15 | + * Tax data |
| 16 | + * |
| 17 | + * @var \Magento\Tax\Helper\Data |
| 18 | + */ |
| 19 | + protected $taxData; |
| 20 | + |
| 21 | + /** @var \Magento\Framework\Registry */ |
| 22 | + protected $registry; |
| 23 | + |
| 24 | + /** |
| 25 | + * @param \Magento\Framework\Registry $registry |
| 26 | + * @param \Magento\Tax\Helper\Data $taxData |
| 27 | + */ |
| 28 | + public function __construct( |
| 29 | + \Magento\Framework\Registry $registry, |
| 30 | + \Magento\Tax\Helper\Data $taxData |
| 31 | + ) { |
| 32 | + $this->registry = $registry; |
| 33 | + $this->taxData = $taxData; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Modify the bundle config for the front end to resemble the tax included price when tax included prices |
| 38 | + * |
| 39 | + * @param \Magento\Framework\Event\Observer $observer |
| 40 | + * @return $this |
| 41 | + * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
| 42 | + */ |
| 43 | + public function execute(\Magento\Framework\Event\Observer $observer) |
| 44 | + { |
| 45 | + if ($this->taxData->displayPriceIncludingTax()) { |
| 46 | + /** @var \Magento\Catalog\Model\Product $product */ |
| 47 | + $product = $this->registry->registry('current_product'); |
| 48 | + if ($product->getTypeId() == \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) { |
| 49 | + $priceConfigObj = $observer->getData('configObj'); |
| 50 | + try { |
| 51 | + $priceConfig = $this->recurConfigAndUpdatePrice( |
| 52 | + $priceConfigObj->getConfig(), |
| 53 | + 'prices' |
| 54 | + ); |
| 55 | + $priceConfigObj->setConfig($priceConfig); |
| 56 | + } catch (\Exception $e) { |
| 57 | + return $this; |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + return $this; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Recurse through the config array and modify the base price |
| 66 | + * |
| 67 | + * @param array $input |
| 68 | + * @param string $searchKey |
| 69 | + * @return array |
| 70 | + */ |
| 71 | + private function recurConfigAndUpdatePrice($input, $searchKey) |
| 72 | + { |
| 73 | + $holder = []; |
| 74 | + if (is_array($input)) { |
| 75 | + foreach ($input as $key => $el) { |
| 76 | + if (is_array($el)) { |
| 77 | + $holder[$key] = |
| 78 | + $this->recurConfigAndUpdatePrice($el, $searchKey); |
| 79 | + if ($key === $searchKey) { |
| 80 | + if ((array_key_exists('basePrice', $holder[$key]))) { |
| 81 | + if (array_key_exists('optionId', $input)) { |
| 82 | + $holder = $this->updatePriceForBundle($holder, $key); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } else { |
| 87 | + $holder[$key] = $el; |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + return $holder; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Update the base price for bundle product option |
| 96 | + * |
| 97 | + * @param array $holder |
| 98 | + * @param int|string $key |
| 99 | + * @return array |
| 100 | + */ |
| 101 | + private function updatePriceForBundle($holder, $key) |
| 102 | + { |
| 103 | + if (array_key_exists($key, $holder)) { |
| 104 | + if (array_key_exists('basePrice', $holder[$key])) { |
| 105 | + /** @var \Magento\Catalog\Model\Product $product */ |
| 106 | + $product = $this->registry->registry('current_product'); |
| 107 | + if ($product->getTypeId() == \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) { |
| 108 | + $typeInstance = $product->getTypeInstance(); |
| 109 | + $typeInstance->setStoreFilter($product->getStoreId(), $product); |
| 110 | + |
| 111 | + $selectionCollection = $typeInstance->getSelectionsCollection( |
| 112 | + $typeInstance->getOptionsIds($product), |
| 113 | + $product |
| 114 | + ); |
| 115 | + |
| 116 | + foreach ($selectionCollection->getItems() as $selectionItem) { |
| 117 | + if ($holder['optionId'] == $selectionItem->getId()) { |
| 118 | + /** @var \Magento\Framework\Pricing\Amount\Base $baseAmount */ |
| 119 | + $baseAmount = $selectionItem->getPriceInfo()->getPrice(BasePrice::PRICE_CODE)->getAmount(); |
| 120 | + /** @var \Magento\Framework\Pricing\Amount\Base $oldAmount */ |
| 121 | + $oldAmount = |
| 122 | + $selectionItem->getPriceInfo()->getPrice(RegularPrice::PRICE_CODE)->getAmount(); |
| 123 | + if ($baseAmount->hasAdjustment('tax')) { |
| 124 | + $holder[$key]['basePrice']['amount'] = |
| 125 | + $baseAmount->getBaseAmount() + $baseAmount->getAdjustmentAmount('tax'); |
| 126 | + $holder[$key]['oldPrice']['amount'] = |
| 127 | + $oldAmount->getBaseAmount() + $oldAmount->getAdjustmentAmount('tax'); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + return $holder; |
| 135 | + } |
| 136 | +} |
0 commit comments