Skip to content

Commit 8d032a1

Browse files
ENGCOM-1128: [Forwardport] Issues #10559 - Extend swatch using mixins (M2.2) #14247
- Merge Pull Request #14247 from rostyslav-hymon/magento2:2.3-develop-PR-port-12929 - Merged commits: 1. 3b27605 2. 7d51276 3. 0044fef
2 parents 160e368 + 0044fef commit 8d032a1

File tree

9 files changed

+329
-78
lines changed

9 files changed

+329
-78
lines changed

app/code/Magento/Catalog/Pricing/Render/FinalPriceBox.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ protected function wrapResult($html)
115115
{
116116
return '<div class="price-box ' . $this->getData('css_classes') . '" ' .
117117
'data-role="priceBox" ' .
118-
'data-product-id="' . $this->getSaleableItem()->getId() . '"' .
118+
'data-product-id="' . $this->getSaleableItem()->getId() . '" ' .
119+
'data-price-box="product-id-' . $this->getSaleableItem()->getId() . '"' .
119120
'>' . $html . '</div>';
120121
}
121122

app/code/Magento/Catalog/Test/Unit/Pricing/Render/FinalPriceBoxTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,8 @@ public function testRenderMsrpEnabled()
246246

247247
//assert price wrapper
248248
$this->assertEquals(
249-
'<div class="price-box price-final_price" data-role="priceBox" data-product-id="">test</div>',
249+
'<div class="price-box price-final_price" data-role="priceBox" data-product-id="" ' .
250+
'data-price-box="product-id-">test</div>',
250251
$result
251252
);
252253
}

app/code/Magento/ConfigurableProduct/Block/Product/View/Type/Configurable.php

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ class Configurable extends \Magento\Catalog\Block\Product\View\AbstractView
7474
*/
7575
private $customerSession;
7676

77+
/**
78+
* @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Variations\Prices
79+
*/
80+
private $variationPrices;
81+
7782
/**
7883
* @param \Magento\Catalog\Block\Product\Context $context
7984
* @param \Magento\Framework\Stdlib\ArrayUtils $arrayUtils
@@ -86,6 +91,7 @@ class Configurable extends \Magento\Catalog\Block\Product\View\AbstractView
8691
* @param array $data
8792
* @param Format|null $localeFormat
8893
* @param Session|null $customerSession
94+
* @param \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Variations\Prices|null $variationPrices
8995
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
9096
*/
9197
public function __construct(
@@ -99,7 +105,8 @@ public function __construct(
99105
ConfigurableAttributeData $configurableAttributeData,
100106
array $data = [],
101107
Format $localeFormat = null,
102-
Session $customerSession = null
108+
Session $customerSession = null,
109+
\Magento\ConfigurableProduct\Model\Product\Type\Configurable\Variations\Prices $variationPrices = null
103110
) {
104111
$this->priceCurrency = $priceCurrency;
105112
$this->helper = $helper;
@@ -109,6 +116,9 @@ public function __construct(
109116
$this->configurableAttributeData = $configurableAttributeData;
110117
$this->localeFormat = $localeFormat ?: ObjectManager::getInstance()->get(Format::class);
111118
$this->customerSession = $customerSession ?: ObjectManager::getInstance()->get(Session::class);
119+
$this->variationPrices = $variationPrices ?: ObjectManager::getInstance()->get(
120+
\Magento\ConfigurableProduct\Model\Product\Type\Configurable\Variations\Prices::class
121+
);
112122

113123
parent::__construct(
114124
$context,
@@ -211,9 +221,6 @@ public function getJsonConfig()
211221
$store = $this->getCurrentStore();
212222
$currentProduct = $this->getProduct();
213223

214-
$regularPrice = $currentProduct->getPriceInfo()->getPrice('regular_price');
215-
$finalPrice = $currentProduct->getPriceInfo()->getPrice('final_price');
216-
217224
$options = $this->helper->getOptions($currentProduct, $this->getAllowProducts());
218225
$attributesData = $this->configurableAttributeData->getAttributesData($currentProduct, $options);
219226

@@ -223,17 +230,7 @@ public function getJsonConfig()
223230
'currencyFormat' => $store->getCurrentCurrency()->getOutputFormat(),
224231
'optionPrices' => $this->getOptionPrices(),
225232
'priceFormat' => $this->localeFormat->getPriceFormat(),
226-
'prices' => [
227-
'oldPrice' => [
228-
'amount' => $this->localeFormat->getNumber($regularPrice->getAmount()->getValue()),
229-
],
230-
'basePrice' => [
231-
'amount' => $this->localeFormat->getNumber($finalPrice->getAmount()->getBaseAmount()),
232-
],
233-
'finalPrice' => [
234-
'amount' => $this->localeFormat->getNumber($finalPrice->getAmount()->getValue()),
235-
],
236-
],
233+
'prices' => $this->variationPrices->getFormattedPrices($this->getProduct()->getPriceInfo()),
237234
'productId' => $currentProduct->getId(),
238235
'chooseText' => __('Choose an Option...'),
239236
'images' => $this->getOptionImages(),
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\ConfigurableProduct\Model\Product\Type\Configurable\Variations;
8+
9+
/**
10+
* Configurable product variation prices.
11+
*/
12+
class Prices
13+
{
14+
/**
15+
* @var \Magento\Framework\Locale\Format
16+
*/
17+
private $localeFormat;
18+
19+
/**
20+
* Prices constructor.
21+
* @param \Magento\Framework\Locale\Format $localeFormat
22+
*/
23+
public function __construct(\Magento\Framework\Locale\Format $localeFormat)
24+
{
25+
$this->localeFormat = $localeFormat;
26+
}
27+
28+
/**
29+
* Get product prices for configurable variations
30+
*
31+
* @param \Magento\Framework\Pricing\PriceInfo\Base $priceInfo
32+
* @return array
33+
*/
34+
public function getFormattedPrices(\Magento\Framework\Pricing\PriceInfo\Base $priceInfo)
35+
{
36+
$regularPrice = $priceInfo->getPrice('regular_price');
37+
$finalPrice = $priceInfo->getPrice('final_price');
38+
39+
return [
40+
'oldPrice' => [
41+
'amount' => $this->localeFormat->getNumber($regularPrice->getAmount()->getValue()),
42+
],
43+
'basePrice' => [
44+
'amount' => $this->localeFormat->getNumber($finalPrice->getAmount()->getBaseAmount()),
45+
],
46+
'finalPrice' => [
47+
'amount' => $this->localeFormat->getNumber($finalPrice->getAmount()->getValue()),
48+
],
49+
];
50+
}
51+
}

app/code/Magento/ConfigurableProduct/Test/Unit/Block/Product/View/Type/ConfigurableTest.php

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ class ConfigurableTest extends \PHPUnit\Framework\TestCase
7878
*/
7979
private $customerSession;
8080

81+
/**
82+
* @var \PHPUnit_Framework_MockObject_MockObject
83+
*/
84+
private $variationPricesMock;
85+
8186
protected function setUp()
8287
{
8388
$this->mockContextObject();
@@ -144,6 +149,10 @@ protected function setUp()
144149
->disableOriginalConstructor()
145150
->getMock();
146151

152+
$this->variationPricesMock = $this->createMock(
153+
\Magento\ConfigurableProduct\Model\Product\Type\Configurable\Variations\Prices::class
154+
);
155+
147156
$this->block = new \Magento\ConfigurableProduct\Block\Product\View\Type\Configurable(
148157
$this->context,
149158
$this->arrayUtils,
@@ -155,7 +164,8 @@ protected function setUp()
155164
$this->configurableAttributeData,
156165
[],
157166
$this->localeFormat,
158-
$this->customerSession
167+
$this->customerSession,
168+
$this->variationPricesMock
159169
);
160170
}
161171

@@ -260,12 +270,8 @@ public function testGetJsonConfig()
260270
'getAmount',
261271
])
262272
->getMockForAbstractClass();
263-
$priceMock->expects($this->any())
264-
->method('getAmount')
265-
->willReturn($amountMock);
266-
273+
$priceMock->expects($this->any())->method('getAmount')->willReturn($amountMock);
267274
$tierPriceMock = $this->getTierPriceMock($amountMock, $priceQty, $percentage);
268-
269275
$productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
270276
->disableOriginalConstructor()
271277
->getMock();
@@ -283,27 +289,16 @@ public function testGetJsonConfig()
283289
['tier_price', $tierPriceMock],
284290
]);
285291

286-
$productMock->expects($this->any())
287-
->method('getTypeInstance')
288-
->willReturn($productTypeMock);
289-
$productMock->expects($this->any())
290-
->method('getPriceInfo')
291-
->willReturn($priceInfoMock);
292-
$productMock->expects($this->any())
293-
->method('isSaleable')
294-
->willReturn(true);
295-
$productMock->expects($this->any())
296-
->method('getId')
297-
->willReturn($productId);
292+
$productMock->expects($this->any())->method('getTypeInstance')->willReturn($productTypeMock);
293+
$productMock->expects($this->any())->method('getPriceInfo')->willReturn($priceInfoMock);
294+
$productMock->expects($this->any())->method('isSaleable')->willReturn(true);
295+
$productMock->expects($this->any())->method('getId')->willReturn($productId);
298296

299297
$this->helper->expects($this->any())
300298
->method('getOptions')
301299
->with($productMock, [$productMock])
302300
->willReturn([]);
303-
304-
$this->product->expects($this->any())
305-
->method('getSkipSaleableCheck')
306-
->willReturn(true);
301+
$this->product->expects($this->any())->method('getSkipSaleableCheck')->willReturn(true);
307302

308303
$attributesData = [
309304
'attributes' => [],
@@ -315,9 +310,7 @@ public function testGetJsonConfig()
315310
->with($productMock, [])
316311
->willReturn($attributesData);
317312

318-
$this->localeFormat->expects($this->any())
319-
->method('getPriceFormat')
320-
->willReturn([]);
313+
$this->localeFormat->expects($this->atLeastOnce())->method('getPriceFormat')->willReturn([]);
321314
$this->localeFormat->expects($this->any())
322315
->method('getNumber')
323316
->willReturnMap([
@@ -326,16 +319,29 @@ public function testGetJsonConfig()
326319
[$percentage, $percentage],
327320
]);
328321

322+
$this->variationPricesMock->expects($this->once())
323+
->method('getFormattedPrices')
324+
->with($priceInfoMock)
325+
->willReturn(
326+
[
327+
'oldPrice' => [
328+
'amount' => $amount,
329+
],
330+
'basePrice' => [
331+
'amount' => $amount,
332+
],
333+
'finalPrice' => [
334+
'amount' => $amount,
335+
],
336+
]
337+
);
338+
329339
$expectedArray = $this->getExpectedArray($productId, $amount, $priceQty, $percentage);
330340
$expectedJson = json_encode($expectedArray);
331341

332-
$this->jsonEncoder->expects($this->once())
333-
->method('encode')
334-
->with($expectedArray)
335-
->willReturn($expectedJson);
342+
$this->jsonEncoder->expects($this->once())->method('encode')->with($expectedArray)->willReturn($expectedJson);
336343

337344
$this->block->setData('product', $productMock);
338-
339345
$result = $this->block->getJsonConfig();
340346
$this->assertEquals($expectedJson, $result);
341347
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\ConfigurableProduct\Test\Unit\Model\Product\Type\Configurable\Variations;
8+
9+
use PHPUnit\Framework\TestCase;
10+
11+
class PricesTest extends TestCase
12+
{
13+
/**
14+
* @var \PHPUnit_Framework_MockObject_MockObject
15+
*/
16+
private $localeFormatMock;
17+
18+
/**
19+
* @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Variations\Prices
20+
*/
21+
private $model;
22+
23+
protected function setUp()
24+
{
25+
$this->localeFormatMock = $this->createMock(\Magento\Framework\Locale\Format::class);
26+
$this->model = new \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Variations\Prices(
27+
$this->localeFormatMock
28+
);
29+
}
30+
31+
public function testGetFormattedPrices()
32+
{
33+
$expected = [
34+
'oldPrice' => [
35+
'amount' => 500
36+
],
37+
'basePrice' => [
38+
'amount' => 1000
39+
],
40+
'finalPrice' => [
41+
'amount' => 500
42+
]
43+
];
44+
$priceInfoMock = $this->createMock(\Magento\Framework\Pricing\PriceInfo\Base::class);
45+
$priceMock = $this->createMock(\Magento\Framework\Pricing\Price\PriceInterface::class);
46+
$priceInfoMock->expects($this->atLeastOnce())->method('getPrice')->willReturn($priceMock);
47+
48+
$amountMock = $this->createMock(\Magento\Framework\Pricing\Amount\AmountInterface::class);
49+
$amountMock->expects($this->atLeastOnce())->method('getValue')->willReturn(500);
50+
$amountMock->expects($this->atLeastOnce())->method('getBaseAmount')->willReturn(1000);
51+
$priceMock->expects($this->atLeastOnce())->method('getAmount')->willReturn($amountMock);
52+
53+
$this->localeFormatMock->expects($this->atLeastOnce())
54+
->method('getNumber')
55+
->withConsecutive([500], [1000], [500])
56+
->will($this->onConsecutiveCalls(500, 1000, 500));
57+
58+
$this->assertEquals($expected, $this->model->getFormattedPrices($priceInfoMock));
59+
}
60+
}

0 commit comments

Comments
 (0)