6
6
7
7
namespace Magento \CatalogInventory \Model \ResourceModel ;
8
8
9
+ use Magento \Catalog \Model \ResourceModel \Product \Collection ;
9
10
use Magento \CatalogInventory \Api \StockConfigurationInterface ;
11
+ use Magento \CatalogInventory \Model \Configuration ;
12
+ use Magento \Framework \App \Config \ScopeConfigInterface ;
13
+ use Magento \Framework \DB \Select ;
14
+ use Magento \Framework \Model \ResourceModel \Db \AbstractDb ;
15
+ use Magento \Framework \Model \ResourceModel \Db \Context ;
16
+ use Magento \Framework \Stdlib \DateTime \DateTime ;
17
+ use Magento \Store \Model \ScopeInterface ;
10
18
use Magento \Store \Model \StoreManagerInterface ;
11
19
12
20
/**
13
21
* Stock resource model
14
22
*/
15
- class Stock extends \ Magento \ Framework \ Model \ ResourceModel \ Db \ AbstractDb implements QtyCounterInterface
23
+ class Stock extends AbstractDb implements QtyCounterInterface
16
24
{
17
25
/**
18
26
* @var StockConfigurationInterface
@@ -64,12 +72,12 @@ class Stock extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb impleme
64
72
/**
65
73
* Core store config
66
74
*
67
- * @var \Magento\Framework\App\Config\ ScopeConfigInterface
75
+ * @var ScopeConfigInterface
68
76
*/
69
77
protected $ _scopeConfig ;
70
78
71
79
/**
72
- * @var \Magento\Framework\Stdlib\DateTime\ DateTime
80
+ * @var DateTime
73
81
*/
74
82
protected $ dateTime ;
75
83
@@ -80,17 +88,17 @@ class Stock extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb impleme
80
88
protected $ storeManager ;
81
89
82
90
/**
83
- * @param \Magento\Framework\Model\ResourceModel\Db\ Context $context
84
- * @param \Magento\Framework\App\Config\ ScopeConfigInterface $scopeConfig
85
- * @param \Magento\Framework\Stdlib\DateTime\ DateTime $dateTime
91
+ * @param Context $context
92
+ * @param ScopeConfigInterface $scopeConfig
93
+ * @param DateTime $dateTime
86
94
* @param StockConfigurationInterface $stockConfiguration
87
95
* @param StoreManagerInterface $storeManager
88
96
* @param string $connectionName
89
97
*/
90
98
public function __construct (
91
- \ Magento \ Framework \ Model \ ResourceModel \ Db \ Context $ context ,
92
- \ Magento \ Framework \ App \ Config \ ScopeConfigInterface $ scopeConfig ,
93
- \ Magento \ Framework \ Stdlib \ DateTime \ DateTime $ dateTime ,
99
+ Context $ context ,
100
+ ScopeConfigInterface $ scopeConfig ,
101
+ DateTime $ dateTime ,
94
102
StockConfigurationInterface $ stockConfiguration ,
95
103
StoreManagerInterface $ storeManager ,
96
104
$ connectionName = null
@@ -125,9 +133,18 @@ public function lockProductsStock(array $productIds, $websiteId)
125
133
return [];
126
134
}
127
135
$ itemTable = $ this ->getTable ('cataloginventory_stock_item ' );
128
- $ select = $ this ->getConnection ()->select ()->from (['si ' => $ itemTable ])
136
+
137
+ //get indexed entries for row level lock instead of table lock
138
+ $ itemIds = [];
139
+ $ preSelect = $ this ->getConnection ()->select ()->from ($ itemTable , 'item_id ' )
129
140
->where ('website_id = ? ' , $ websiteId )
130
- ->where ('product_id IN(?) ' , $ productIds )
141
+ ->where ('product_id IN(?) ' , $ productIds );
142
+ foreach ($ this ->getConnection ()->query ($ preSelect )->fetchAll () as $ item ) {
143
+ $ itemIds [] = (int )$ item ['item_id ' ];
144
+ }
145
+
146
+ $ select = $ this ->getConnection ()->select ()->from (['si ' => $ itemTable ])
147
+ ->where ('item_id IN (?) ' , $ itemIds )
131
148
->forUpdate (true );
132
149
133
150
$ productTable = $ this ->getTable ('catalog_product_entity ' );
@@ -147,12 +164,12 @@ public function lockProductsStock(array $productIds, $websiteId)
147
164
foreach ($ this ->getConnection ()->fetchAll ($ selectProducts ) as $ p ) {
148
165
$ items [$ p ['product_id ' ]]['type_id ' ] = $ p ['type_id ' ];
149
166
}
150
-
167
+
151
168
return $ items ;
152
169
}
153
170
154
171
/**
155
- * { @inheritdoc}
172
+ * @inheritdoc
156
173
*/
157
174
public function correctItemsQty (array $ items , $ websiteId , $ operator )
158
175
{
@@ -185,16 +202,16 @@ protected function _initConfig()
185
202
{
186
203
if (!$ this ->_isConfig ) {
187
204
$ configMap = [
188
- '_isConfigManageStock ' => \ Magento \ CatalogInventory \ Model \ Configuration::XML_PATH_MANAGE_STOCK ,
189
- '_isConfigBackorders ' => \ Magento \ CatalogInventory \ Model \ Configuration::XML_PATH_BACKORDERS ,
190
- '_configMinQty ' => \ Magento \ CatalogInventory \ Model \ Configuration::XML_PATH_MIN_QTY ,
191
- '_configNotifyStockQty ' => \ Magento \ CatalogInventory \ Model \ Configuration::XML_PATH_NOTIFY_STOCK_QTY ,
205
+ '_isConfigManageStock ' => Configuration::XML_PATH_MANAGE_STOCK ,
206
+ '_isConfigBackorders ' => Configuration::XML_PATH_BACKORDERS ,
207
+ '_configMinQty ' => Configuration::XML_PATH_MIN_QTY ,
208
+ '_configNotifyStockQty ' => Configuration::XML_PATH_NOTIFY_STOCK_QTY ,
192
209
];
193
210
194
211
foreach ($ configMap as $ field => $ const ) {
195
212
$ this ->{$ field } = (int ) $ this ->_scopeConfig ->getValue (
196
213
$ const ,
197
- \ Magento \ Store \ Model \ ScopeInterface::SCOPE_STORE
214
+ ScopeInterface::SCOPE_STORE
198
215
);
199
216
}
200
217
@@ -317,11 +334,11 @@ public function updateLowStockDate($website)
317
334
/**
318
335
* Add low stock filter to product collection
319
336
*
320
- * @param \Magento\Catalog\Model\ResourceModel\Product\ Collection $collection
337
+ * @param Collection $collection
321
338
* @param array $fields
322
339
* @return $this
323
340
*/
324
- public function addLowStockFilter (\ Magento \ Catalog \ Model \ ResourceModel \ Product \ Collection $ collection , $ fields )
341
+ public function addLowStockFilter (Collection $ collection , $ fields )
325
342
{
326
343
$ this ->_initConfig ();
327
344
$ connection = $ collection ->getSelect ()->getConnection ();
@@ -344,14 +361,14 @@ public function addLowStockFilter(\Magento\Catalog\Model\ResourceModel\Product\C
344
361
345
362
$ where = [];
346
363
foreach ($ conditions as $ k => $ part ) {
347
- $ where [$ k ] = join (' ' . \ Magento \ Framework \ DB \ Select::SQL_AND . ' ' , $ part );
364
+ $ where [$ k ] = join (' ' . Select::SQL_AND . ' ' , $ part );
348
365
}
349
366
350
367
$ where = $ connection ->prepareSqlCondition (
351
368
'invtr.low_stock_date ' ,
352
369
['notnull ' => true ]
353
- ) . ' ' . \ Magento \ Framework \ DB \ Select::SQL_AND . ' (( ' . join (
354
- ') ' . \ Magento \ Framework \ DB \ Select::SQL_OR . ' ( ' ,
370
+ ) . ' ' . Select::SQL_AND . ' (( ' . join (
371
+ ') ' . Select::SQL_OR . ' ( ' ,
355
372
$ where
356
373
) . ')) ' ;
357
374
0 commit comments