Skip to content

Commit a2503eb

Browse files
author
Yu Tang
committed
MAGETWO-36325: Catalog Prices on Frontend do not include the cost of required Custom Option
- Fixed exception when option is of text or field type
1 parent 83f597a commit a2503eb

File tree

2 files changed

+81
-4
lines changed

2 files changed

+81
-4
lines changed

app/code/Magento/Catalog/Pricing/Price/CustomOptionPrice.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ public function getValue()
4242
$min = 0.;
4343
}
4444
$max = 0.;
45-
/** @var $optionValue \Magento\Catalog\Model\Product\Option\Value */
46-
foreach ($optionItem->getValues() as $optionValue) {
47-
$price = $optionValue->getPrice($optionValue->getPriceType() == Value::TYPE_PERCENT);
45+
if ($optionItem->getValues() === null && $optionItem->getPrice() !== null) {
46+
$price = $optionItem->getPrice($optionItem->getPriceType() == Value::TYPE_PERCENT);
4847
if ($min === null) {
4948
$min = $price;
5049
} elseif ($price < $min) {
@@ -53,6 +52,19 @@ public function getValue()
5352
if ($price > $max) {
5453
$max = $price;
5554
}
55+
} else {
56+
/** @var $optionValue \Magento\Catalog\Model\Product\Option\Value */
57+
foreach ($optionItem->getValues() as $optionValue) {
58+
$price = $optionValue->getPrice($optionValue->getPriceType() == Value::TYPE_PERCENT);
59+
if ($min === null) {
60+
$min = $price;
61+
} elseif ($price < $min) {
62+
$min = $price;
63+
}
64+
if ($price > $max) {
65+
$max = $price;
66+
}
67+
}
5668
}
5769
$optionValues[] = [
5870
'option_id' => $optionItem->getId(),

app/code/Magento/Catalog/Test/Unit/Pricing/Price/CustomOptionPriceTest.php

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use \Magento\Catalog\Pricing\Price\CustomOptionPrice;
99

1010
use Magento\Framework\Pricing\PriceInfoInterface;
11+
use Magento\Catalog\Model\Product\Option\Value;
1112

1213
/**
1314
* Class OptionPriceTest
@@ -123,6 +124,46 @@ protected function setupOptions(array $optionsData)
123124
return $options;
124125
}
125126

127+
protected function setupSingleValueOptions($optionsData)
128+
{
129+
$options = [];
130+
foreach ($optionsData as $optionData) {
131+
$optionItemMock = $this->getMockBuilder('Magento\Catalog\Model\Product\Option')
132+
->disableOriginalConstructor()
133+
->setMethods([
134+
'getValues',
135+
'__wakeup',
136+
'getIsRequire',
137+
'getId',
138+
'getType',
139+
'getPriceType',
140+
'getPrice',
141+
])
142+
->getMock();
143+
$optionItemMock->expects($this->any())
144+
->method('getId')
145+
->will($this->returnValue($optionData['id']));
146+
$optionItemMock->expects($this->any())
147+
->method('getType')
148+
->will($this->returnValue($optionData['type']));
149+
$optionItemMock->expects($this->any())
150+
->method('getIsRequire')
151+
->will($this->returnValue($optionData['is_require']));
152+
$optionItemMock->expects($this->any())
153+
->method('getValues')
154+
->will($this->returnValue(null));
155+
$optionItemMock->expects($this->any())
156+
->method('getPriceType')
157+
->willReturn($optionData['price_type']);
158+
$optionItemMock->expects($this->any())
159+
->method('getPrice')
160+
->with($optionData['price_type'] == Value::TYPE_PERCENT)
161+
->willReturn($optionData['price']);
162+
$options[] = $optionItemMock;
163+
}
164+
return $options;
165+
}
166+
126167
/**
127168
* Test getValue()
128169
*/
@@ -133,7 +174,7 @@ public function testGetValue()
133174
$option1MinPrice = 10;
134175
$option1Type = 'select';
135176

136-
$option2Id = '2';
177+
$option2Id = 2;
137178
$option2MaxPrice = 200;
138179
$option2MinPrice = 20;
139180
$option2Type = 'choice';
@@ -154,7 +195,25 @@ public function testGetValue()
154195
'is_require' => false,
155196
]
156197
];
198+
199+
$singleValueOptionId = 3;
200+
$singleValueOptionPrice = '50';
201+
$singleValueOptionType = 'text';
202+
203+
$singleValueOptions = $this->setupSingleValueOptions(
204+
[
205+
[
206+
'id' => $singleValueOptionId,
207+
'type' => $singleValueOptionType,
208+
'price' => $singleValueOptionPrice,
209+
'price_type' => 'fixed',
210+
'is_require' => true,
211+
],
212+
]
213+
);
214+
157215
$options = $this->setupOptions($optionsData);
216+
$options[] = $singleValueOptions[0];
158217
$this->product->expects($this->once())
159218
->method('getOptions')
160219
->will($this->returnValue($options));
@@ -171,6 +230,12 @@ public function testGetValue()
171230
'type' => $option2Type,
172231
'min' => 0.,
173232
'max' => $option2MaxPrice,
233+
],
234+
[
235+
'option_id' => $singleValueOptionId,
236+
'type' => $singleValueOptionType,
237+
'min' => $singleValueOptionPrice,
238+
'max' => $singleValueOptionPrice,
174239
]
175240
];
176241
$result = $this->object->getValue();

0 commit comments

Comments
 (0)