Skip to content

Commit d6d4a1d

Browse files
author
Andrii Kasian
committed
Merge branch 'MAGETWO-36825' into S69
2 parents b0fa8b7 + f3c0912 commit d6d4a1d

File tree

4 files changed

+85
-15
lines changed

4 files changed

+85
-15
lines changed

app/code/Magento/Quote/Model/Quote/Item.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,16 +316,14 @@ protected function _prepareQty($qty)
316316
*/
317317
public function addQty($qty)
318318
{
319-
$oldQty = $this->getQty();
320-
$qty = $this->_prepareQty($qty);
321-
322319
/**
323320
* We can't modify quantity of existing items which have parent
324321
* This qty declared just once during add process and is not editable
325322
*/
326323
if (!$this->getParentItem() || !$this->getId()) {
324+
$qty = $this->_prepareQty($qty);
327325
$this->setQtyToAdd($qty);
328-
$this->setQty($oldQty + $qty);
326+
$this->setQty($this->getQty() + $qty);
329327
}
330328
return $this;
331329
}

app/code/Magento/Quote/Model/Quote/Item/Processor.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Store\Model\StoreManagerInterface;
1212
use Magento\Framework\App\State;
1313
use Magento\Framework\Object;
14+
use Magento\Quote\Api\Data\CartItemInterface;
1415

1516
/**
1617
* Class Processor
@@ -74,7 +75,7 @@ public function init(Product $product, $request)
7475
$item->setProduct($product);
7576

7677
if ($request->getResetCount() && !$product->getStickWithinParent() && $item->getId() === $request->getId()) {
77-
$item->setData('qty', 0);
78+
$item->setData(CartItemInterface::KEY_QTY, 0);
7879
}
7980

8081
return $item;
@@ -84,7 +85,7 @@ public function init(Product $product, $request)
8485
* Set qty and custom price for quote item
8586
*
8687
* @param Item $item
87-
* @param Object $request
88+
* @param \Magento\Framework\Object $request
8889
* @param Product $candidate
8990
* @return void
9091
*/
@@ -93,6 +94,9 @@ public function prepare(Item $item, Object $request, Product $candidate)
9394
/**
9495
* We specify qty after we know about parent (for stock)
9596
*/
97+
if ($request->getResetCount()) {
98+
$item->setData(CartItemInterface::KEY_QTY, 0);
99+
}
96100
$item->addQty($candidate->getCartQty());
97101

98102
$customPrice = $request->getCustomPrice();

app/code/Magento/Quote/Test/Unit/Model/Quote/Item/ProcessorTest.php

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
*/
66
namespace Magento\Quote\Test\Unit\Model\Quote\Item;
77

8-
use \Magento\Quote\Model\Quote\Item\Processor;
9-
10-
use \Magento\Catalog\Model\Product;
8+
use Magento\Catalog\Model\Product;
9+
use Magento\Quote\Api\Data\CartItemInterface;
10+
use Magento\Quote\Model\Quote\Item\Processor;
1111
use Magento\Quote\Model\Quote\ItemFactory;
1212
use Magento\Quote\Model\Quote\Item;
1313
use Magento\Store\Model\StoreManagerInterface;
@@ -109,7 +109,7 @@ protected function setUp()
109109

110110
$this->productMock = $this->getMock(
111111
'Magento\Catalog\Model\Product',
112-
['getCustomOptions', '__wakeup', 'getParentProductId'],
112+
['getCustomOptions', '__wakeup', 'getParentProductId', 'getCartQty'],
113113
[],
114114
'',
115115
false
@@ -239,31 +239,60 @@ public function testPrepare()
239239
$qty = 3000000000;
240240
$customPrice = 400000000;
241241

242+
$this->productMock->expects($this->any())
243+
->method('getCartQty')
244+
->will($this->returnValue($qty));
245+
242246
$this->itemMock->expects($this->any())
243247
->method('addQty')
244-
->will($this->returnValue($qty));
248+
->with($qty);
249+
250+
$this->objectMock->expects($this->any())
251+
->method('getCustomPrice')
252+
->will($this->returnValue($customPrice));
245253

246254
$this->itemMock->expects($this->any())
247255
->method('setCustomPrice')
248256
->will($this->returnValue($customPrice));
249-
250257
$this->itemMock->expects($this->any())
251258
->method('setOriginalCustomPrice')
252259
->will($this->returnValue($customPrice));
253260

254-
$this->itemMock->expects($this->any())
255-
->method('addQty')
256-
->will($this->returnValue($qty));
261+
$this->processor->prepare($this->itemMock, $this->objectMock, $this->productMock);
262+
}
263+
264+
public function testPrepareResetCount()
265+
{
266+
$qty = 3000000000;
267+
$customPrice = 400000000;
257268

269+
$this->objectMock->expects($this->any())
270+
->method('getResetCount')
271+
->will($this->returnValue(true));
272+
273+
$this->itemMock->expects($this->any())
274+
->method('setData')
275+
->with(CartItemInterface::KEY_QTY, 0);
258276

259277
$this->productMock->expects($this->any())
260278
->method('getCartQty')
261279
->will($this->returnValue($qty));
262280

281+
$this->itemMock->expects($this->any())
282+
->method('addQty')
283+
->with($qty);
284+
263285
$this->objectMock->expects($this->any())
264286
->method('getCustomPrice')
265287
->will($this->returnValue($customPrice));
266288

289+
$this->itemMock->expects($this->any())
290+
->method('setCustomPrice')
291+
->will($this->returnValue($customPrice));
292+
$this->itemMock->expects($this->any())
293+
->method('setOriginalCustomPrice')
294+
->will($this->returnValue($customPrice));
295+
267296
$this->processor->prepare($this->itemMock, $this->objectMock, $this->productMock);
268297
}
269298
}

dev/tests/integration/testsuite/Magento/Quote/Model/QuoteTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ private function convertToArray($entity)
1616
->create('Magento\Framework\Api\ExtensibleDataObjectConverter')
1717
->toFlatArray($entity);
1818
}
19+
1920
/**
2021
* @magentoDataFixture Magento/Catalog/_files/product_virtual.php
2122
* @magentoDataFixture Magento/Sales/_files/quote.php
@@ -277,6 +278,44 @@ public function testAssignCustomerWithAddressChange()
277278
}
278279
}
279280

281+
/**
282+
* @magentoDataFixture Magento/Catalog/_files/product_simple_duplicated.php
283+
*/
284+
public function testAddProductUpdateItem()
285+
{
286+
/** @var \Magento\Quote\Model\Quote $quote */
287+
$quote = Bootstrap::getObjectManager()->create('Magento\Quote\Model\Quote');
288+
$quote->load('test01', 'reserved_order_id');
289+
290+
$productStockQty = 100;
291+
/** @var \Magento\Catalog\Model\Product $product */
292+
$product = Bootstrap::getObjectManager()->create('Magento\Catalog\Model\Product');
293+
$product->load(2);
294+
$quote->addProduct($product, 50);
295+
$quote->setTotalsCollectedFlag(false)->collectTotals();
296+
$this->assertEquals(50, $quote->getItemsQty());
297+
$quote->addProduct($product, 50);
298+
$quote->setTotalsCollectedFlag(false)->collectTotals();
299+
$this->assertEquals(100, $quote->getItemsQty());
300+
$params = [
301+
'related_product' => '',
302+
'product' => $product->getId(),
303+
'qty' => 1,
304+
'id' => 0
305+
];
306+
$updateParams = new \Magento\Framework\Object($params);
307+
$quote->updateItem($updateParams['id'], $updateParams);
308+
$quote->setTotalsCollectedFlag(false)->collectTotals();
309+
$this->assertEquals(1, $quote->getItemsQty());
310+
311+
$this->setExpectedException(
312+
'\Magento\Framework\Exception\LocalizedException',
313+
'We don\'t have as many "Simple Product" as you requested.'
314+
);
315+
$updateParams['qty'] = $productStockQty + 1;
316+
$quote->updateItem($updateParams['id'], $updateParams);
317+
}
318+
280319
/**
281320
* Prepare quote for testing assignCustomerWithAddressChange method.
282321
*

0 commit comments

Comments
 (0)