Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
DefaultStock Class Reference
Inheritance diagram for DefaultStock:
AbstractIndexer StockInterface AbstractResource AbstractDb AbstractResource Stock Configurable Grouped

Public Member Functions

 __construct (\Magento\Framework\Model\ResourceModel\Db\Context $context, \Magento\Framework\Indexer\Table\StrategyInterface $tableStrategy, \Magento\Eav\Model\Config $eavConfig, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, $connectionName=null)
 
 reindexAll ()
 
 reindexEntity ($entityIds)
 
 getActionType ()
 
 setActionType ($type)
 
 setTypeId ($typeId)
 
 getTypeId ()
 
 setIsComposite ($flag)
 
 getIsComposite ()
 
 getIdxTable ($table=null)
 
- Public Member Functions inherited from AbstractIndexer
 __construct (\Magento\Framework\Model\ResourceModel\Db\Context $context, \Magento\Framework\Indexer\Table\StrategyInterface $tableStrategy, \Magento\Eav\Model\Config $eavConfig, $connectionName=null)
 
 getRelationsByChild ($childIds)
 
 getRelationsByParent ($parentIds)
 
- Public Member Functions inherited from AbstractResource
 __construct (\Magento\Framework\Model\ResourceModel\Db\Context $context, \Magento\Framework\Indexer\Table\StrategyInterface $tableStrategy, $connectionName=null)
 
 reindexAll ()
 
 getIdxTable ($table=null)
 
 syncData ()
 
 insertFromTable ($sourceTable, $destTable, $readToIndex=true)
 
 insertFromSelect ($select, $destTable, array $columns, $readToIndex=true)
 
 clearTemporaryIndexTable ()
 
- Public Member Functions inherited from AbstractDb
 __construct (\Magento\Framework\Model\ResourceModel\Db\Context $context, $connectionName=null)
 
 __sleep ()
 
 __wakeup ()
 
 getIdFieldName ()
 
 getMainTable ()
 
 getTable ($tableName)
 
 getConnection ()
 
 load (\Magento\Framework\Model\AbstractModel $object, $value, $field=null)
 
 save (\Magento\Framework\Model\AbstractModel $object)
 
 delete (\Magento\Framework\Model\AbstractModel $object)
 
 addUniqueField ($field)
 
 resetUniqueField ()
 
 unserializeFields (\Magento\Framework\Model\AbstractModel $object)
 
 getUniqueFields ()
 
 hasDataChanged ($object)
 
 getChecksum ($table)
 
 afterLoad (\Magento\Framework\DataObject $object)
 
 beforeSave (\Magento\Framework\DataObject $object)
 
 afterSave (\Magento\Framework\DataObject $object)
 
 beforeDelete (\Magento\Framework\DataObject $object)
 
 afterDelete (\Magento\Framework\DataObject $object)
 
 serializeFields (\Magento\Framework\Model\AbstractModel $object)
 
- Public Member Functions inherited from AbstractResource
 __construct ()
 
 getConnection ()
 
 beginTransaction ()
 
 addCommitCallback ($callback)
 
 commit ()
 
 rollBack ()
 
 getValidationRulesBeforeSave ()
 

Protected Member Functions

 _construct ()
 
 _isManageStock ()
 
 _getStockStatusSelect ($entityIds=null, $usePrimaryTable=false)
 
 _prepareIndexTable ($entityIds=null)
 
 _updateIndex ($entityIds)
 
 _updateIndexTable ($data)
 
 getStatusExpression (AdapterInterface $connection, $isAggregate=false)
 
 getStockConfiguration ()
 
- Protected Member Functions inherited from AbstractIndexer
 _getAttribute ($attributeCode)
 
 _addAttributeToSelect ($select, $attrCode, $entity, $store, $condition=null, $required=false)
 
 _addWebsiteJoinToSelect ($select, $store=true, $joinCondition=null)
 
 _addProductWebsiteJoinToSelect ($select, $website, $product)
 
 getMetadataPool ()
 
- Protected Member Functions inherited from AbstractResource
 _getIndexAdapter ()
 
- Protected Member Functions inherited from AbstractDb
 _init ($mainTable, $idFieldName)
 
 _setResource ($connections, $tables=null)
 
 _setMainTable ($mainTable, $idFieldName=null)
 
 _getConnection ($resourceName)
 
 _getLoadSelect ($field, $value, $object)
 
 _initUniqueFields ()
 
 _prepareDataForSave (\Magento\Framework\Model\AbstractModel $object)
 
 _prepareValueForSave ($value, $type)
 
 _checkUnique (\Magento\Framework\Model\AbstractModel $object)
 
 _afterLoad (\Magento\Framework\Model\AbstractModel $object)
 
 _beforeSave (\Magento\Framework\Model\AbstractModel $object)
 
 _afterSave (\Magento\Framework\Model\AbstractModel $object)
 
 _beforeDelete (\Magento\Framework\Model\AbstractModel $object)
 
 _afterDelete (\Magento\Framework\Model\AbstractModel $object)
 
 _serializeFields (\Magento\Framework\Model\AbstractModel $object)
 
 prepareDataForUpdate ($object)
 
 isObjectNotNew (\Magento\Framework\Model\AbstractModel $object)
 
 saveNewObject (\Magento\Framework\Model\AbstractModel $object)
 
 updateObject (\Magento\Framework\Model\AbstractModel $object)
 
 processAfterSaves (\Magento\Framework\Model\AbstractModel $object)
 
 isModified (\Magento\Framework\Model\AbstractModel $object)
 
 processNotModifiedSave (\Magento\Framework\Model\AbstractModel $object)
 
- Protected Member Functions inherited from AbstractResource
 _construct ()
 
 _serializeField (DataObject $object, $field, $defaultValue=null, $unsetEmpty=false)
 
 _unserializeField (DataObject $object, $field, $defaultValue=null)
 
 _prepareDataForTable (DataObject $object, $table)
 
 _prepareTableValueForSave ($value, $type)
 
 _getColumnsForEntityLoad (\Magento\Framework\Model\AbstractModel $object, $tableName)
 
 getSerializer ()
 

Protected Attributes

 $_typeId
 
 $_isComposite = false
 
 $_scopeConfig
 
 $stockConfiguration
 
- Protected Attributes inherited from AbstractIndexer
 $_eavConfig
 
 $metadataPool
 
- Protected Attributes inherited from AbstractResource
 $tableStrategy
 
- Protected Attributes inherited from AbstractDb
 $_resources
 
 $connectionName = \Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION
 
 $_connections = []
 
 $_resourceModel
 
 $_tables = []
 
 $_mainTable
 
 $_idFieldName
 
 $_isPkAutoIncrement = true
 
 $_useIsObjectNew = false
 
 $_mainTableFields
 
 $_uniqueFields = null
 
 $_serializableFields = []
 
 $transactionManager
 
 $objectRelationProcessor
 
- Protected Attributes inherited from AbstractResource
 $serializer
 
 $_logger
 

Detailed Description

CatalogInventory Default Stock Status Indexer Resource Model @SuppressWarnings(PHPMD.CouplingBetweenObjects) @api

Since
100.0.2
Deprecated:
2.3.0 Replaced with Multi Source Inventory https://devdocs.magento.com/guides/v2.3/inventory/catalog-inventory-replacements.html

Definition at line 26 of file DefaultStock.php.

Constructor & Destructor Documentation

◆ __construct()

Class constructor

Parameters
\Magento\Framework\Model\ResourceModel\Db\Context$context
\Magento\Framework\Indexer\Table\StrategyInterface$tableStrategy
\Magento\Eav\Model\Config$eavConfig
\Magento\Framework\App\Config\ScopeConfigInterface$scopeConfig
string$connectionName

Definition at line 76 of file DefaultStock.php.

82  {
83  $this->_scopeConfig = $scopeConfig;
84  parent::__construct($context, $tableStrategy, $eavConfig, $connectionName);
85  }

Member Function Documentation

◆ _construct()

_construct ( )
protected

Initialize connection and define main table name

Returns
void

Definition at line 92 of file DefaultStock.php.

93  {
94  $this->_init('cataloginventory_stock_status', 'product_id');
95  }

◆ _getStockStatusSelect()

_getStockStatusSelect (   $entityIds = null,
  $usePrimaryTable = false 
)
protected

Get the select object for get stock status by product ids

Parameters
int | array$entityIds
bool$usePrimaryTableuse primary or temporary index table
Returns
\Magento\Framework\DB\Select @SuppressWarnings(PHPMD.UnusedFormalParameter)

Definition at line 229 of file DefaultStock.php.

230  {
231  $connection = $this->getConnection();
232  $qtyExpr = $connection->getCheckSql('cisi.qty > 0', 'cisi.qty', 0);
233  $metadata = $this->getMetadataPool()->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class);
234  $linkField = $metadata->getLinkField();
235 
236  $select = $connection->select()->from(
237  ['e' => $this->getTable('catalog_product_entity')],
238  ['entity_id']
239  );
240  $select->join(
241  ['cis' => $this->getTable('cataloginventory_stock')],
242  '',
243  ['website_id', 'stock_id']
244  )->joinInner(
245  ['cisi' => $this->getTable('cataloginventory_stock_item')],
246  'cisi.stock_id = cis.stock_id AND cisi.product_id = e.entity_id',
247  []
248  )->joinInner(
249  ['mcpei' => $this->getTable('catalog_product_entity_int')],
250  'e.' . $linkField . ' = mcpei.' . $linkField
251  . ' AND mcpei.attribute_id = ' . $this->_getAttribute('status')->getId()
252  . ' AND mcpei.value = ' . ProductStatus::STATUS_ENABLED,
253  []
254  )->columns(
255  ['qty' => $qtyExpr]
256  )->where(
257  'cis.website_id = ?',
258  $this->getStockConfiguration()->getDefaultScopeId()
259  )->where('e.type_id = ?', $this->getTypeId())
260  ->group(['e.entity_id', 'cis.website_id', 'cis.stock_id']);
261 
262  $select->columns(['status' => $this->getStatusExpression($connection, true)]);
263  if ($entityIds !== null) {
264  $select->where('e.entity_id IN(?)', $entityIds);
265  }
266 
267  return $select;
268  }
getStatusExpression(AdapterInterface $connection, $isAggregate=false)
$connection
Definition: bulk.php:13

◆ _isManageStock()

_isManageStock ( )
protected

Retrieve is Global Manage Stock enabled

Returns
bool

Definition at line 213 of file DefaultStock.php.

214  {
215  return $this->_scopeConfig->isSetFlag(
216  \Magento\CatalogInventory\Model\Configuration::XML_PATH_MANAGE_STOCK,
218  );
219  }

◆ _prepareIndexTable()

_prepareIndexTable (   $entityIds = null)
protected

Prepare stock status data in temporary index table

Parameters
int | array$entityIdsthe product limitation
Returns
$this

Definition at line 276 of file DefaultStock.php.

277  {
278  $connection = $this->getConnection();
279  $select = $this->_getStockStatusSelect($entityIds, true);
280  $select = $this->getQueryProcessorComposite()->processQuery($select, $entityIds);
281  $query = $select->insertFromSelect($this->getIdxTable());
282  $connection->query($query);
283 
284  return $this;
285  }
$connection
Definition: bulk.php:13

◆ _updateIndex()

_updateIndex (   $entityIds)
protected

Update Stock status index by product ids

Parameters
array | int$entityIds
Returns
$this

Definition at line 293 of file DefaultStock.php.

294  {
295  $connection = $this->getConnection();
296  $select = $this->_getStockStatusSelect($entityIds, true);
297  $select = $this->getQueryProcessorComposite()->processQuery($select, $entityIds, true);
298  $query = $connection->query($select);
299 
300  $i = 0;
301  $data = [];
302  while ($row = $query->fetch(\PDO::FETCH_ASSOC)) {
303  $i++;
304  $data[] = [
305  'product_id' => (int)$row['entity_id'],
306  'website_id' => (int)$row['website_id'],
307  'stock_id' => Stock::DEFAULT_STOCK_ID,
308  'qty' => (double)$row['qty'],
309  'stock_status' => (int)$row['status'],
310  ];
311  if ($i % 1000 == 0) {
312  $this->_updateIndexTable($data);
313  $data = [];
314  }
315  }
316 
317  $this->deleteOldRecords($entityIds);
318  $this->_updateIndexTable($data);
319 
320  return $this;
321  }
$connection
Definition: bulk.php:13
$i
Definition: gallery.phtml:31

◆ _updateIndexTable()

_updateIndexTable (   $data)
protected

Update stock status index table (INSERT ... ON DUPLICATE KEY UPDATE ...)

Parameters
array$data
Returns
$this

Definition at line 344 of file DefaultStock.php.

345  {
346  if (empty($data)) {
347  return $this;
348  }
349 
350  $connection = $this->getConnection();
351  $connection->insertOnDuplicate($this->getMainTable(), $data, ['qty', 'stock_status']);
352 
353  return $this;
354  }
$connection
Definition: bulk.php:13

◆ getActionType()

getActionType ( )

Returns action run type

Returns
string
Since
100.2.0

Definition at line 141 of file DefaultStock.php.

142  {
143  return $this->actionType;
144  }

◆ getIdxTable()

getIdxTable (   $table = null)

Retrieve temporary index table name

Parameters
string$table
Returns
string @SuppressWarnings(PHPMD.UnusedFormalParameter)

Definition at line 363 of file DefaultStock.php.

364  {
365  return $this->tableStrategy->getTableName('cataloginventory_stock_status');
366  }

◆ getIsComposite()

getIsComposite ( )

Check product type is composite

Returns
bool @SuppressWarnings(PHPMD.BooleanGetMethodName)

Definition at line 203 of file DefaultStock.php.

◆ getStatusExpression()

getStatusExpression ( AdapterInterface  $connection,
  $isAggregate = false 
)
protected
Parameters
AdapterInterface$connection
bool$isAggregate
Returns
mixed
Since
100.1.0

Definition at line 374 of file DefaultStock.php.

375  {
376  $isInStockExpression = $isAggregate ? 'MAX(cisi.is_in_stock)' : 'cisi.is_in_stock';
377  if ($this->_isManageStock()) {
378  $statusExpr = $connection->getCheckSql(
379  'cisi.use_config_manage_stock = 0 AND cisi.manage_stock = 0',
380  1,
381  $isInStockExpression
382  );
383  } else {
384  $statusExpr = $connection->getCheckSql(
385  'cisi.use_config_manage_stock = 0 AND cisi.manage_stock = 1',
386  $isInStockExpression,
387  1
388  );
389  }
390  return $statusExpr;
391  }
$connection
Definition: bulk.php:13

◆ getStockConfiguration()

getStockConfiguration ( )
protected
Returns
StockConfigurationInterface
Deprecated:
100.1.0
Since
100.1.0

Definition at line 399 of file DefaultStock.php.

400  {
401  if ($this->stockConfiguration === null) {
402  $this->stockConfiguration = \Magento\Framework\App\ObjectManager::getInstance()
403  ->get(\Magento\CatalogInventory\Api\StockConfigurationInterface::class);
404  }
406  }

◆ getTypeId()

getTypeId ( )

Retrieve active Product Type Id

Returns
string
Exceptions

Implements StockInterface.

Definition at line 177 of file DefaultStock.php.

178  {
179  if ($this->_typeId === null) {
180  throw new \Magento\Framework\Exception\LocalizedException(__('Undefined product type'));
181  }
182  return $this->_typeId;
183  }
__()
Definition: __.php:13

◆ reindexAll()

reindexAll ( )

Reindex all stock status data for default logic product type

Returns
$this
Exceptions

Implements StockInterface.

Definition at line 103 of file DefaultStock.php.

104  {
105  $this->tableStrategy->setUseIdxTable(true);
106  $this->beginTransaction();
107  try {
108  $this->_prepareIndexTable();
109  $this->commit();
110  } catch (\Exception $e) {
111  $this->rollBack();
112  throw $e;
113  }
114  return $this;
115  }

◆ reindexEntity()

reindexEntity (   $entityIds)

Reindex stock data for defined product ids

Parameters
int | array$entityIds
Returns
$this

Implements StockInterface.

Definition at line 123 of file DefaultStock.php.

124  {
125  if ($this->getActionType() === Full::ACTION_TYPE) {
126  $this->tableStrategy->setUseIdxTable(false);
127  $this->_prepareIndexTable($entityIds);
128  return $this;
129  }
130 
131  $this->_updateIndex($entityIds);
132  return $this;
133  }

◆ setActionType()

setActionType (   $type)

Set action run type

Parameters
string$type
Returns
$this
Since
100.2.0

Definition at line 153 of file DefaultStock.php.

154  {
155  $this->actionType = $type;
156  return $this;
157  }
$type
Definition: item.phtml:13

◆ setIsComposite()

setIsComposite (   $flag)

Set Product Type Composite flag

Parameters
bool$flag
Returns
$this

Definition at line 191 of file DefaultStock.php.

192  {
193  $this->_isComposite = (bool) $flag;
194  return $this;
195  }

◆ setTypeId()

setTypeId (   $typeId)

Set active Product Type Id

Parameters
string$typeId
Returns
$this

Implements StockInterface.

Definition at line 165 of file DefaultStock.php.

166  {
167  $this->_typeId = $typeId;
168  return $this;
169  }

Field Documentation

◆ $_isComposite

$_isComposite = false
protected

Definition at line 40 of file DefaultStock.php.

◆ $_scopeConfig

$_scopeConfig
protected

Definition at line 47 of file DefaultStock.php.

◆ $_typeId

$_typeId
protected

Definition at line 33 of file DefaultStock.php.

◆ $stockConfiguration

$stockConfiguration
protected

Definition at line 58 of file DefaultStock.php.


The documentation for this class was generated from the following file: