Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
UpdateProductOptionsObserver.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Weee\Observer;
7 
9 
11 {
17  protected $weeeData = null;
18 
22  protected $registry;
23 
29  protected $taxData;
30 
36  public function __construct(
37  \Magento\Framework\Registry $registry,
38  \Magento\Weee\Helper\Data $weeeData,
39  \Magento\Tax\Helper\Data $taxData
40  ) {
41  $this->weeeData = $weeeData;
42  $this->registry = $registry;
43  $this->taxData = $taxData;
44  }
45 
53  public function execute(\Magento\Framework\Event\Observer $observer)
54  {
55  $response = $observer->getEvent()->getResponseObject();
56  $options = $response->getAdditionalOptions();
57 
59  $product = $this->registry->registry('current_product');
60  if (!$product) {
61  return $this;
62  }
63 
64  // if the Weee module is enabled, then only do processing on bundle products
65  if ($this->weeeData->isEnabled() && $product->getTypeId() == \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) {
66  if ($this->taxData->priceIncludesTax() && $this->taxData->displayPriceExcludingTax()) {
67  // the Tax module might have set up a default, but we will re-decide which calcPrice field to use
68  unset($options['optionTemplate']);
69  }
70 
71  if (!array_key_exists('optionTemplate', $options)) {
72  $calcPrice = $this->getWhichCalcPriceToUse($product->getStoreId());
73  $options['optionTemplate'] = '<%- data.label %>'
74  . '<% if (data.' . $calcPrice . '.value) { %>'
75  . ' +<%- data.' . $calcPrice . '.formatted %>'
76  . '<% } %>';
77  }
78 
79  if (!$this->weeeData->isDisplayIncl($product->getStoreId()) &&
80  !$this->weeeData->isDisplayExcl($product->getStoreId())) {
81  // we need to display the individual Weee amounts
82  foreach ($this->weeeData->getWeeeAttributesForBundle($product) as $weeeAttributes) {
83  foreach ($weeeAttributes as $weeeAttribute) {
84  if (!preg_match('/' . $weeeAttribute->getCode() . '/', $options['optionTemplate'])) {
85  $options['optionTemplate'] .= sprintf(
86  ' <%% if (data.weeePrice' . $weeeAttribute->getCode() . ') { %%>'
87  . ' (' . $weeeAttribute->getName()
88  . ': <%%- data.weeePrice' . $weeeAttribute->getCode()
89  . '.formatted %%>)'
90  . '<%% } %%>'
91  );
92  }
93  }
94  }
95  }
96 
97  if ($this->weeeData->isDisplayExclDescIncl($product->getStoreId())) {
98  $options['optionTemplate'] .= sprintf(
99  ' <%% if (data.weeePrice) { %%>'
100  . '<%%- data.weeePrice.formatted %%>'
101  . '<%% } %%>'
102  );
103  }
104  }
105  $response->setAdditionalOptions($options);
106  return $this;
107  }
108 
115  protected function getWhichCalcPriceToUse($storeId = null)
116  {
117  $calcPrice = 'finalPrice';
118 
119  if ($this->weeeData->isDisplayExclDescIncl($storeId) ||
120  ($this->weeeData->isDisplayExcl($storeId) && $this->taxData->displayPriceExcludingTax())) {
121  $calcPrice = 'basePrice';
122  }
123  return $calcPrice;
124  }
125 }
__construct(\Magento\Framework\Registry $registry, \Magento\Weee\Helper\Data $weeeData, \Magento\Tax\Helper\Data $taxData)
$response
Definition: 404.php:11