Skip to content

Commit 792822b

Browse files
author
Robert He
committed
Merge branch 'FearlessKiwis-MAGETWO-43549-fpt-prices-negative' of https://github.corp.magento.com/magento-fearless-kiwis/magento2ce into develop-PR
2 parents 2ffc8c9 + 6be1be0 commit 792822b

File tree

4 files changed

+152
-81
lines changed

4 files changed

+152
-81
lines changed

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

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,28 @@ public function execute(\Magento\Framework\Event\Observer $observer)
5959
return $this;
6060
}
6161

62-
if ($this->weeeData->isEnabled() &&
63-
!$this->weeeData->geDisplayIncl($product->getStoreId()) &&
64-
!$this->weeeData->geDisplayExcl($product->getStoreId())
65-
) {
66-
// only do processing on bundle product
67-
if ($product->getTypeId() == \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) {
68-
if (!array_key_exists('optionTemplate', $options)) {
69-
$calcPrice = $this->getWhichCalcPriceToUse($product->getStoreId());
70-
$options['optionTemplate'] = '<%- data.label %>'
71-
. '<% if (data.' . $calcPrice . '.value) { %>'
72-
. ' +<%- data.' . $calcPrice . '.formatted %>'
73-
. '<% } %>';
74-
}
62+
// if the Weee module is enabled, then only do processing on bundle products
63+
if ($this->weeeData->isEnabled() && $product->getTypeId() == \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) {
64+
65+
if ($this->taxData->priceIncludesTax() && $this->taxData->displayPriceExcludingTax()) {
66+
// the Tax module might have set up a default, but we will re-decide which calcPrice field to use
67+
unset($options['optionTemplate']);
68+
}
69+
70+
if (!array_key_exists('optionTemplate', $options)) {
71+
$calcPrice = $this->getWhichCalcPriceToUse($product->getStoreId());
72+
$options['optionTemplate'] = '<%- data.label %>'
73+
. '<% if (data.' . $calcPrice . '.value) { %>'
74+
. ' +<%- data.' . $calcPrice . '.formatted %>'
75+
. '<% } %>';
76+
}
7577

78+
if (!$this->weeeData->geDisplayIncl($product->getStoreId()) &&
79+
!$this->weeeData->geDisplayExcl($product->getStoreId())) {
80+
// we need to display the individual Weee amounts
7681
foreach ($this->weeeData->getWeeeAttributesForBundle($product) as $weeeAttributes) {
7782
foreach ($weeeAttributes as $weeeAttribute) {
78-
if (!preg_match('/'.$weeeAttribute->getCode().'/', $options['optionTemplate'])) {
83+
if (!preg_match('/' . $weeeAttribute->getCode() . '/', $options['optionTemplate'])) {
7984
$options['optionTemplate'] .= sprintf(
8085
' <%% if (data.weeePrice' . $weeeAttribute->getCode() . ') { %%>'
8186
. ' (' . $weeeAttribute->getName()
@@ -86,34 +91,32 @@ public function execute(\Magento\Framework\Event\Observer $observer)
8691
}
8792
}
8893
}
94+
}
8995

90-
if ($this->weeeData->geDisplayExlDescIncl($product->getStoreId())) {
91-
$options['optionTemplate'] .= sprintf(
92-
' <%% if (data.weeePrice) { %%>'
93-
. '<%%- data.weeePrice.formatted %%>'
94-
. '<%% } %%>'
95-
);
96-
}
97-
96+
if ($this->weeeData->geDisplayExlDescIncl($product->getStoreId())) {
97+
$options['optionTemplate'] .= sprintf(
98+
' <%% if (data.weeePrice) { %%>'
99+
. '<%%- data.weeePrice.formatted %%>'
100+
. '<%% } %%>'
101+
);
98102
}
99103
}
100104
$response->setAdditionalOptions($options);
101105
return $this;
102106
}
103107

104108
/**
105-
* Returns which product price to use as a basis for the Weee's final price
109+
* Returns which product price to show (before listing the individual Weee amounts, if applicable)
106110
*
107111
* @param int|null $storeId
108112
* @return string
109113
*/
110114
protected function getWhichCalcPriceToUse($storeId = null)
111115
{
112116
$calcPrice = 'finalPrice';
113-
if ($this->weeeData->geDisplayExcl($storeId) ||
114-
$this->weeeData->geDisplayExlDescIncl($storeId) ||
115-
($this->taxData->priceIncludesTax() && $this->taxData->displayPriceExcludingTax())
116-
) {
117+
if ($this->weeeData->geDisplayExlDescIncl($storeId)) {
118+
$calcPrice = 'basePrice';
119+
} elseif ($this->weeeData->geDisplayExcl($storeId) && $this->taxData->displayPriceExcludingTax()) {
117120
$calcPrice = 'basePrice';
118121
}
119122
return $calcPrice;

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

Lines changed: 81 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,28 @@
66

77
namespace Magento\Weee\Test\Unit\Observer;
88

9-
use \Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
9+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
10+
use Magento\Weee\Model\Tax as WeeeDisplayConfig;
11+
use Magento\Tax\Model\Config as TaxConfig;
1012

1113
class UpdateProductOptionsObserverTest extends \PHPUnit_Framework_TestCase
1214
{
1315
/**
1416
* Tests the methods that rely on the ScopeConfigInterface object to provide their return values
1517
*
16-
* @param array $testArray The initial array that specifies the set of additional options
18+
* @param array $initialArray The initial array that specifies the set of additional options
1719
* @param bool $weeeEnabled Whether the Weee module is assumed to be enabled
18-
* @param bool $weeeDisplayExclDescIncl Is this Weee display setting assumed to be set
20+
* @param int $weeeDisplay Which Weee display is configured
21+
* @param int $priceDisplay Values are: including tax, excluding tax, or both including and excluding tax
1922
* @param array $expectedArray The revised array of the additional options
2023
*
2124
* @dataProvider updateProductOptionsProvider
2225
*/
23-
public function testUpdateProductOptions($testArray, $weeeEnabled, $weeeDisplayExclDescIncl, $expectedArray)
26+
public function testUpdateProductOptions($initialArray, $weeeEnabled, $weeeDisplay, $priceDisplay, $expectedArray)
2427
{
2528
$configObj = new \Magento\Framework\DataObject(
2629
[
27-
'additional_options' => $testArray,
30+
'additional_options' => $initialArray,
2831
]
2932
);
3033

@@ -46,13 +49,27 @@ public function testUpdateProductOptions($testArray, $weeeEnabled, $weeeDisplayE
4649
$weeeHelper->expects($this->any())
4750
->method('isEnabled')
4851
->will($this->returnValue($weeeEnabled));
52+
$weeeHelper->expects($this->any())
53+
->method('geDisplayIncl')
54+
->will($this->returnValue($weeeDisplay == WeeeDisplayConfig::DISPLAY_INCL));
4955
$weeeHelper->expects($this->any())
5056
->method('geDisplayExlDescIncl')
51-
->will($this->returnValue($weeeDisplayExclDescIncl));
57+
->will($this->returnValue($weeeDisplay == WeeeDisplayConfig::DISPLAY_EXCL_DESCR_INCL));
58+
$weeeHelper->expects($this->any())
59+
->method('geDisplayExcl')
60+
->will($this->returnValue($weeeDisplay == WeeeDisplayConfig::DISPLAY_EXCL));
5261
$weeeHelper->expects($this->any())
5362
->method('getWeeeAttributesForBundle')
5463
->will($this->returnValue([['fpt1' => $weeeObject1], ['fpt1'=>$weeeObject1, 'fpt2'=>$weeeObject2]]));
5564

65+
$taxHelper=$this->getMock('Magento\Tax\Helper\Data', [], [], '', false);
66+
$taxHelper->expects($this->any())
67+
->method('displayPriceExcludingTax')
68+
->will($this->returnValue($priceDisplay == TaxConfig::DISPLAY_TYPE_EXCLUDING_TAX));
69+
$taxHelper->expects($this->any())
70+
->method('priceIncludesTax')
71+
->will($this->returnValue(true));
72+
5673
$responseObject=$this->getMock('Magento\Framework\Event\Observer', ['getResponseObject'], [], '', false);
5774
$responseObject->expects($this->any())
5875
->method('getResponseObject')
@@ -83,6 +100,7 @@ public function testUpdateProductOptions($testArray, $weeeEnabled, $weeeDisplayE
83100
'Magento\Weee\Observer\UpdateProductOptionsObserver',
84101
[
85102
'weeeData' => $weeeHelper,
103+
'taxData' => $taxHelper,
86104
'registry' => $registry,
87105
]
88106
);
@@ -99,45 +117,64 @@ public function updateProductOptionsProvider()
99117
{
100118
return [
101119
'weee not enabled' => [
102-
'testArray' => [
120+
'initialArray' => [
121+
'TOTAL_BASE_CALCULATION' => 'TOTAL_BASE_CALCULATION',
122+
'optionTemplate' => '<%= data.label %><% if (data.finalPrice.value) '
123+
. '{ %> +<%- data.finalPrice.formatted %><% } %>',
124+
],
125+
'weeeEnabled' => false,
126+
'weeeDisplay' => WeeeDisplayConfig::DISPLAY_INCL, // has no effect for this scenario
127+
'priceDisplay' => TaxConfig::DISPLAY_TYPE_EXCLUDING_TAX, // has no effect for this scenario
128+
'expectedArray' => [
129+
'TOTAL_BASE_CALCULATION' => 'TOTAL_BASE_CALCULATION',
130+
'optionTemplate' => '<%= data.label %><% if (data.finalPrice.value) '
131+
. '{ %> +<%- data.finalPrice.formatted %><% } %>',
132+
],
133+
],
134+
135+
'weee enabled, and display with Weee included in the price' => [
136+
'initialArray' => [
103137
'TOTAL_BASE_CALCULATION' => 'TOTAL_BASE_CALCULATION',
104138
'optionTemplate' => '<%= data.label %><% if (data.basePrice.value) '
105139
. '{ %> +<%- data.basePrice.formatted %><% } %>',
106140
],
107-
'weeeEnabled' => false,
108-
'weeeDisplayExclDescIncl' => true,
141+
'weeeEnabled' => true,
142+
'weeeDisplay' => WeeeDisplayConfig::DISPLAY_INCL,
143+
'priceDisplay' => TaxConfig::DISPLAY_TYPE_INCLUDING_TAX,
109144
'expectedArray' => [
110145
'TOTAL_BASE_CALCULATION' => 'TOTAL_BASE_CALCULATION',
111146
'optionTemplate' => '<%= data.label %><% if (data.basePrice.value) '
112147
. '{ %> +<%- data.basePrice.formatted %><% } %>',
113148
],
114149
],
115150

116-
'weee enabled, but not displaying ExclDescIncl' => [
117-
'testArray' => [
151+
'weee enabled, and display with Weee included in the price, and include the Weee descriptions' => [
152+
'initialArray' => [
118153
'TOTAL_BASE_CALCULATION' => 'TOTAL_BASE_CALCULATION',
119154
'optionTemplate' => '<%= data.label %><% if (data.basePrice.value) '
120155
. '{ %> +<%- data.basePrice.formatted %><% } %>',
121156
],
122157
'weeeEnabled' => true,
123-
'weeeDisplayExclDescIncl' => false,
158+
'weeeDisplay' => WeeeDisplayConfig::DISPLAY_INCL_DESCR,
159+
'priceDisplay' => TaxConfig::DISPLAY_TYPE_INCLUDING_TAX,
124160
'expectedArray' => [
125161
'TOTAL_BASE_CALCULATION' => 'TOTAL_BASE_CALCULATION',
126162
'optionTemplate' => '<%= data.label %><% if (data.basePrice.value) '
127163
. '{ %> +<%- data.basePrice.formatted %><% } %> <% if (data.weeePricefpt1) '
128-
. '{ %> (: <%- data.weeePricefpt1.formatted %>)<% } %>'
129-
. ' <% if (data.weeePricefpt2) { %> (: <%- data.weeePricefpt2.formatted %>)<% } %>',
164+
. '{ %> (: <%- data.weeePricefpt1.formatted %>)<% } %> '
165+
. '<% if (data.weeePricefpt2) { %> (: <%- data.weeePricefpt2.formatted %>)<% } %>',
130166
],
131167
],
132168

133169
'weee enabled, and display with ExclDescIncl' => [
134-
'testArray' => [
170+
'initialArray' => [
135171
'TOTAL_BASE_CALCULATION' => 'TOTAL_BASE_CALCULATION',
136172
'optionTemplate' => '<%= data.label %><% if (data.basePrice.value) '
137173
. '{ %> +<%- data.basePrice.formatted %><% } %>',
138174
],
139175
'weeeEnabled' => true,
140-
'weeeDisplayExclDescIncl' => true,
176+
'weeeDisplay' => WeeeDisplayConfig::DISPLAY_EXCL_DESCR_INCL,
177+
'priceDisplay' => TaxConfig::DISPLAY_TYPE_INCLUDING_TAX,
141178
'expectedArray' => [
142179
'TOTAL_BASE_CALCULATION' => 'TOTAL_BASE_CALCULATION',
143180
'optionTemplate' => '<%= data.label %><% if (data.basePrice.value) '
@@ -147,6 +184,34 @@ public function updateProductOptionsProvider()
147184
. '<% if (data.weeePrice) { %><%- data.weeePrice.formatted %><% } %>',
148185
],
149186
],
187+
188+
'weee enabled, and display prices including tax but without Weee' => [
189+
'initialArray' => [
190+
'TOTAL_BASE_CALCULATION' => 'TOTAL_BASE_CALCULATION',
191+
],
192+
'weeeEnabled' => true,
193+
'weeeDisplay' => WeeeDisplayConfig::DISPLAY_EXCL,
194+
'priceDisplay' => TaxConfig::DISPLAY_TYPE_INCLUDING_TAX,
195+
'expectedArray' => [
196+
'TOTAL_BASE_CALCULATION' => 'TOTAL_BASE_CALCULATION',
197+
'optionTemplate' => '<%- data.label %><% if (data.finalPrice.value) '
198+
. '{ %> +<%- data.finalPrice.formatted %><% } %>',
199+
],
200+
],
201+
202+
'weee enabled, and display prices excluding tax but without Weee' => [
203+
'initialArray' => [
204+
'TOTAL_BASE_CALCULATION' => 'TOTAL_BASE_CALCULATION',
205+
],
206+
'weeeEnabled' => true,
207+
'weeeDisplay' => WeeeDisplayConfig::DISPLAY_EXCL,
208+
'priceDisplay' => TaxConfig::DISPLAY_TYPE_EXCLUDING_TAX,
209+
'expectedArray' => [
210+
'TOTAL_BASE_CALCULATION' => 'TOTAL_BASE_CALCULATION',
211+
'optionTemplate' => '<%- data.label %><% if (data.basePrice.value) '
212+
. '{ %> +<%- data.basePrice.formatted %><% } %>',
213+
],
214+
],
150215
];
151216
}
152217
}

lib/internal/Magento/Framework/Pricing/Adjustment/Calculator.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public function __construct(AmountFactory $amountFactory)
4040
public function getAmount($amount, SaleableInterface $saleableItem, $exclude = null, $context = [])
4141
{
4242
$baseAmount = $fullAmount = $amount;
43+
$previousAdjustments = 0;
4344
$adjustments = [];
4445
foreach ($saleableItem->getPriceInfo()->getAdjustments() as $adjustment) {
4546
$code = $adjustment->getAdjustmentCode();
@@ -51,7 +52,7 @@ public function getAmount($amount, SaleableInterface $saleableItem, $exclude = n
5152
$adjust = $adjustment->extractAdjustment($baseAmount, $saleableItem, $context);
5253
$baseAmount -= $adjust;
5354
$fullAmount = $adjustment->applyAdjustment($fullAmount, $saleableItem, $context);
54-
$adjust = $fullAmount - $baseAmount;
55+
$adjust = $fullAmount - $baseAmount - $previousAdjustments;
5556
if (!$toExclude) {
5657
$adjustments[$code] = $adjust;
5758
}
@@ -63,6 +64,7 @@ public function getAmount($amount, SaleableInterface $saleableItem, $exclude = n
6364
$adjust = $newAmount - $fullAmount;
6465
$adjustments[$code] = $adjust;
6566
$fullAmount = $newAmount;
67+
$previousAdjustments += $adjust;
6668
}
6769
}
6870

0 commit comments

Comments
 (0)