GetPriceConfigurationObserver.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Weee\Observer;
  7. use Magento\Framework\Event\ObserverInterface;
  8. class GetPriceConfigurationObserver implements ObserverInterface
  9. {
  10. /**
  11. * Tax data
  12. *
  13. * @var \Magento\Tax\Helper\Data
  14. */
  15. protected $taxData;
  16. /**
  17. * Weee data
  18. *
  19. * @var \Magento\Weee\Helper\Data
  20. */
  21. protected $weeeData;
  22. /**
  23. * @var \Magento\Framework\Registry
  24. */
  25. protected $registry;
  26. /**
  27. * @param \Magento\Framework\Registry $registry
  28. * @param \Magento\Weee\Helper\Data $weeeData
  29. * @param \Magento\Tax\Helper\Data $taxData
  30. */
  31. public function __construct(
  32. \Magento\Framework\Registry $registry,
  33. \Magento\Weee\Helper\Data $weeeData,
  34. \Magento\Tax\Helper\Data $taxData
  35. ) {
  36. $this->registry = $registry;
  37. $this->taxData = $taxData;
  38. $this->weeeData = $weeeData;
  39. }
  40. /**
  41. * Modify the options config for the front end to resemble the weee final price
  42. *
  43. * @param \Magento\Framework\Event\Observer $observer
  44. * @return $this
  45. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  46. */
  47. public function execute(\Magento\Framework\Event\Observer $observer)
  48. {
  49. if ($this->weeeData->isEnabled()) {
  50. $priceConfigObj = $observer->getData('configObj');
  51. try {
  52. /** @var \Magento\Catalog\Model\Product $product */
  53. $product = $this->registry->registry('current_product');
  54. $weeeAttributesForBundle = $this->weeeData->getWeeeAttributesForBundle($product);
  55. $priceConfig = $this->recurConfigAndInsertWeeePrice(
  56. $priceConfigObj->getConfig(),
  57. 'prices',
  58. $this->getWhichCalcPriceToUse($product->getStoreId(), $weeeAttributesForBundle),
  59. $weeeAttributesForBundle
  60. );
  61. $priceConfigObj->setConfig($priceConfig);
  62. } catch (\Exception $e) {
  63. return $this;
  64. }
  65. }
  66. return $this;
  67. }
  68. /**
  69. * Recurse through the config array and insert the weee price
  70. *
  71. * @param array $input
  72. * @param string $searchKey
  73. * @param string $calcPrice
  74. * @param array $weeeAttributesForBundle
  75. * @return array
  76. */
  77. private function recurConfigAndInsertWeeePrice($input, $searchKey, $calcPrice, $weeeAttributesForBundle = null)
  78. {
  79. $holder = [];
  80. if (is_array($input)) {
  81. foreach ($input as $key => $el) {
  82. if (is_array($el)) {
  83. $holder[$key] =
  84. $this->recurConfigAndInsertWeeePrice($el, $searchKey, $calcPrice, $weeeAttributesForBundle);
  85. if ($key === $searchKey) {
  86. if ((!array_key_exists('weeePrice', $holder[$key])) &&
  87. (array_key_exists($calcPrice, $holder[$key]))
  88. ) {
  89. //this is required for product options && bundle
  90. $holder[$key]['weeePrice'] = $holder[$key][$calcPrice];
  91. // only do processing on product options
  92. if (array_key_exists('optionId', $input) && $weeeAttributesForBundle) {
  93. $holder = $this->insertWeeePrice($holder, $key, $weeeAttributesForBundle);
  94. }
  95. }
  96. }
  97. } else {
  98. $holder[$key] = $el;
  99. }
  100. }
  101. }
  102. return $holder;
  103. }
  104. /**
  105. * Insert the weee price for bundle product
  106. *
  107. * @param array $holder
  108. * @param int|string $key
  109. * @param array $weeeAttributesForBundle
  110. * @return array
  111. */
  112. private function insertWeeePrice($holder, $key, $weeeAttributesForBundle)
  113. {
  114. if (array_key_exists($holder['optionId'], $weeeAttributesForBundle)) {
  115. if (count($weeeAttributesForBundle[$holder['optionId']]) > 0 &&
  116. is_array($weeeAttributesForBundle[$holder['optionId']])
  117. ) {
  118. $weeeSum = 0;
  119. foreach ($weeeAttributesForBundle[$holder['optionId']] as $weeeAttribute) {
  120. $holder[$key]['weeePrice' . $weeeAttribute->getCode()] =
  121. ['amount' => (float)$weeeAttribute->getAmount()];
  122. $weeeSum += (float)$weeeAttribute->getAmount();
  123. }
  124. $holder[$key]['weeePrice']['amount'] += (float)$weeeSum;
  125. } else {
  126. //there were no Weee attributes for this option
  127. unset($holder[$key]['weeePrice']);
  128. }
  129. }
  130. return $holder;
  131. }
  132. /**
  133. * Returns which product price to use as a basis for the Weee's final price
  134. *
  135. * @param int|null $storeId
  136. * @param array|null $weeeAttributesForBundle
  137. * @return string
  138. */
  139. protected function getWhichCalcPriceToUse($storeId = null, $weeeAttributesForBundle = null)
  140. {
  141. $calcPrice = 'finalPrice';
  142. if (!empty($weeeAttributesForBundle)) {
  143. if ($this->weeeData->isDisplayExcl($storeId) ||
  144. $this->weeeData->isDisplayExclDescIncl($storeId) ||
  145. ($this->taxData->priceIncludesTax() && $this->taxData->displayPriceExcludingTax())
  146. ) {
  147. $calcPrice = 'basePrice';
  148. }
  149. }
  150. return $calcPrice;
  151. }
  152. }