AbstractStockqty.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CatalogInventory\Block\Stockqty;
  7. use Magento\Catalog\Model\Product;
  8. /**
  9. * Product stock qty abstract block
  10. */
  11. abstract class AbstractStockqty extends \Magento\Framework\View\Element\Template
  12. {
  13. /**
  14. * Threshold qty config path
  15. * @deprecated
  16. * @see \Magento\CatalogInventory\Model\Configuration::XML_PATH_STOCK_THRESHOLD_QTY
  17. */
  18. const XML_PATH_STOCK_THRESHOLD_QTY = 'cataloginventory/options/stock_threshold_qty';
  19. /**
  20. * Core registry
  21. *
  22. * @var \Magento\Framework\Registry
  23. */
  24. protected $_coreRegistry;
  25. /**
  26. * @var \Magento\CatalogInventory\Api\StockStateInterface
  27. */
  28. protected $stockState;
  29. /**
  30. * @var \Magento\CatalogInventory\Api\StockRegistryInterface
  31. */
  32. protected $stockRegistry;
  33. /**
  34. * @param \Magento\Framework\View\Element\Template\Context $context
  35. * @param \Magento\Framework\Registry $registry
  36. * @param \Magento\CatalogInventory\Api\StockStateInterface $stockState
  37. * @param \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
  38. * @param array $data
  39. */
  40. public function __construct(
  41. \Magento\Framework\View\Element\Template\Context $context,
  42. \Magento\Framework\Registry $registry,
  43. \Magento\CatalogInventory\Api\StockStateInterface $stockState,
  44. \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry,
  45. array $data = []
  46. ) {
  47. $this->_coreRegistry = $registry;
  48. $this->stockState = $stockState;
  49. $this->stockRegistry = $stockRegistry;
  50. parent::__construct($context, $data);
  51. }
  52. /**
  53. * Retrieve current product object
  54. *
  55. * @return \Magento\Catalog\Model\Product
  56. */
  57. public function getProduct()
  58. {
  59. return $this->_coreRegistry->registry('current_product');
  60. }
  61. /**
  62. * Retrieve current product stock qty
  63. *
  64. * @return float
  65. */
  66. public function getStockQty()
  67. {
  68. if (!$this->hasData('product_stock_qty')) {
  69. $qty = 0;
  70. $productId = $this->getProduct()->getId();
  71. if ($productId) {
  72. $qty = $this->getProductStockQty($this->getProduct());
  73. }
  74. $this->setData('product_stock_qty', $qty);
  75. }
  76. return $this->getData('product_stock_qty');
  77. }
  78. /**
  79. * Retrieve product stock qty
  80. *
  81. * @param Product $product
  82. * @return float
  83. */
  84. public function getProductStockQty($product)
  85. {
  86. return $this->stockRegistry->getStockStatus($product->getId(), $product->getStore()->getWebsiteId())->getQty();
  87. }
  88. /**
  89. * Retrieve threshold of qty to display stock qty message
  90. *
  91. * @return string
  92. */
  93. public function getThresholdQty()
  94. {
  95. if (!$this->hasData('threshold_qty')) {
  96. $qty = (float)$this->_scopeConfig->getValue(
  97. self::XML_PATH_STOCK_THRESHOLD_QTY,
  98. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  99. );
  100. $this->setData('threshold_qty', $qty);
  101. }
  102. return $this->getData('threshold_qty');
  103. }
  104. /**
  105. * Retrieve id of message placeholder in template
  106. *
  107. * @return string
  108. */
  109. public function getPlaceholderId()
  110. {
  111. return 'stock-qty-' . $this->getProduct()->getId();
  112. }
  113. /**
  114. * Retrieve visibility of stock qty message
  115. *
  116. * @return bool
  117. */
  118. public function isMsgVisible()
  119. {
  120. return $this->getStockQty() > 0
  121. && $this->getStockQtyLeft() > 0
  122. && $this->getStockQtyLeft() <= $this->getThresholdQty();
  123. }
  124. /**
  125. * Retrieve current product qty left in stock
  126. *
  127. * @return float
  128. */
  129. public function getStockQtyLeft()
  130. {
  131. $stockItem = $this->stockRegistry->getStockItem($this->getProduct()->getId());
  132. $minStockQty = $stockItem->getMinQty();
  133. return $this->getStockQty() - $minStockQty;
  134. }
  135. }