public function testGetJsonConfigFixedPriceBundle() { $baseAmount = 123; $basePriceValue = 123123; $selections = [$this->createOptionSelection(1123, 'Selection 1', 23, [['price' => new MagentoObject(['base_amount' => $baseAmount, 'value' => $basePriceValue])]], true, true)]; $bundleProductPrice = $this->getMockBuilder('\\Magento\\Bundle\\Model\\Product\\Price')->disableOriginalConstructor()->setMethods(['getLowestPrice'])->getMock(); $bundleProductPrice->expects($this->at(0))->method('getLowestPrice')->with($this->product, $baseAmount)->will($this->returnValue(999)); $bundleProductPrice->expects($this->at(1))->method('getLowestPrice')->with($this->product, $basePriceValue)->will($this->returnValue(888)); $this->bundleProductPriceFactory->expects($this->once())->method('create')->will($this->returnValue($bundleProductPrice)); $options = [$this->createOption(1, 'Title `1', $selections)]; $finalPriceMock = $this->getPriceMock(['getPriceWithoutOption' => new MagentoObject(['value' => 100, 'base_amount' => 100])]); $regularPriceMock = $this->getPriceMock(['getAmount' => new MagentoObject(['value' => 110, 'base_amount' => 110])]); $prices = ['bundle_option' => $this->getAmountPriceMock($baseAmount, $regularPriceMock, [['item' => $selections[0], 'value' => $basePriceValue, 'base_amount' => 321321]]), \Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE => $finalPriceMock, \Magento\Catalog\Pricing\Price\RegularPrice::PRICE_CODE => $regularPriceMock]; $priceInfo = $this->getPriceInfoMock($prices); $this->product->expects($this->once())->method('hasPreconfiguredValues')->will($this->returnValue(true)); $preconfiguredValues = new \Magento\Framework\DataObject(['bundle_option' => [1 => 123123111]]); $this->product->expects($this->once())->method('getPreconfiguredValues')->will($this->returnValue($preconfiguredValues)); $this->_bundleBlock = $this->setupBundleBlock($options, $priceInfo, \Magento\Bundle\Model\Product\Price::PRICE_TYPE_FIXED); $jsonConfig = $this->_bundleBlock->getJsonConfig(); $this->assertEquals(110, $jsonConfig['prices']['oldPrice']['amount']); $this->assertEquals(100, $jsonConfig['prices']['basePrice']['amount']); $this->assertEquals(100, $jsonConfig['prices']['finalPrice']['amount']); }
/** * 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); }