Skip to content

Commit b1ceba9

Browse files
author
Yu Tang
committed
MAGETWO-32364: Improve performance in pricing framework
1 parent a437b12 commit b1ceba9

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
@@ -776,8 +776,10 @@ public function afterSave()
776776
*/
777777
public function setQty($qty)
778778
{
779-
$this->setData('qty', $qty);
780-
$this->reloadPriceInfo();
779+
if ($this->getData('qty') != $qty) {
780+
$this->setData('qty', $qty);
781+
$this->reloadPriceInfo();
782+
}
781783
return $this;
782784
}
783785

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

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

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

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

8486
/**

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

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

45+
/**
46+
* Cached price models
47+
*
48+
* @var array
49+
*/
50+
protected $priceModels;
51+
4552
/**
4653
* Constructor
4754
*
@@ -60,6 +67,7 @@ public function __construct(
6067
$this->priceFactory = $priceFactory;
6168
$this->pool = $pool;
6269
$this->quantity = $quantity;
70+
$this->priceModels = [];
6371
}
6472

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

0 commit comments

Comments
 (0)