Skip to content

Commit 2de273e

Browse files
author
Yu Tang
committed
Merge remote-tracking branch 'origin/FearlessKiwis-MAGETWO-43961-fpt-final-price-with-custom-opts' into FearlessKiwis-develop-no-refactoring
2 parents 4e751b6 + 41a325c commit 2de273e

File tree

2 files changed

+95
-88
lines changed

2 files changed

+95
-88
lines changed

app/code/Magento/Weee/Observer/GetPriceConfigurationObserver.php

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ public function execute(\Magento\Framework\Event\Observer $observer)
5555
try {
5656
/** @var \Magento\Catalog\Model\Product $product */
5757
$product = $this->registry->registry('current_product');
58-
$weeeAttributes = $this->weeeData->getWeeeAttributesForBundle($product);
58+
$weeeAttributesForBundle = $this->weeeData->getWeeeAttributesForBundle($product);
5959
$priceConfig = $this->recurConfigAndInsertWeeePrice(
6060
$priceConfigObj->getConfig(),
6161
'prices',
62-
$this->getWhichCalcPriceToUse($product->getStoreId()),
63-
$weeeAttributes
62+
$this->getWhichCalcPriceToUse($product->getStoreId(), $weeeAttributesForBundle),
63+
$weeeAttributesForBundle
6464
);
6565
$priceConfigObj->setConfig($priceConfig);
6666
} catch (\Exception $e) {
@@ -76,25 +76,26 @@ public function execute(\Magento\Framework\Event\Observer $observer)
7676
* @param array $input
7777
* @param string $searchKey
7878
* @param string $calcPrice
79-
* @param array $weeeAttributes
79+
* @param array $weeeAttributesForBundle
8080
* @return array
8181
*/
82-
private function recurConfigAndInsertWeeePrice($input, $searchKey, $calcPrice, $weeeAttributes = null)
82+
private function recurConfigAndInsertWeeePrice($input, $searchKey, $calcPrice, $weeeAttributesForBundle = null)
8383
{
8484
$holder = [];
8585
if (is_array($input)) {
8686
foreach ($input as $key => $el) {
8787
if (is_array($el)) {
88-
$holder[$key] = $this->recurConfigAndInsertWeeePrice($el, $searchKey, $calcPrice, $weeeAttributes);
88+
$holder[$key] =
89+
$this->recurConfigAndInsertWeeePrice($el, $searchKey, $calcPrice, $weeeAttributesForBundle);
8990
if ($key === $searchKey) {
9091
if ((!array_key_exists('weeePrice', $holder[$key])) &&
9192
(array_key_exists($calcPrice, $holder[$key]))
9293
) {
9394
//this is required for product options && bundle
9495
$holder[$key]['weeePrice'] = $holder[$key][$calcPrice];
9596
// only do processing on product options
96-
if (array_key_exists('optionId', $input) && $weeeAttributes) {
97-
$holder = $this->insertWeeePrice($holder, $key, $weeeAttributes);
97+
if (array_key_exists('optionId', $input) && $weeeAttributesForBundle) {
98+
$holder = $this->insertWeeePrice($holder, $key, $weeeAttributesForBundle);
9899
}
99100
}
100101
}
@@ -111,15 +112,17 @@ private function recurConfigAndInsertWeeePrice($input, $searchKey, $calcPrice, $
111112
*
112113
* @param array $holder
113114
* @param int|string $key
114-
* @param array $weeeAttributes
115+
* @param array $weeeAttributesForBundle
115116
* @return array
116117
*/
117-
private function insertWeeePrice($holder, $key, $weeeAttributes)
118+
private function insertWeeePrice($holder, $key, $weeeAttributesForBundle)
118119
{
119-
if (array_key_exists($holder['optionId'], $weeeAttributes)) {
120-
if (count($weeeAttributes[$holder['optionId']]) > 0 && is_array($weeeAttributes[$holder['optionId']])) {
120+
if (array_key_exists($holder['optionId'], $weeeAttributesForBundle)) {
121+
if (count($weeeAttributesForBundle[$holder['optionId']]) > 0 &&
122+
is_array($weeeAttributesForBundle[$holder['optionId']])
123+
) {
121124
$weeeSum = 0;
122-
foreach ($weeeAttributes[$holder['optionId']] as $weeeAttribute) {
125+
foreach ($weeeAttributesForBundle[$holder['optionId']] as $weeeAttribute) {
123126
$holder[$key]['weeePrice' . $weeeAttribute->getCode()] =
124127
['amount' => (float)$weeeAttribute->getAmountExclTax()];
125128
$weeeSum += (float)$weeeAttribute->getAmountExclTax();
@@ -137,16 +140,19 @@ private function insertWeeePrice($holder, $key, $weeeAttributes)
137140
* Returns which product price to use as a basis for the Weee's final price
138141
*
139142
* @param int|null $storeId
143+
* @param array|null $weeeAttributesForBundle
140144
* @return string
141145
*/
142-
protected function getWhichCalcPriceToUse($storeId = null)
146+
protected function getWhichCalcPriceToUse($storeId = null, $weeeAttributesForBundle = null)
143147
{
144148
$calcPrice = 'finalPrice';
145-
if ($this->weeeData->isDisplayExcl($storeId) ||
146-
$this->weeeData->isDisplayExclDescIncl($storeId) ||
147-
($this->taxData->priceIncludesTax() && $this->taxData->displayPriceExcludingTax())
148-
) {
149-
$calcPrice = 'basePrice';
149+
if (!empty($weeeAttributesForBundle)) {
150+
if ($this->weeeData->isDisplayExcl($storeId) ||
151+
$this->weeeData->isDisplayExclDescIncl($storeId) ||
152+
($this->taxData->priceIncludesTax() && $this->taxData->displayPriceExcludingTax())
153+
) {
154+
$calcPrice = 'basePrice';
155+
}
150156
}
151157
return $calcPrice;
152158
}

app/code/Magento/Weee/Test/Unit/Observer/GetPriceConfigurationObserverTest.php

Lines changed: 70 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ class GetPriceConfigurationObserverTest extends \PHPUnit_Framework_TestCase
1313
/**
1414
* Tests the methods that rely on the ScopeConfigInterface object to provide their return values
1515
* @dataProvider getPriceConfigurationProvider
16+
* @param bool $hasWeeeAttributes
1617
* @param array $testArray
1718
* @param array $expectedArray
1819
*/
19-
public function testGetPriceConfiguration($testArray, $expectedArray)
20+
public function testGetPriceConfiguration($hasWeeeAttributes, $testArray, $expectedArray)
2021
{
2122
$configObj = new \Magento\Framework\DataObject(
2223
[
@@ -74,15 +75,21 @@ public function testGetPriceConfiguration($testArray, $expectedArray)
7475
->with('current_product')
7576
->will($this->returnValue($product));
7677

77-
$weeeHelper->expects($this->any())
78-
->method('getWeeeAttributesForBundle')
79-
->will($this->returnValue([
80-
1 => ['fpt1' => $weeeObject1],
81-
2 => [
82-
'fpt1'=>$weeeObject1,
83-
'fpt2'=> $weeeObject2
84-
]
85-
]));
78+
if ($hasWeeeAttributes) {
79+
$weeeHelper->expects($this->any())
80+
->method('getWeeeAttributesForBundle')
81+
->will($this->returnValue([
82+
1 => ['fpt1' => $weeeObject1],
83+
2 => [
84+
'fpt1' => $weeeObject1,
85+
'fpt2' => $weeeObject2
86+
]
87+
]));
88+
} else {
89+
$weeeHelper->expects($this->any())
90+
->method('getWeeeAttributesForBundle')
91+
->will($this->returnValue(null));
92+
}
8693

8794
$objectManager = new ObjectManager($this);
8895
/** @var \Magento\Weee\Observer\GetPriceConfigurationObserver $weeeObserverObject */
@@ -105,31 +112,24 @@ public function testGetPriceConfiguration($testArray, $expectedArray)
105112
public function getPriceConfigurationProvider()
106113
{
107114
return [
108-
[
115+
"basic" => [
116+
'hasWeeeAttributes' => true,
109117
'testArray' => [
110118
[
111119
[
112120
'optionId' => 1,
113121
'prices' =>
114122
[
115-
'finalPrice' => [
116-
'amount' => 31.50,
117-
],
118-
'basePrice' => [
119-
'amount' => 33.50,
120-
],
123+
'finalPrice' => ['amount' => 31.50],
124+
'basePrice' => ['amount' => 33.50],
121125
],
122126
],
123127
[
124128
'optionId' => 2,
125129
'prices' =>
126130
[
127-
'finalPrice' =>[
128-
'amount' => 331.50,
129-
],
130-
'basePrice' => [
131-
'amount' => 333.50,
132-
],
131+
'finalPrice' =>['amount' => 331.50],
132+
'basePrice' => ['amount' => 333.50],
133133
],
134134
],
135135
],
@@ -140,53 +140,35 @@ public function getPriceConfigurationProvider()
140140
'optionId' => 1,
141141
'prices' =>
142142
[
143-
'finalPrice' => [
144-
'amount' => 31.50,
145-
],
146-
'basePrice' => [
147-
'amount' => 33.50,
148-
],
149-
'weeePrice' => [
150-
'amount' => 46.5,
151-
],
152-
'weeePricefpt1' => [
153-
'amount' => 15,
154-
],
143+
'finalPrice' => ['amount' => 31.50],
144+
'basePrice' => ['amount' => 33.50],
145+
'weeePrice' => ['amount' => 46.5],
146+
'weeePricefpt1' => ['amount' => 15],
155147
],
156148
],
157149
[
158150
'optionId' => 2,
159151
'prices' =>
160152
[
161-
'finalPrice' =>[
162-
'amount' => 331.50,
163-
],
164-
'basePrice' => [
165-
'amount' => 333.50,
166-
],
167-
'weeePrice' => [
168-
'amount' => 362.5,
169-
],
170-
'weeePricefpt1' => [
171-
'amount' => 15,
172-
],
173-
'weeePricefpt2' => [
174-
'amount' => 16,
175-
],
153+
'finalPrice' =>['amount' => 331.50],
154+
'basePrice' => ['amount' => 333.50],
155+
'weeePrice' => ['amount' => 362.5],
156+
'weeePricefpt1' => ['amount' => 15],
157+
'weeePricefpt2' => ['amount' => 16],
176158
],
177159
],
178160
],
179161
],
180162
],
181-
[
163+
164+
"layered, with extra keys" => [
165+
'hasWeeeAttributes' => true,
182166
'testArray' => [
183167
[
184168
[
185169
'prices' =>
186170
[
187-
'finalPrice' => [
188-
'amount' => 31.50,
189-
],
171+
'finalPrice' => ['amount' => 31.50],
190172
],
191173
'somekey' => 0,
192174
],
@@ -195,9 +177,7 @@ public function getPriceConfigurationProvider()
195177
[
196178
'prices' =>
197179
[
198-
'finalPrice' =>[
199-
'amount' => 31.50,
200-
],
180+
'finalPrice' =>['amount' => 321.50],
201181
],
202182
],
203183
'otherkey' => [ 1, 2 , 3],
@@ -210,12 +190,8 @@ public function getPriceConfigurationProvider()
210190
[
211191
'prices' =>
212192
[
213-
'finalPrice' => [
214-
'amount' => 31.50,
215-
],
216-
'weeePrice' => [
217-
'amount' => 31.50,
218-
],
193+
'finalPrice' => ['amount' => 31.50],
194+
'weeePrice' => ['amount' => 31.50],
219195
],
220196
'somekey' => 0,
221197
],
@@ -224,12 +200,8 @@ public function getPriceConfigurationProvider()
224200
[
225201
'prices' =>
226202
[
227-
'finalPrice' =>[
228-
'amount' => 31.50,
229-
],
230-
'weeePrice' => [
231-
'amount' => 31.50,
232-
],
203+
'finalPrice' =>['amount' => 321.50],
204+
'weeePrice' => ['amount' => 321.50],
233205
],
234206
],
235207
'otherkey' => [ 1, 2 , 3],
@@ -238,6 +210,35 @@ public function getPriceConfigurationProvider()
238210
],
239211
],
240212
],
213+
214+
"no Weee attributes, expect WeeePrice to be same as FinalPrice" => [
215+
'hasWeeeAttributes' => false,
216+
'testArray' => [
217+
[
218+
[
219+
'optionId' => 1,
220+
'prices' =>
221+
[
222+
'basePrice' => ['amount' => 10],
223+
'finalPrice' => ['amount' => 11],
224+
],
225+
],
226+
],
227+
],
228+
'expectedArray' => [
229+
[
230+
[
231+
'optionId' => 1,
232+
'prices' =>
233+
[
234+
'basePrice' => ['amount' => 10],
235+
'finalPrice' => ['amount' => 11],
236+
'weeePrice' => ['amount' => 11],
237+
],
238+
],
239+
],
240+
],
241+
],
241242
];
242243
}
243244
}

0 commit comments

Comments
 (0)