66
77namespace Magento \CatalogInventory \Model \ResourceModel ;
88
9+ use Magento \Catalog \Model \ResourceModel \Product \Collection ;
910use 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 ;
1018use Magento \Store \Model \StoreManagerInterface ;
1119
1220/**
1321 * Stock resource model
1422 */
15- class Stock extends \ Magento \ Framework \ Model \ ResourceModel \ Db \ AbstractDb implements QtyCounterInterface
23+ class Stock extends AbstractDb implements QtyCounterInterface
1624{
1725 /**
1826 * @var StockConfigurationInterface
@@ -64,12 +72,12 @@ class Stock extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb impleme
6472 /**
6573 * Core store config
6674 *
67- * @var \Magento\Framework\App\Config\ ScopeConfigInterface
75+ * @var ScopeConfigInterface
6876 */
6977 protected $ _scopeConfig ;
7078
7179 /**
72- * @var \Magento\Framework\Stdlib\DateTime\ DateTime
80+ * @var DateTime
7381 */
7482 protected $ dateTime ;
7583
@@ -80,17 +88,17 @@ class Stock extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb impleme
8088 protected $ storeManager ;
8189
8290 /**
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
8694 * @param StockConfigurationInterface $stockConfiguration
8795 * @param StoreManagerInterface $storeManager
8896 * @param string $connectionName
8997 */
9098 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 ,
94102 StockConfigurationInterface $ stockConfiguration ,
95103 StoreManagerInterface $ storeManager ,
96104 $ connectionName = null
@@ -125,9 +133,18 @@ public function lockProductsStock(array $productIds, $websiteId)
125133 return [];
126134 }
127135 $ 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 ' )
129140 ->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 )
131148 ->forUpdate (true );
132149
133150 $ productTable = $ this ->getTable ('catalog_product_entity ' );
@@ -147,12 +164,12 @@ public function lockProductsStock(array $productIds, $websiteId)
147164 foreach ($ this ->getConnection ()->fetchAll ($ selectProducts ) as $ p ) {
148165 $ items [$ p ['product_id ' ]]['type_id ' ] = $ p ['type_id ' ];
149166 }
150-
167+
151168 return $ items ;
152169 }
153170
154171 /**
155- * { @inheritdoc}
172+ * @inheritdoc
156173 */
157174 public function correctItemsQty (array $ items , $ websiteId , $ operator )
158175 {
@@ -185,16 +202,16 @@ protected function _initConfig()
185202 {
186203 if (!$ this ->_isConfig ) {
187204 $ 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 ,
192209 ];
193210
194211 foreach ($ configMap as $ field => $ const ) {
195212 $ this ->{$ field } = (int ) $ this ->_scopeConfig ->getValue (
196213 $ const ,
197- \ Magento \ Store \ Model \ ScopeInterface::SCOPE_STORE
214+ ScopeInterface::SCOPE_STORE
198215 );
199216 }
200217
@@ -317,11 +334,11 @@ public function updateLowStockDate($website)
317334 /**
318335 * Add low stock filter to product collection
319336 *
320- * @param \Magento\Catalog\Model\ResourceModel\Product\ Collection $collection
337+ * @param Collection $collection
321338 * @param array $fields
322339 * @return $this
323340 */
324- public function addLowStockFilter (\ Magento \ Catalog \ Model \ ResourceModel \ Product \ Collection $ collection , $ fields )
341+ public function addLowStockFilter (Collection $ collection , $ fields )
325342 {
326343 $ this ->_initConfig ();
327344 $ connection = $ collection ->getSelect ()->getConnection ();
@@ -344,14 +361,14 @@ public function addLowStockFilter(\Magento\Catalog\Model\ResourceModel\Product\C
344361
345362 $ where = [];
346363 foreach ($ conditions as $ k => $ part ) {
347- $ where [$ k ] = join (' ' . \ Magento \ Framework \ DB \ Select::SQL_AND . ' ' , $ part );
364+ $ where [$ k ] = join (' ' . Select::SQL_AND . ' ' , $ part );
348365 }
349366
350367 $ where = $ connection ->prepareSqlCondition (
351368 'invtr.low_stock_date ' ,
352369 ['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 . ' ( ' ,
355372 $ where
356373 ) . ')) ' ;
357374
0 commit comments