Skip to content

Commit 12c6c9b

Browse files
author
Joan He
committed
Merge remote-tracking branch 'trigger/MC-15085' into pr
2 parents ee309fc + b0627fb commit 12c6c9b

File tree

25 files changed

+434
-45
lines changed

25 files changed

+434
-45
lines changed

app/code/Magento/ConfigurableImportExport/etc/module.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
9-
<module name="Magento_ConfigurableImportExport" >
9+
<module name="Magento_ConfigurableImportExport">
10+
<sequence>
11+
<module name="Magento_ConfigurableProduct"/>
12+
</sequence>
1013
</module>
1114
</config>

app/code/Magento/Msrp/Helper/Data.php

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@
77

88
use Magento\Framework\App\Helper\AbstractHelper;
99
use Magento\Framework\App\Helper\Context;
10+
use Magento\Framework\App\ObjectManager;
1011
use Magento\Msrp\Model\Product\Attribute\Source\Type;
12+
use Magento\Msrp\Pricing\MsrpPriceCalculatorInterface;
1113
use Magento\Store\Model\StoreManagerInterface;
1214
use Magento\Catalog\Model\Product;
1315
use Magento\Catalog\Api\ProductRepositoryInterface;
14-
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
1516

1617
/**
1718
* Msrp data helper
19+
*
20+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1821
*/
1922
class Data extends AbstractHelper
2023
{
@@ -43,6 +46,11 @@ class Data extends AbstractHelper
4346
*/
4447
protected $productRepository;
4548

49+
/**
50+
* @var MsrpPriceCalculatorInterface
51+
*/
52+
private $msrpPriceCalculator;
53+
4654
/**
4755
* @param Context $context
4856
* @param StoreManagerInterface $storeManager
@@ -51,6 +59,7 @@ class Data extends AbstractHelper
5159
* @param \Magento\Msrp\Model\Config $config
5260
* @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
5361
* @param ProductRepositoryInterface $productRepository
62+
* @param MsrpPriceCalculatorInterface|null $msrpPriceCalculator
5463
*/
5564
public function __construct(
5665
Context $context,
@@ -59,7 +68,8 @@ public function __construct(
5968
\Magento\Msrp\Model\Msrp $msrp,
6069
\Magento\Msrp\Model\Config $config,
6170
\Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
62-
ProductRepositoryInterface $productRepository
71+
ProductRepositoryInterface $productRepository,
72+
MsrpPriceCalculatorInterface $msrpPriceCalculator = null
6373
) {
6474
parent::__construct($context);
6575
$this->storeManager = $storeManager;
@@ -68,6 +78,8 @@ public function __construct(
6878
$this->config = $config;
6979
$this->priceCurrency = $priceCurrency;
7080
$this->productRepository = $productRepository;
81+
$this->msrpPriceCalculator = $msrpPriceCalculator
82+
?: ObjectManager::getInstance()->get(MsrpPriceCalculatorInterface::class);
7183
}
7284

7385
/**
@@ -78,6 +90,7 @@ public function __construct(
7890
* @return bool
7991
*
8092
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
93+
* @throws \Magento\Framework\Exception\NoSuchEntityException
8194
*/
8295
public function canApplyMsrp($product, $visibility = null)
8396
{
@@ -111,6 +124,7 @@ public function canApplyMsrp($product, $visibility = null)
111124
*
112125
* @param Product $product
113126
* @return string
127+
* @throws \Magento\Framework\Exception\NoSuchEntityException
114128
*/
115129
public function getMsrpPriceMessage($product)
116130
{
@@ -128,6 +142,7 @@ public function getMsrpPriceMessage($product)
128142
*
129143
* @param int|Product $product
130144
* @return bool
145+
* @throws \Magento\Framework\Exception\NoSuchEntityException
131146
*/
132147
public function isShowPriceOnGesture($product)
133148
{
@@ -139,6 +154,7 @@ public function isShowPriceOnGesture($product)
139154
*
140155
* @param int|Product $product
141156
* @return bool
157+
* @throws \Magento\Framework\Exception\NoSuchEntityException
142158
*/
143159
public function isShowBeforeOrderConfirm($product)
144160
{
@@ -149,31 +165,16 @@ public function isShowBeforeOrderConfirm($product)
149165
* Check if any MAP price is larger than as low as value.
150166
*
151167
* @param int|Product $product
152-
* @return bool|float
168+
* @return bool
169+
* @throws \Magento\Framework\Exception\NoSuchEntityException
153170
*/
154171
public function isMinimalPriceLessMsrp($product)
155172
{
156173
if (is_numeric($product)) {
157174
$product = $this->productRepository->getById($product, false, $this->storeManager->getStore()->getId());
158175
}
159-
$msrp = $product->getMsrp();
176+
$msrp = $this->msrpPriceCalculator->getMsrpPriceValue($product);
160177
$price = $product->getPriceInfo()->getPrice(\Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE);
161-
if ($msrp === null) {
162-
if ($product->getTypeId() === \Magento\GroupedProduct\Model\Product\Type\Grouped::TYPE_CODE) {
163-
$msrp = $product->getTypeInstance()->getChildrenMsrp($product);
164-
} elseif ($product->getTypeId() === Configurable::TYPE_CODE) {
165-
$prices = [];
166-
foreach ($product->getTypeInstance()->getUsedProducts($product) as $item) {
167-
if ($item->getMsrp() !== null) {
168-
$prices[] = $item->getMsrp();
169-
}
170-
}
171-
172-
$msrp = $prices ? max($prices) : 0;
173-
} else {
174-
return false;
175-
}
176-
}
177178
if ($msrp) {
178179
$msrp = $this->priceCurrency->convertAndRound($msrp);
179180
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Msrp\Pricing;
10+
11+
use Magento\Catalog\Api\Data\ProductInterface;
12+
13+
/**
14+
* @inheritdoc
15+
*/
16+
class MsrpPriceCalculator implements MsrpPriceCalculatorInterface
17+
{
18+
/**
19+
* @var MsrpPriceCalculatorInterface[]
20+
*/
21+
private $msrpPriceCalculators;
22+
23+
/**
24+
* @param array $msrpPriceCalculators
25+
*/
26+
public function __construct(array $msrpPriceCalculators)
27+
{
28+
$this->msrpPriceCalculators = $this->getMsrpPriceCalculators($msrpPriceCalculators);
29+
}
30+
31+
/**
32+
* @inheritdoc
33+
*/
34+
public function getMsrpPriceValue(ProductInterface $product): float
35+
{
36+
$productType = $product->getTypeId();
37+
if (isset($this->msrpPriceCalculators[$productType])) {
38+
$priceCalculator = $this->msrpPriceCalculators[$productType];
39+
$msrp = $priceCalculator->getMsrpPriceValue($product);
40+
} else {
41+
$msrp = (float)$product->getMsrp();
42+
}
43+
44+
return $msrp;
45+
}
46+
47+
/**
48+
* Convert the configuration of MSRP price calculators.
49+
*
50+
* @param array $msrpPriceCalculatorsConfig
51+
* @return array
52+
*/
53+
private function getMsrpPriceCalculators(array $msrpPriceCalculatorsConfig): array
54+
{
55+
$msrpPriceCalculators = [];
56+
foreach ($msrpPriceCalculatorsConfig as $msrpPriceCalculator) {
57+
if (isset($msrpPriceCalculator['productType'], $msrpPriceCalculator['priceCalculator'])) {
58+
$msrpPriceCalculators[$msrpPriceCalculator['productType']] =
59+
$msrpPriceCalculator['priceCalculator'];
60+
}
61+
}
62+
return $msrpPriceCalculators;
63+
}
64+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Msrp\Pricing;
8+
9+
use Magento\Catalog\Api\Data\ProductInterface;
10+
11+
/**
12+
* Provide information about MSRP price of a product.
13+
*/
14+
interface MsrpPriceCalculatorInterface
15+
{
16+
/**
17+
* Return the value of MSRP product price.
18+
*
19+
* @param ProductInterface $product
20+
* @return float
21+
*/
22+
public function getMsrpPriceValue(ProductInterface $product): float;
23+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Msrp\Pricing\Render;
10+
11+
use Magento\Catalog\Model\Product;
12+
use Magento\Framework\Json\Helper\Data;
13+
use Magento\Framework\Math\Random;
14+
use Magento\Framework\Pricing\Price\PriceInterface;
15+
use Magento\Framework\Pricing\Render\RendererPool;
16+
use Magento\Framework\View\Element\Template\Context;
17+
use Magento\Msrp\Pricing\MsrpPriceCalculatorInterface;
18+
19+
/**
20+
* MSRP price box render.
21+
*/
22+
class PriceBox extends \Magento\Catalog\Pricing\Render\PriceBox
23+
{
24+
/**
25+
* @var MsrpPriceCalculatorInterface
26+
*/
27+
private $msrpPriceCalculator;
28+
29+
/**
30+
* Constructor
31+
*
32+
* @param Context $context
33+
* @param Product $saleableItem
34+
* @param PriceInterface $price
35+
* @param RendererPool $rendererPool
36+
* @param Data $jsonHelper
37+
* @param Random $mathRandom
38+
* @param MsrpPriceCalculatorInterface $msrpPriceCalculator
39+
*/
40+
public function __construct(
41+
Context $context,
42+
Product $saleableItem,
43+
PriceInterface $price,
44+
RendererPool $rendererPool,
45+
Data $jsonHelper,
46+
Random $mathRandom,
47+
MsrpPriceCalculatorInterface $msrpPriceCalculator
48+
) {
49+
$this->msrpPriceCalculator = $msrpPriceCalculator;
50+
parent::__construct($context, $saleableItem, $price, $rendererPool, $jsonHelper, $mathRandom);
51+
}
52+
53+
/**
54+
* Return MSRP price calculator.
55+
*
56+
* @return MsrpPriceCalculatorInterface
57+
*/
58+
public function getMsrpPriceCalculator(): MsrpPriceCalculatorInterface
59+
{
60+
return $this->msrpPriceCalculator;
61+
}
62+
}

app/code/Magento/Msrp/Test/Unit/Helper/DataTest.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
namespace Magento\Msrp\Test\Unit\Helper;
88

9+
use Magento\Msrp\Pricing\MsrpPriceCalculatorInterface;
10+
911
/**
1012
* Class DataTest
1113
*/
@@ -26,24 +28,38 @@ class DataTest extends \PHPUnit\Framework\TestCase
2628
*/
2729
protected $productMock;
2830

31+
/**
32+
* @var MsrpPriceCalculatorInterface|\PHPUnit_Framework_MockObject_MockObject
33+
*/
34+
private $msrpPriceCalculator;
35+
36+
/**
37+
* @inheritdoc
38+
*/
2939
protected function setUp()
3040
{
3141
$this->priceCurrencyMock = $this->createMock(\Magento\Framework\Pricing\PriceCurrencyInterface::class);
3242
$this->productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
3343
->disableOriginalConstructor()
3444
->setMethods(['getMsrp', 'getPriceInfo', '__wakeup'])
3545
->getMock();
46+
$this->msrpPriceCalculator = $this->getMockBuilder(MsrpPriceCalculatorInterface::class)
47+
->getMockForAbstractClass();
3648

3749
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
3850

3951
$this->helper = $objectManager->getObject(
4052
\Magento\Msrp\Helper\Data::class,
4153
[
4254
'priceCurrency' => $this->priceCurrencyMock,
55+
'msrpPriceCalculator' => $this->msrpPriceCalculator,
4356
]
4457
);
4558
}
4659

60+
/**
61+
* @throws \Magento\Framework\Exception\NoSuchEntityException
62+
*/
4763
public function testIsMinimalPriceLessMsrp()
4864
{
4965
$msrp = 120;
@@ -73,12 +89,13 @@ function ($arg) {
7389
->with(\Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE)
7490
->will($this->returnValue($finalPriceMock));
7591

76-
$this->productMock->expects($this->any())
77-
->method('getMsrp')
78-
->will($this->returnValue($msrp));
92+
$this->msrpPriceCalculator
93+
->expects($this->any())
94+
->method('getMsrpPriceValue')
95+
->willReturn($msrp);
7996
$this->productMock->expects($this->any())
8097
->method('getPriceInfo')
81-
->will($this->returnValue($priceInfoMock));
98+
->willReturn($priceInfoMock);
8299

83100
$result = $this->helper->isMinimalPriceLessMsrp($this->productMock);
84101
$this->assertTrue($result, "isMinimalPriceLessMsrp returned incorrect value");

app/code/Magento/Msrp/composer.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
"magento/module-catalog": "*",
1111
"magento/module-downloadable": "*",
1212
"magento/module-eav": "*",
13-
"magento/module-grouped-product": "*",
14-
"magento/module-configurable-product": "*",
1513
"magento/module-store": "*",
1614
"magento/module-tax": "*"
1715
},

app/code/Magento/Msrp/etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
99
<preference for="\Magento\Msrp\Api\Data\ProductRender\MsrpPriceInfoInterface" type="\Magento\Msrp\Model\ProductRender\MsrpPriceInfo" />
10+
<preference for="\Magento\Msrp\Pricing\MsrpPriceCalculatorInterface" type="\Magento\Msrp\Pricing\MsrpPriceCalculator"/>
1011
<virtualType name="Magento\Catalog\Pricing\Price\Pool">
1112
<arguments>
1213
<argument name="prices" xsi:type="array">

app/code/Magento/Msrp/view/base/layout/catalog_product_prices.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<argument name="default" xsi:type="array">
1212
<item name="prices" xsi:type="array">
1313
<item name="msrp_price" xsi:type="array">
14-
<item name="render_class" xsi:type="string">Magento\Catalog\Pricing\Render\PriceBox</item>
14+
<item name="render_class" xsi:type="string">Magento\Msrp\Pricing\Render\PriceBox</item>
1515
<item name="render_template" xsi:type="string">Magento_Msrp::product/price/msrp.phtml</item>
1616
</item>
1717
</item>

0 commit comments

Comments
 (0)