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 @@ -776,8 +776,10 @@ public function afterSave()
776
776
*/
777
777
public function setQty ($ qty )
778
778
{
779
- $ this ->setData ('qty ' , $ qty );
780
- $ this ->reloadPriceInfo ();
779
+ if ($ this ->getData ('qty ' ) != $ qty ) {
780
+ $ this ->setData ('qty ' , $ qty );
781
+ $ this ->reloadPriceInfo ();
782
+ }
781
783
return $ this ;
782
784
}
783
785
Original file line number Diff line number Diff line change @@ -423,10 +423,16 @@ public function testGetPriceInfo()
423
423
*/
424
424
public function testSetQty ()
425
425
{
426
- $ this ->productTypeInstanceMock ->expects ($ this ->once ( ))
426
+ $ this ->productTypeInstanceMock ->expects ($ this ->exactly ( 2 ))
427
427
->method ('getPriceInfo ' )
428
428
->with ($ this ->equalTo ($ this ->model ))
429
429
->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
430
436
$ this ->assertEquals ($ this ->model , $ this ->model ->setQty (1 ));
431
437
$ this ->assertEquals ($ this ->model ->getPriceInfo (), $ this ->_priceInfoMock );
432
438
}
Original file line number Diff line number Diff line change @@ -79,6 +79,8 @@ public function testGet()
79
79
)
80
80
->will ($ this ->returnValue ($ this ->priceMock ));
81
81
$ 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 ' ));
82
84
}
83
85
84
86
/**
Original file line number Diff line number Diff line change @@ -42,6 +42,13 @@ class Collection implements \Iterator
42
42
*/
43
43
protected $ excludes ;
44
44
45
+ /**
46
+ * Cached price models
47
+ *
48
+ * @var array
49
+ */
50
+ protected $ priceModels ;
51
+
45
52
/**
46
53
* Constructor
47
54
*
@@ -60,6 +67,7 @@ public function __construct(
60
67
$ this ->priceFactory = $ priceFactory ;
61
68
$ this ->pool = $ pool ;
62
69
$ this ->quantity = $ quantity ;
70
+ $ this ->priceModels = [];
63
71
}
64
72
65
73
/**
@@ -120,10 +128,13 @@ public function valid()
120
128
*/
121
129
public function get ($ code )
122
130
{
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 ];
128
139
}
129
140
}
You can’t perform that action at this time.
0 commit comments