diff --git a/app/code/Magento/ConfigurableProduct/Pricing/Render/FinalPriceBox.php b/app/code/Magento/ConfigurableProduct/Pricing/Render/FinalPriceBox.php new file mode 100644 index 0000000000000..16a296a355454 --- /dev/null +++ b/app/code/Magento/ConfigurableProduct/Pricing/Render/FinalPriceBox.php @@ -0,0 +1,60 @@ +configurableOptionsProvider = $configurableOptionsProvider; + parent::__construct($context, $saleableItem, $price, $rendererPool, $data); + } + + /** + * Define if the special price should be shown + * + * @return bool + */ + public function hasSpecialPrice() + { + $product = $this->getSaleableItem(); + foreach ($this->configurableOptionsProvider->getProducts($product) as $subProduct) { + $regularPrice = $subProduct->getPriceInfo()->getPrice(RegularPrice::PRICE_CODE)->getValue(); + $finalPrice = $subProduct->getPriceInfo()->getPrice(FinalPrice::PRICE_CODE)->getValue(); + if ($finalPrice < $regularPrice) { + return true; + } + } + return false; + } +} diff --git a/app/code/Magento/ConfigurableProduct/Test/Unit/Pricing/Render/FinalPriceBoxTest.php b/app/code/Magento/ConfigurableProduct/Test/Unit/Pricing/Render/FinalPriceBoxTest.php new file mode 100644 index 0000000000000..4dbcfed531525 --- /dev/null +++ b/app/code/Magento/ConfigurableProduct/Test/Unit/Pricing/Render/FinalPriceBoxTest.php @@ -0,0 +1,137 @@ +context = $this->getMockBuilder(\Magento\Framework\View\Element\Template\Context::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->saleableItem = $this->getMockBuilder(\Magento\Catalog\Model\Product::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->price = $this->getMockBuilder(\Magento\Framework\Pricing\Price\PriceInterface::class) + ->getMockForAbstractClass(); + + $this->rendererPool = $this->getMockBuilder(\Magento\Framework\Pricing\Render\RendererPool::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->configurableOptionsProvider = $this->getMockBuilder(ConfigurableOptionsProviderInterface::class) + ->getMockForAbstractClass(); + + $this->model = new FinalPriceBox( + $this->context, + $this->saleableItem, + $this->price, + $this->rendererPool, + $this->configurableOptionsProvider + ); + } + + /** + * @param float $regularPrice + * @param float $finalPrice + * @param bool $expected + * @dataProvider hasSpecialPriceDataProvider + */ + public function testHasSpecialPrice( + $regularPrice, + $finalPrice, + $expected + ) { + $priceMockOne = $this->getMockBuilder(\Magento\Framework\Pricing\Price\PriceInterface::class) + ->getMockForAbstractClass(); + + $priceMockOne->expects($this->once()) + ->method('getValue') + ->willReturn($regularPrice); + + $priceMockTwo = $this->getMockBuilder(\Magento\Framework\Pricing\Price\PriceInterface::class) + ->getMockForAbstractClass(); + + $priceMockTwo->expects($this->once()) + ->method('getValue') + ->willReturn($finalPrice); + + $priceInfoMock = $this->getMockBuilder(\Magento\Framework\Pricing\PriceInfo\Base::class) + ->disableOriginalConstructor() + ->getMock(); + + $priceInfoMock->expects($this->exactly(2)) + ->method('getPrice') + ->willReturnMap([ + [RegularPrice::PRICE_CODE, $priceMockOne], + [FinalPrice::PRICE_CODE, $priceMockTwo], + ]); + + $productMock = $this->getMockBuilder(\Magento\Catalog\Api\Data\ProductInterface::class) + ->setMethods(['getPriceInfo']) + ->getMockForAbstractClass(); + + $productMock->expects($this->exactly(2)) + ->method('getPriceInfo') + ->willReturn($priceInfoMock); + + $this->configurableOptionsProvider->expects($this->once()) + ->method('getProducts') + ->with($this->saleableItem) + ->willReturn([$productMock]); + + $this->assertEquals($expected, $this->model->hasSpecialPrice()); + } + + /** + * @return array + */ + public function hasSpecialPriceDataProvider() + { + return [ + [10., 20., false], + [10., 10., false], + [20., 10., true], + ]; + } +} diff --git a/app/code/Magento/ConfigurableProduct/view/base/layout/catalog_product_prices.xml b/app/code/Magento/ConfigurableProduct/view/base/layout/catalog_product_prices.xml new file mode 100644 index 0000000000000..47fe31681b5bf --- /dev/null +++ b/app/code/Magento/ConfigurableProduct/view/base/layout/catalog_product_prices.xml @@ -0,0 +1,21 @@ + + + + + + + + + Magento\ConfigurableProduct\Pricing\Render\FinalPriceBox + Magento_ConfigurableProduct::product/price/final_price.phtml + + + + + + diff --git a/app/code/Magento/ConfigurableProduct/view/base/templates/product/price/final_price.phtml b/app/code/Magento/ConfigurableProduct/view/base/templates/product/price/final_price.phtml new file mode 100644 index 0000000000000..84383d95c1741 --- /dev/null +++ b/app/code/Magento/ConfigurableProduct/view/base/templates/product/price/final_price.phtml @@ -0,0 +1,60 @@ + + +getPriceType('regular_price'); + +/** @var \Magento\Framework\Pricing\Price\PriceInterface $finalPriceModel */ +$finalPriceModel = $block->getPriceType('final_price'); +$idSuffix = $block->getIdSuffix() ? $block->getIdSuffix() : ''; +$schema = ($block->getZone() == 'item_view') ? true : false; +?> +hasSpecialPrice()): ?> + + renderAmount($finalPriceModel->getAmount(), [ + 'display_label' => __('Special Price'), + 'price_id' => $block->getPriceId('product-price-' . $idSuffix), + 'price_type' => 'finalPrice', + 'include_container' => true, + 'schema' => $schema + ]); ?> + + + renderAmount($priceModel->getAmount(), [ + 'display_label' => __('Regular Price'), + 'price_id' => $block->getPriceId('old-price-' . $idSuffix), + 'price_type' => 'oldPrice', + 'include_container' => true, + 'skip_adjustments' => true + ]); ?> + + + renderAmount($finalPriceModel->getAmount(), [ + 'price_id' => $block->getPriceId('product-price-' . $idSuffix), + 'price_type' => 'finalPrice', + 'include_container' => true, + 'schema' => $schema + ]); ?> + + +showMinimalPrice()): ?> + getUseLinkForAsLowAs()):?> + + renderAmountMinimal(); ?> + + + + renderAmountMinimal(); ?> + + + diff --git a/app/code/Magento/ConfigurableProduct/view/frontend/web/js/configurable.js b/app/code/Magento/ConfigurableProduct/view/frontend/web/js/configurable.js index d38e2760f1dc7..572b2ac3ff503 100644 --- a/app/code/Magento/ConfigurableProduct/view/frontend/web/js/configurable.js +++ b/app/code/Magento/ConfigurableProduct/view/frontend/web/js/configurable.js @@ -28,6 +28,7 @@ define([ '<% } %>', mediaGallerySelector: '[data-gallery-role=gallery-placeholder]', mediaGalleryInitial: null, + regularPriceSelector: '.old-price', onlyMainImg: false }, @@ -246,6 +247,7 @@ define([ this._resetChildren(element); } this._reloadPrice(); + this._displayRegularPriceBlock(this.simpleProduct); this._changeProductImage(); }, @@ -442,7 +444,7 @@ define([ }, /** - * Returns pracies for configured products + * Returns prices for configured products * * @param {*} config - Products configuration * @returns {*} @@ -485,6 +487,23 @@ define([ undefined : _.first(config.allowedProducts); + }, + + /** + * Show or hide regular price block + * + * @param {*} optionId + * @private + */ + _displayRegularPriceBlock: function (optionId) { + if (typeof optionId != 'undefined' + && this.options.spConfig.optionPrices[optionId].oldPrice.amount + != this.options.spConfig.optionPrices[optionId].finalPrice.amount + ) { + $(this.options.regularPriceSelector).show(); + } else { + $(this.options.regularPriceSelector).hide(); + } } });