/** * Get the price value for one of selection product * * @return bool|float */ public function getValue() { if (null !== $this->value) { return $this->value; } $priceCode = $this->useRegularPrice ? BundleRegularPrice::PRICE_CODE : FinalPrice::PRICE_CODE; if ($this->bundleProduct->getPriceType() == Price::PRICE_TYPE_DYNAMIC) { // just return whatever the product's value is $value = $this->priceInfo->getPrice($priceCode)->getValue(); } else { // don't multiply by quantity. Instead just keep as quantity = 1 $selectionPriceValue = $this->selection->getSelectionPriceValue(); if ($this->product->getSelectionPriceType()) { // calculate price for selection type percent $price = $this->bundleProduct->getPriceInfo()->getPrice(CatalogPrice\RegularPrice::PRICE_CODE)->getValue(); $product = clone $this->bundleProduct; $product->setFinalPrice($price); $this->eventManager->dispatch('catalog_product_get_final_price', ['product' => $product, 'qty' => $this->bundleProduct->getQty()]); $value = $product->getData('final_price') * ($selectionPriceValue / 100); } else { // calculate price for selection type fixed $value = $this->priceCurrency->convert($selectionPriceValue) * $this->quantity; } } if (!$this->useRegularPrice) { $value = $this->discountCalculator->calculateDiscount($this->bundleProduct, $value); } $this->value = $this->priceCurrency->round($value); return $this->value; }
/** * test for method getValue with type Fixed and selectionPriceType is empty or zero */ public function testGetValueTypeFixedWithoutSelectionPriceType() { $this->bundleMock->expects($this->once())->method('getPriceType')->will($this->returnValue(\Magento\Bundle\Model\Product\Price::PRICE_TYPE_FIXED)); $this->productMock->expects($this->once())->method('getSelectionPriceType')->will($this->returnValue(false)); $this->productMock->expects($this->any())->method('getSelectionPriceValue')->will($this->returnValue(100)); $this->discountCalculatorMock->expects($this->once())->method('calculateDiscount')->with($this->equalTo($this->bundleMock), $this->equalTo(100))->will($this->returnValue(70)); $this->assertEquals(70, $this->selectionPrice->getValue()); }
public function testGetAmount() { $this->setupSelectionPrice(); $price = 10.0; $amount = 20.0; $this->priceInfoMock->expects($this->once())->method('getPrice')->with(\Magento\Bundle\Pricing\Price\FinalPrice::PRICE_CODE)->willReturn($this->finalPriceMock); $this->finalPriceMock->expects($this->once())->method('getValue')->willReturn($price); $this->discountCalculatorMock->expects($this->once())->method('calculateDiscount')->with($this->bundleMock, $price)->willReturn($price); $this->priceCurrencyMock->expects($this->once())->method('round')->with($price)->willReturn($price); $this->bundleMock->expects($this->any())->method('getPriceType')->willReturn(\Magento\Bundle\Model\Product\Price::PRICE_TYPE_DYNAMIC); $this->calculatorMock->expects($this->once())->method('getAmount')->with($price, $this->productMock, null)->willReturn($amount); $this->assertEquals($amount, $this->selectionPrice->getAmount()); }
/** * test for method getValue with type Fixed and selectionPriceType is empty or zero * * @param bool $useRegularPrice * @dataProvider useRegularPriceDataProvider */ public function testGetValueTypeFixedWithoutSelectionPriceType($useRegularPrice) { $this->setupSelectionPrice($useRegularPrice); $regularPrice = 100.125; $discountedPrice = 70.453; $convertedValue = 100.247; $actualPrice = $useRegularPrice ? $convertedValue : $discountedPrice; $expectedPrice = $useRegularPrice ? round($convertedValue, 2) : round($discountedPrice, 2); $this->bundleMock->expects($this->once())->method('getPriceType')->will($this->returnValue(\Magento\Bundle\Model\Product\Price::PRICE_TYPE_FIXED)); $this->productMock->expects($this->once())->method('getSelectionPriceType')->will($this->returnValue(false)); $this->productMock->expects($this->any())->method('getSelectionPriceValue')->will($this->returnValue($regularPrice)); $this->priceCurrencyMock->expects($this->once())->method('convert')->with($regularPrice)->will($this->returnValue($convertedValue)); if (!$useRegularPrice) { $this->discountCalculatorMock->expects($this->once())->method('calculateDiscount')->with($this->equalTo($this->bundleMock), $this->equalTo($convertedValue))->will($this->returnValue($discountedPrice)); } $this->priceCurrencyMock->expects($this->once())->method('round')->with($actualPrice)->will($this->returnValue($expectedPrice)); $this->assertEquals($expectedPrice, $this->selectionPrice->getValue()); }
/** * Get the price value for one of selection product * * @return bool|float */ public function getValue() { if (null !== $this->value) { return $this->value; } if ($this->bundleProduct->getPriceType() == Price::PRICE_TYPE_DYNAMIC) { $value = $this->priceInfo->getPrice(FinalPrice::PRICE_CODE)->getValue(); } else { if ($this->product->getSelectionPriceType()) { // calculate price for selection type percent $price = $this->bundleProduct->getPriceInfo()->getPrice(CatalogPrice\RegularPrice::PRICE_CODE)->getValue(); $product = clone $this->bundleProduct; $product->setFinalPrice($price); $this->eventManager->dispatch('catalog_product_get_final_price', array('product' => $product, 'qty' => $this->bundleProduct->getQty())); $value = $product->getData('final_price') * ($this->product->getSelectionPriceValue() / 100); } else { // calculate price for selection type fixed $value = $this->product->getSelectionPriceValue() * $this->quantity; } } $this->value = $this->discountCalculator->calculateDiscount($this->bundleProduct, $value); return $this->value; }
/** * test method calculateDiscount with custom price amount */ public function testCalculateDiscountWithCustomAmount() { $this->productMock->expects($this->once())->method('getPriceInfo')->will($this->returnValue($this->priceInfoMock)); $this->priceInfoMock->expects($this->once())->method('getPrices')->will($this->returnValue([$this->getPriceMock(30), $this->getPriceMock(20), $this->getPriceMock(40)])); $this->assertEquals(10, $this->calculator->calculateDiscount($this->productMock, 50)); }