Skip to content

Commit 9c7786c

Browse files
author
Yu Tang
committed
Merge branch 'MAGETWO-32364-pricing-performance' into develop
2 parents 401627e + a25ab55 commit 9c7786c

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

app/code/Magento/Catalog/Model/Product.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -777,8 +777,10 @@ public function afterSave()
777777
*/
778778
public function setQty($qty)
779779
{
780-
$this->setData('qty', $qty);
781-
$this->reloadPriceInfo();
780+
if ($this->getData('qty') != $qty) {
781+
$this->setData('qty', $qty);
782+
$this->reloadPriceInfo();
783+
}
782784
return $this;
783785
}
784786

dev/tests/unit/testsuite/Magento/Catalog/Model/ProductTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,10 +424,16 @@ public function testGetPriceInfo()
424424
*/
425425
public function testSetQty()
426426
{
427-
$this->productTypeInstanceMock->expects($this->once())
427+
$this->productTypeInstanceMock->expects($this->exactly(2))
428428
->method('getPriceInfo')
429429
->with($this->equalTo($this->model))
430430
->will($this->returnValue($this->_priceInfoMock));
431+
432+
//initialize the priceInfo field
433+
$this->model->getPriceInfo();
434+
//Calling setQty will reset the priceInfo field
435+
$this->assertEquals($this->model, $this->model->setQty(1));
436+
//Call the setQty method with the same qty, getPriceInfo should not be called this time
431437
$this->assertEquals($this->model, $this->model->setQty(1));
432438
$this->assertEquals($this->model->getPriceInfo(), $this->_priceInfoMock);
433439
}

dev/tests/unit/testsuite/Magento/Framework/Pricing/Price/CollectionTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ public function testGet()
8080
)
8181
->will($this->returnValue($this->priceMock));
8282
$this->assertEquals($this->priceMock, $this->collection->get('regular_price'));
83+
//Calling the get method again with the same code, cached copy should be used
84+
$this->assertEquals($this->priceMock, $this->collection->get('regular_price'));
8385
}
8486

8587
/**

lib/internal/Magento/Framework/Pricing/Price/Collection.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ class Collection implements \Iterator
4343
*/
4444
protected $excludes;
4545

46+
/**
47+
* Cached price models
48+
*
49+
* @var array
50+
*/
51+
protected $priceModels;
52+
4653
/**
4754
* Constructor
4855
*
@@ -61,6 +68,7 @@ public function __construct(
6168
$this->priceFactory = $priceFactory;
6269
$this->pool = $pool;
6370
$this->quantity = $quantity;
71+
$this->priceModels = [];
6472
}
6573

6674
/**
@@ -121,10 +129,13 @@ public function valid()
121129
*/
122130
public function get($code)
123131
{
124-
return $this->priceFactory->create(
125-
$this->saleableItem,
126-
$this->pool[$code],
127-
$this->quantity
128-
);
132+
if (!isset($this->priceModels[$code])) {
133+
$this->priceModels[$code] = $this->priceFactory->create(
134+
$this->saleableItem,
135+
$this->pool[$code],
136+
$this->quantity
137+
);
138+
}
139+
return $this->priceModels[$code];
129140
}
130141
}

0 commit comments

Comments
 (0)