_coreRegistry = $registry; $this->stockState = $stockState; $this->stockRegistry = $stockRegistry; parent::__construct($context, $data); } /** * Retrieve current product object * * @return \Magento\Catalog\Model\Product */ public function getProduct() { return $this->_coreRegistry->registry('current_product'); } /** * Retrieve current product stock qty * * @return float */ public function getStockQty() { if (!$this->hasData('product_stock_qty')) { $qty = 0; $productId = $this->getProduct()->getId(); if ($productId) { $qty = $this->getProductStockQty($this->getProduct()); } $this->setData('product_stock_qty', $qty); } return $this->getData('product_stock_qty'); } /** * Retrieve product stock qty * * @param Product $product * @return float */ public function getProductStockQty($product) { return $this->stockRegistry->getStockStatus($product->getId(), $product->getStore()->getWebsiteId())->getQty(); } /** * Retrieve threshold of qty to display stock qty message * * @return string */ public function getThresholdQty() { if (!$this->hasData('threshold_qty')) { $qty = (float)$this->_scopeConfig->getValue( self::XML_PATH_STOCK_THRESHOLD_QTY, \Magento\Store\Model\ScopeInterface::SCOPE_STORE ); $this->setData('threshold_qty', $qty); } return $this->getData('threshold_qty'); } /** * Retrieve id of message placeholder in template * * @return string */ public function getPlaceholderId() { return 'stock-qty-' . $this->getProduct()->getId(); } /** * Retrieve visibility of stock qty message * * @return bool */ public function isMsgVisible() { return $this->getStockQty() > 0 && $this->getStockQtyLeft() > 0 && $this->getStockQtyLeft() <= $this->getThresholdQty(); } /** * Retrieve current product qty left in stock * * @return float */ public function getStockQtyLeft() { $stockItem = $this->stockRegistry->getStockItem($this->getProduct()->getId()); $minStockQty = $stockItem->getMinQty(); return $this->getStockQty() - $minStockQty; } }