/**
  * Change default JavaScript templates for options rendering
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @return $this
  */
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     $response = $observer->getEvent()->getResponseObject();
     $options = $response->getAdditionalOptions();
     $_product = $this->registry->registry('current_product');
     if (!$_product) {
         return $this;
     }
     $algorithm = $this->taxData->getCalculationAlgorithm();
     $options['calculationAlgorithm'] = $algorithm;
     // prepare correct template for options render
     if ($this->taxData->displayBothPrices()) {
         $options['optionTemplate'] = sprintf('<%%= data.label %%>' . '<%% if (data.finalPrice.value) { %%>' . ' +<%%= data.finalPrice.formatted %%> (%1$s <%%= data.basePrice.formatted %%>)' . '<%% } %%>', __('Excl. tax:'));
     } elseif ($this->taxData->priceIncludesTax() && $this->taxData->displayPriceExcludingTax()) {
         $options['optionTemplate'] = sprintf('<%%= data.label %%>' . '<%% if (data.basePrice.value) { %%>' . ' +<%%= data.basePrice.formatted %%>' . '<%% } %%>');
     }
     $response->setAdditionalOptions($options);
     return $this;
 }
示例#2
0
 /**
  * Calculate amount for dynamic bundle product
  *
  * @param float $basePriceValue
  * @param Product $bundleProduct
  * @param \Magento\Bundle\Pricing\Price\BundleSelectionPrice[] $selectionPriceList
  * @param null|bool|string|array $exclude
  * @return \Magento\Framework\Pricing\Amount\AmountInterface
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 protected function calculateDynamicBundleAmount($basePriceValue, $bundleProduct, $selectionPriceList, $exclude)
 {
     $fullAmount = 0.0;
     $adjustments = [];
     $i = 0;
     $amountList[$i]['amount'] = $this->calculator->getAmount($basePriceValue, $bundleProduct, $exclude);
     $amountList[$i]['quantity'] = 1;
     foreach ($selectionPriceList as $selectionPrice) {
         ++$i;
         $amountList[$i]['amount'] = $selectionPrice->getAmount();
         // always honor the quantity given
         $amountList[$i]['quantity'] = $selectionPrice->getQuantity();
     }
     /** @var  Store $store */
     $store = $bundleProduct->getStore();
     $roundingMethod = $this->taxHelper->getCalculationAlgorithm($store);
     foreach ($amountList as $amountInfo) {
         /** @var \Magento\Framework\Pricing\Amount\AmountInterface $itemAmount */
         $itemAmount = $amountInfo['amount'];
         $qty = $amountInfo['quantity'];
         if ($roundingMethod != TaxCalculationInterface::CALC_TOTAL_BASE) {
             //We need to round the individual selection first
             $fullAmount += $this->priceCurrency->round($itemAmount->getValue()) * $qty;
             foreach ($itemAmount->getAdjustmentAmounts() as $code => $adjustment) {
                 $adjustment = $this->priceCurrency->round($adjustment) * $qty;
                 $adjustments[$code] = isset($adjustments[$code]) ? $adjustments[$code] + $adjustment : $adjustment;
             }
         } else {
             $fullAmount += $itemAmount->getValue() * $qty;
             foreach ($itemAmount->getAdjustmentAmounts() as $code => $adjustment) {
                 $adjustment = $adjustment * $qty;
                 $adjustments[$code] = isset($adjustments[$code]) ? $adjustments[$code] + $adjustment : $adjustment;
             }
         }
     }
     if (is_array($exclude) == false) {
         if ($exclude && isset($adjustments[$exclude])) {
             $fullAmount -= $adjustments[$exclude];
             unset($adjustments[$exclude]);
         }
     } else {
         foreach ($exclude as $oneExclusion) {
             if ($oneExclusion && isset($adjustments[$oneExclusion])) {
                 $fullAmount -= $adjustments[$oneExclusion];
                 unset($adjustments[$oneExclusion]);
             }
         }
     }
     return $this->amountFactory->create($fullAmount, $adjustments);
 }