Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractStockqty.php
Go to the documentation of this file.
1 <?php
8 
10 
15 {
21  const XML_PATH_STOCK_THRESHOLD_QTY = 'cataloginventory/options/stock_threshold_qty';
22 
28  protected $_coreRegistry;
29 
33  protected $stockState;
34 
38  protected $stockRegistry;
39 
47  public function __construct(
48  \Magento\Framework\View\Element\Template\Context $context,
49  \Magento\Framework\Registry $registry,
50  \Magento\CatalogInventory\Api\StockStateInterface $stockState,
51  \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry,
52  array $data = []
53  ) {
54  $this->_coreRegistry = $registry;
55  $this->stockState = $stockState;
56  $this->stockRegistry = $stockRegistry;
57 
58  parent::__construct($context, $data);
59  }
60 
66  public function getProduct()
67  {
68  return $this->_coreRegistry->registry('current_product');
69  }
70 
76  public function getStockQty()
77  {
78  if (!$this->hasData('product_stock_qty')) {
79  $qty = 0;
80  $productId = $this->getProduct()->getId();
81  if ($productId) {
82  $qty = $this->getProductStockQty($this->getProduct());
83  }
84  $this->setData('product_stock_qty', $qty);
85  }
86  return $this->getData('product_stock_qty');
87  }
88 
95  public function getProductStockQty($product)
96  {
97  return $this->stockRegistry->getStockStatus($product->getId(), $product->getStore()->getWebsiteId())->getQty();
98  }
99 
105  public function getThresholdQty()
106  {
107  if (!$this->hasData('threshold_qty')) {
108  $qty = (float)$this->_scopeConfig->getValue(
109  self::XML_PATH_STOCK_THRESHOLD_QTY,
111  );
112  $this->setData('threshold_qty', $qty);
113  }
114  return $this->getData('threshold_qty');
115  }
116 
122  public function getPlaceholderId()
123  {
124  return 'stock-qty-' . $this->getProduct()->getId();
125  }
126 
132  public function isMsgVisible()
133  {
134  return $this->getStockQty() > 0 && $this->getStockQtyLeft() <= $this->getThresholdQty();
135  }
136 
142  public function getStockQtyLeft()
143  {
144  $stockItem = $this->stockRegistry->getStockItem($this->getProduct()->getId());
145  $minStockQty = $stockItem->getMinQty();
146  return $this->getStockQty() - $minStockQty;
147  }
148 }
getData($key='', $index=null)
Definition: DataObject.php:119
__construct(\Magento\Framework\View\Element\Template\Context $context, \Magento\Framework\Registry $registry, \Magento\CatalogInventory\Api\StockStateInterface $stockState, \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry, array $data=[])
setData($key, $value=null)
Definition: DataObject.php:72