GetPriceConfigurationObserver.php 4.9 KB

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