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\Tax\Observer;
7 
11 
13 {
19  protected $taxData;
20 
24  protected $registry;
25 
30  public function __construct(
31  \Magento\Framework\Registry $registry,
32  \Magento\Tax\Helper\Data $taxData
33  ) {
34  $this->registry = $registry;
35  $this->taxData = $taxData;
36  }
37 
45  public function execute(\Magento\Framework\Event\Observer $observer)
46  {
47  if ($this->taxData->displayPriceIncludingTax()) {
49  $product = $this->registry->registry('current_product');
50  if ($product->getTypeId() == \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) {
51  $priceConfigObj = $observer->getData('configObj');
52  try {
53  $priceConfig = $this->recurConfigAndUpdatePrice(
54  $priceConfigObj->getConfig(),
55  'prices'
56  );
57  $priceConfigObj->setConfig($priceConfig);
58  } catch (\Exception $e) {
59  return $this;
60  }
61  }
62  }
63  return $this;
64  }
65 
73  private function recurConfigAndUpdatePrice($input, $searchKey)
74  {
75  $holder = [];
76  if (is_array($input)) {
77  foreach ($input as $key => $el) {
78  if (is_array($el)) {
79  $holder[$key] =
80  $this->recurConfigAndUpdatePrice($el, $searchKey);
81  if ($key === $searchKey) {
82  if ((array_key_exists('basePrice', $holder[$key]))) {
83  if (array_key_exists('optionId', $input)) {
84  $holder = $this->updatePriceForBundle($holder, $key);
85  }
86  }
87  }
88  } else {
89  $holder[$key] = $el;
90  }
91  }
92  }
93  return $holder;
94  }
95 
103  private function updatePriceForBundle($holder, $key)
104  {
105  if (array_key_exists($key, $holder)) {
106  if (array_key_exists('basePrice', $holder[$key])) {
108  $product = $this->registry->registry('current_product');
109  if ($product->getTypeId() == \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) {
110  $typeInstance = $product->getTypeInstance();
111  $typeInstance->setStoreFilter($product->getStoreId(), $product);
112 
113  $selectionCollection = $typeInstance->getSelectionsCollection(
114  $typeInstance->getOptionsIds($product),
115  $product
116  );
117 
118  foreach ($selectionCollection->getItems() as $selectionItem) {
119  if ($holder['optionId'] == $selectionItem->getId()) {
121  $baseAmount = $selectionItem->getPriceInfo()->getPrice(BasePrice::PRICE_CODE)->getAmount();
123  $oldAmount =
124  $selectionItem->getPriceInfo()->getPrice(RegularPrice::PRICE_CODE)->getAmount();
125  if ($baseAmount->hasAdjustment('tax')) {
126  $holder[$key]['basePrice']['amount'] =
127  $baseAmount->getBaseAmount() + $baseAmount->getAdjustmentAmount('tax');
128  $holder[$key]['oldPrice']['amount'] =
129  $oldAmount->getBaseAmount() + $oldAmount->getAdjustmentAmount('tax');
130  }
131  }
132  }
133  }
134  }
135  }
136  return $holder;
137  }
138 }
$baseAmount
Definition: tax.phtml:46
__construct(\Magento\Framework\Registry $registry, \Magento\Tax\Helper\Data $taxData)
$selectionCollection