UpdateProductOptionsObserver.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 UpdateProductOptionsObserver implements ObserverInterface
  9. {
  10. /**
  11. * Weee data
  12. *
  13. * @var \Magento\Weee\Helper\Data
  14. */
  15. protected $weeeData = null;
  16. /**
  17. * @var \Magento\Framework\Registry
  18. */
  19. protected $registry;
  20. /**
  21. * Tax data
  22. *
  23. * @var \Magento\Tax\Helper\Data
  24. */
  25. protected $taxData;
  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->weeeData = $weeeData;
  37. $this->registry = $registry;
  38. $this->taxData = $taxData;
  39. }
  40. /**
  41. * Change default JavaScript templates for options rendering
  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. $response = $observer->getEvent()->getResponseObject();
  50. $options = $response->getAdditionalOptions();
  51. /** @var \Magento\Catalog\Model\Product $product */
  52. $product = $this->registry->registry('current_product');
  53. if (!$product) {
  54. return $this;
  55. }
  56. // if the Weee module is enabled, then only do processing on bundle products
  57. if ($this->weeeData->isEnabled() && $product->getTypeId() == \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) {
  58. if ($this->taxData->priceIncludesTax() && $this->taxData->displayPriceExcludingTax()) {
  59. // the Tax module might have set up a default, but we will re-decide which calcPrice field to use
  60. unset($options['optionTemplate']);
  61. }
  62. if (!array_key_exists('optionTemplate', $options)) {
  63. $calcPrice = $this->getWhichCalcPriceToUse($product->getStoreId());
  64. $options['optionTemplate'] = '<%- data.label %>'
  65. . '<% if (data.' . $calcPrice . '.value) { %>'
  66. . ' +<%- data.' . $calcPrice . '.formatted %>'
  67. . '<% } %>';
  68. }
  69. if (!$this->weeeData->isDisplayIncl($product->getStoreId()) &&
  70. !$this->weeeData->isDisplayExcl($product->getStoreId())) {
  71. // we need to display the individual Weee amounts
  72. foreach ($this->weeeData->getWeeeAttributesForBundle($product) as $weeeAttributes) {
  73. foreach ($weeeAttributes as $weeeAttribute) {
  74. if (!preg_match('/' . $weeeAttribute->getCode() . '/', $options['optionTemplate'])) {
  75. $options['optionTemplate'] .= sprintf(
  76. ' <%% if (data.weeePrice' . $weeeAttribute->getCode() . ') { %%>'
  77. . ' (' . $weeeAttribute->getName()
  78. . ': <%%- data.weeePrice' . $weeeAttribute->getCode()
  79. . '.formatted %%>)'
  80. . '<%% } %%>'
  81. );
  82. }
  83. }
  84. }
  85. }
  86. if ($this->weeeData->isDisplayExclDescIncl($product->getStoreId())) {
  87. $options['optionTemplate'] .= sprintf(
  88. ' <%% if (data.weeePrice) { %%>'
  89. . '<%%- data.weeePrice.formatted %%>'
  90. . '<%% } %%>'
  91. );
  92. }
  93. }
  94. $response->setAdditionalOptions($options);
  95. return $this;
  96. }
  97. /**
  98. * Returns which product price to show (before listing the individual Weee amounts, if applicable)
  99. *
  100. * @param int|null $storeId
  101. * @return string
  102. */
  103. protected function getWhichCalcPriceToUse($storeId = null)
  104. {
  105. $calcPrice = 'finalPrice';
  106. if ($this->weeeData->isDisplayExclDescIncl($storeId) ||
  107. ($this->weeeData->isDisplayExcl($storeId) && $this->taxData->displayPriceExcludingTax())) {
  108. $calcPrice = 'basePrice';
  109. }
  110. return $calcPrice;
  111. }
  112. }