Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Attributes
Stock Class Reference
Inheritance diagram for Stock:
AbstractBackend BackendInterface

Public Member Functions

 __construct (\Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry)
 
 afterLoad ($object)
 
 validate ($object)
 
- Public Member Functions inherited from AbstractBackend
 setAttribute ($attribute)
 
 getAttribute ()
 
 getType ()
 
 isStatic ()
 
 getTable ()
 
 getEntityIdField ()
 
 setValueId ($valueId)
 
 setEntityValueId ($entity, $valueId)
 
 getValueId ()
 
 getEntityValueId ($entity)
 
 getDefaultValue ()
 
 validate ($object)
 
 afterLoad ($object)
 
 beforeSave ($object)
 
 afterSave ($object)
 
 beforeDelete ($object)
 
 afterDelete ($object)
 
 getAffectedFields ($object)
 
 isScalar ()
 

Protected Attributes

 $stockRegistry
 
- Protected Attributes inherited from AbstractBackend
 $_attribute
 
 $_valueId
 
 $_valueIds = []
 
 $_table
 
 $_entityIdField
 
 $_defaultValue = null
 

Detailed Description

Quantity and Stock Status attribute processing

Deprecated:
102.0.0 as this attribute should be removed
See also
StockItemInterface when you want to change the stock data
StockStatusInterface when you want to read the stock data for representation layer (storefront)
StockItemRepositoryInterface::save as extension point for customization of saving process

Definition at line 18 of file Stock.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\CatalogInventory\Api\StockRegistryInterface  $stockRegistry)

Construct

Parameters
\Magento\CatalogInventory\Api\StockRegistryInterface$stockRegistry

Definition at line 32 of file Stock.php.

34  {
35  $this->stockRegistry = $stockRegistry;
36  }

Member Function Documentation

◆ afterLoad()

afterLoad (   $object)

Set inventory data to custom attribute

Parameters
Product$object
Returns
$this

Implements BackendInterface.

Definition at line 44 of file Stock.php.

45  {
46  $stockItem = $this->stockRegistry->getStockItem($object->getId(), $object->getStore()->getWebsiteId());
47  $object->setData(
48  $this->getAttribute()->getAttributeCode(),
49  ['is_in_stock' => $stockItem->getIsInStock(), 'qty' => $stockItem->getQty()]
50  );
51  return parent::afterLoad($object);
52  }

◆ validate()

validate (   $object)

Validate

Parameters
Product$object
Exceptions

Definition at line 61 of file Stock.php.

62  {
63  $attrCode = $this->getAttribute()->getAttributeCode();
64  $value = $object->getData($attrCode);
65  if (!empty($value['qty']) && !preg_match('/^-?\d*(\.|,)?\d{0,4}$/i', $value['qty'])) {
66  throw new \Magento\Framework\Exception\LocalizedException(__('Please enter a valid number in this field.'));
67  }
68  return true;
69  }
__()
Definition: __.php:13
$value
Definition: gender.phtml:16

Field Documentation

◆ $stockRegistry

$stockRegistry
protected

Definition at line 25 of file Stock.php.


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