Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
GetPriceConfigurationObserver.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Weee\Observer;
7 
9 
11 {
17  protected $taxData;
18 
24  protected $weeeData;
25 
29  protected $registry;
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->registry = $registry;
42  $this->taxData = $taxData;
43  $this->weeeData = $weeeData;
44  }
45 
53  public function execute(\Magento\Framework\Event\Observer $observer)
54  {
55  if ($this->weeeData->isEnabled()) {
56  $priceConfigObj = $observer->getData('configObj');
57  try {
59  $product = $this->registry->registry('current_product');
60  $weeeAttributesForBundle = $this->weeeData->getWeeeAttributesForBundle($product);
61  $priceConfig = $this->recurConfigAndInsertWeeePrice(
62  $priceConfigObj->getConfig(),
63  'prices',
64  $this->getWhichCalcPriceToUse($product->getStoreId(), $weeeAttributesForBundle),
65  $weeeAttributesForBundle
66  );
67  $priceConfigObj->setConfig($priceConfig);
68  } catch (\Exception $e) {
69  return $this;
70  }
71  }
72  return $this;
73  }
74 
84  private function recurConfigAndInsertWeeePrice($input, $searchKey, $calcPrice, $weeeAttributesForBundle = null)
85  {
86  $holder = [];
87  if (is_array($input)) {
88  foreach ($input as $key => $el) {
89  if (is_array($el)) {
90  $holder[$key] =
91  $this->recurConfigAndInsertWeeePrice($el, $searchKey, $calcPrice, $weeeAttributesForBundle);
92  if ($key === $searchKey) {
93  if ((!array_key_exists('weeePrice', $holder[$key])) &&
94  (array_key_exists($calcPrice, $holder[$key]))
95  ) {
96  //this is required for product options && bundle
97  $holder[$key]['weeePrice'] = $holder[$key][$calcPrice];
98  // only do processing on product options
99  if (array_key_exists('optionId', $input) && $weeeAttributesForBundle) {
100  $holder = $this->insertWeeePrice($holder, $key, $weeeAttributesForBundle);
101  }
102  }
103  }
104  } else {
105  $holder[$key] = $el;
106  }
107  }
108  }
109  return $holder;
110  }
111 
120  private function insertWeeePrice($holder, $key, $weeeAttributesForBundle)
121  {
122  if (array_key_exists($holder['optionId'], $weeeAttributesForBundle)) {
123  if (count($weeeAttributesForBundle[$holder['optionId']]) > 0 &&
124  is_array($weeeAttributesForBundle[$holder['optionId']])
125  ) {
126  $weeeSum = 0;
127  foreach ($weeeAttributesForBundle[$holder['optionId']] as $weeeAttribute) {
128  $holder[$key]['weeePrice' . $weeeAttribute->getCode()] =
129  ['amount' => (float)$weeeAttribute->getAmount()];
130  $weeeSum += (float)$weeeAttribute->getAmount();
131  }
132  $holder[$key]['weeePrice']['amount'] += (float)$weeeSum;
133  } else {
134  //there were no Weee attributes for this option
135  unset($holder[$key]['weeePrice']);
136  }
137  }
138  return $holder;
139  }
140 
148  protected function getWhichCalcPriceToUse($storeId = null, $weeeAttributesForBundle = null)
149  {
150  $calcPrice = 'finalPrice';
151  if (!empty($weeeAttributesForBundle)) {
152  if ($this->weeeData->isDisplayExcl($storeId) ||
153  $this->weeeData->isDisplayExclDescIncl($storeId) ||
154  ($this->taxData->priceIncludesTax() && $this->taxData->displayPriceExcludingTax())
155  ) {
156  $calcPrice = 'basePrice';
157  }
158  }
159  return $calcPrice;
160  }
161 }
__construct(\Magento\Framework\Registry $registry, \Magento\Weee\Helper\Data $weeeData, \Magento\Tax\Helper\Data $taxData)
getWhichCalcPriceToUse($storeId=null, $weeeAttributesForBundle=null)