File tree Expand file tree Collapse file tree 4 files changed +29
-8
lines changed
app/code/Magento/Catalog/Model
dev/tests/unit/testsuite/Magento
lib/internal/Magento/Framework/Pricing/Price Expand file tree Collapse file tree 4 files changed +29
-8
lines changed Original file line number Diff line number Diff line change @@ -777,8 +777,10 @@ public function afterSave()
777
777
*/
778
778
public function setQty ($ qty )
779
779
{
780
- $ this ->setData ('qty ' , $ qty );
781
- $ this ->reloadPriceInfo ();
780
+ if ($ this ->getData ('qty ' ) != $ qty ) {
781
+ $ this ->setData ('qty ' , $ qty );
782
+ $ this ->reloadPriceInfo ();
783
+ }
782
784
return $ this ;
783
785
}
784
786
Original file line number Diff line number Diff line change @@ -424,10 +424,16 @@ public function testGetPriceInfo()
424
424
*/
425
425
public function testSetQty ()
426
426
{
427
- $ this ->productTypeInstanceMock ->expects ($ this ->once ( ))
427
+ $ this ->productTypeInstanceMock ->expects ($ this ->exactly ( 2 ))
428
428
->method ('getPriceInfo ' )
429
429
->with ($ this ->equalTo ($ this ->model ))
430
430
->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
431
437
$ this ->assertEquals ($ this ->model , $ this ->model ->setQty (1 ));
432
438
$ this ->assertEquals ($ this ->model ->getPriceInfo (), $ this ->_priceInfoMock );
433
439
}
Original file line number Diff line number Diff line change @@ -80,6 +80,8 @@ public function testGet()
80
80
)
81
81
->will ($ this ->returnValue ($ this ->priceMock ));
82
82
$ 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 ' ));
83
85
}
84
86
85
87
/**
Original file line number Diff line number Diff line change @@ -43,6 +43,13 @@ class Collection implements \Iterator
43
43
*/
44
44
protected $ excludes ;
45
45
46
+ /**
47
+ * Cached price models
48
+ *
49
+ * @var array
50
+ */
51
+ protected $ priceModels ;
52
+
46
53
/**
47
54
* Constructor
48
55
*
@@ -61,6 +68,7 @@ public function __construct(
61
68
$ this ->priceFactory = $ priceFactory ;
62
69
$ this ->pool = $ pool ;
63
70
$ this ->quantity = $ quantity ;
71
+ $ this ->priceModels = [];
64
72
}
65
73
66
74
/**
@@ -121,10 +129,13 @@ public function valid()
121
129
*/
122
130
public function get ($ code )
123
131
{
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 ];
129
140
}
130
141
}
You can’t perform that action at this time.
0 commit comments