/** * Get tier prices from option selection item * * @param Product $product * @param Product $selection * @return array */ private function getTierPrices(Product $product, Product $selection) { // recalculate currency $tierPrices = $selection->getPriceInfo()->getPrice(\Magento\Catalog\Pricing\Price\TierPrice::PRICE_CODE)->getTierPriceList(); foreach ($tierPrices as &$tierPriceInfo) { /** @var \Magento\Framework\Pricing\Amount\Base $price */ $price = $tierPriceInfo['price']; $priceBaseAmount = $price->getBaseAmount(); $priceValue = $price->getValue(); $bundleProductPrice = $this->productPriceFactory->create(); $priceBaseAmount = $bundleProductPrice->getLowestPrice($product, $priceBaseAmount); $priceValue = $bundleProductPrice->getLowestPrice($product, $priceValue); $tierPriceInfo['prices'] = ['oldPrice' => ['amount' => $priceBaseAmount], 'basePrice' => ['amount' => $priceBaseAmount], 'finalPrice' => ['amount' => $priceValue]]; } return $tierPrices; }
/** * Returns JSON encoded config to be used in JS scripts * * @return string * * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function getJsonConfig() { /** @var \Magento\Bundle\Model\Option[] $optionsArray */ $optionsArray = $this->getOptions(); $options = array(); $selected = array(); $currentProduct = $this->getProduct(); if ($preConfiguredFlag = $currentProduct->hasPreconfiguredValues()) { $preConfiguredValues = $currentProduct->getPreconfiguredValues(); $defaultValues = array(); } $position = 0; foreach ($optionsArray as $optionItem) { /* @var $optionItem \Magento\Bundle\Model\Option */ if (!$optionItem->getSelections()) { continue; } $optionId = $optionItem->getId(); $option = array('selections' => array(), 'title' => $optionItem->getTitle(), 'isMulti' => in_array($optionItem->getType(), array('multi', 'checkbox')), 'position' => $position++); $selectionCount = count($optionItem->getSelections()); foreach ($optionItem->getSelections() as $selectionItem) { /* @var $selectionItem \Magento\Catalog\Model\Product */ $selectionId = $selectionItem->getSelectionId(); $qty = !($selectionItem->getSelectionQty() * 1) ? '1' : $selectionItem->getSelectionQty() * 1; // recalculate currency $tierPrices = $selectionItem->getPriceInfo()->getPrice(\Magento\Catalog\Pricing\Price\TierPrice::PRICE_CODE)->getTierPriceList(); foreach ($tierPrices as &$tierPriceInfo) { /** @var \Magento\Framework\Pricing\Amount\Base $price */ $price = $tierPriceInfo['price']; $priceBaseAmount = $price->getBaseAmount(); $priceValue = $price->getValue(); $bundleProductPrice = $this->_productPrice->create(); $priceBaseAmount = $bundleProductPrice->getLowestPrice($currentProduct, $priceBaseAmount); $priceValue = $bundleProductPrice->getLowestPrice($currentProduct, $priceValue); $tierPriceInfo['price'] = $this->priceCurrency->convert($this->_taxData->displayPriceIncludingTax() ? $priceValue : $priceBaseAmount); $tierPriceInfo['exclTaxPrice'] = $this->priceCurrency->convert($priceBaseAmount); $tierPriceInfo['inclTaxPrice'] = $this->priceCurrency->convert($priceValue); } // break the reference with the last element $canApplyMAP = false; $bundleOptionPriceAmount = $currentProduct->getPriceInfo()->getPrice('bundle_option')->getOptionSelectionAmount($selectionItem); $priceInclTax = $bundleOptionPriceAmount->getValue(); $priceExclTax = $bundleOptionPriceAmount->getBaseAmount(); $selection = array('qty' => $qty, 'customQty' => $selectionItem->getSelectionCanChangeQty(), 'inclTaxPrice' => $this->priceCurrency->convert($priceInclTax), 'exclTaxPrice' => $this->priceCurrency->convert($priceExclTax), 'priceType' => $selectionItem->getSelectionPriceType(), 'tierPrice' => $tierPrices, 'name' => $selectionItem->getName(), 'plusDisposition' => 0, 'minusDisposition' => 0, 'canApplyMAP' => $canApplyMAP); $selection['price'] = $this->_taxData->displayPriceIncludingTax() ? $selection['inclTaxPrice'] : $selection['exclTaxPrice']; $responseObject = new \Magento\Framework\Object(); $args = array('response_object' => $responseObject, 'selection' => $selectionItem); $this->_eventManager->dispatch('bundle_product_view_config', $args); if (is_array($responseObject->getAdditionalOptions())) { foreach ($responseObject->getAdditionalOptions() as $index => $value) { $selection[$index] = $value; } } $option['selections'][$selectionId] = $selection; if (($selectionItem->getIsDefault() || $selectionCount == 1 && $optionItem->getRequired()) && $selectionItem->isSalable()) { $selected[$optionId][] = $selectionId; } } $options[$optionId] = $option; // Add attribute default value (if set) if ($preConfiguredFlag) { $configValue = $preConfiguredValues->getData('bundle_option/' . $optionId); if ($configValue) { $defaultValues[$optionId] = $configValue; } } } $isFixedPrice = $this->getProduct()->getPriceType() == \Magento\Bundle\Model\Product\Price::PRICE_TYPE_FIXED; $productAmount = $currentProduct->getPriceInfo()->getPrice(\Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE)->getAmount(); $baseProductAmount = $currentProduct->getPriceInfo()->getPrice(\Magento\Catalog\Pricing\Price\RegularPrice::PRICE_CODE)->getAmount(); $config = array('options' => $options, 'selected' => $selected, 'bundleId' => $currentProduct->getId(), 'priceFormat' => $this->_localeFormat->getPriceFormat(), 'basePrice' => $this->priceCurrency->convert($baseProductAmount->getValue()), 'finalBasePriceInclTax' => $isFixedPrice ? $this->priceCurrency->convert($productAmount->getValue()) : 0, 'finalBasePriceExclTax' => $isFixedPrice ? $this->priceCurrency->convert($productAmount->getBaseAmount()) : 0, 'priceType' => $currentProduct->getPriceType(), 'specialPrice' => $currentProduct->getPriceInfo()->getPrice(\Magento\Catalog\Pricing\Price\SpecialPrice::PRICE_CODE)->getValue(), 'includeTax' => $this->_taxData->priceIncludesTax() ? 'true' : 'false', 'isFixedPrice' => $isFixedPrice); $config['finalPrice'] = $this->_taxData->displayPriceIncludingTax() ? $config['finalBasePriceInclTax'] : $config['finalBasePriceExclTax']; if ($preConfiguredFlag && !empty($defaultValues)) { $config['defaultValues'] = $defaultValues; } return $this->jsonEncoder->encode($config); }