Stock.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CatalogInventory\Helper;
  7. use Magento\Catalog\Model\Product;
  8. use Magento\Catalog\Model\ResourceModel\Collection\AbstractCollection;
  9. use Magento\CatalogInventory\Api\StockConfigurationInterface;
  10. use Magento\CatalogInventory\Model\ResourceModel\Stock\Status;
  11. use Magento\CatalogInventory\Model\ResourceModel\Stock\StatusFactory;
  12. use Magento\CatalogInventory\Model\Spi\StockRegistryProviderInterface;
  13. use Magento\Framework\App\Config\ScopeConfigInterface;
  14. use Magento\Store\Model\StoreManagerInterface;
  15. /**
  16. * Class Stock
  17. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  18. * @api
  19. *
  20. * @deprecated 100.3.0 Replaced with Multi Source Inventory
  21. * @link https://devdocs.magento.com/guides/v2.3/inventory/index.html
  22. * @link https://devdocs.magento.com/guides/v2.3/inventory/catalog-inventory-replacements.html
  23. * @since 100.0.2
  24. */
  25. class Stock
  26. {
  27. /**
  28. * Store model manager
  29. *
  30. * @var StoreManagerInterface
  31. */
  32. protected $storeManager;
  33. /**
  34. * Core store config
  35. *
  36. * @var ScopeConfigInterface
  37. */
  38. protected $scopeConfig;
  39. /**
  40. * @var Status
  41. */
  42. protected $stockStatusResource;
  43. /**
  44. * @var StatusFactory
  45. */
  46. protected $stockStatusFactory;
  47. /**
  48. * @var StockRegistryProviderInterface
  49. */
  50. private $stockRegistryProvider;
  51. /**
  52. * @var StockConfigurationInterface
  53. */
  54. private $stockConfiguration;
  55. /**
  56. * @param StoreManagerInterface $storeManager
  57. * @param ScopeConfigInterface $scopeConfig
  58. * @param StatusFactory $stockStatusFactory
  59. * @param StockRegistryProviderInterface $stockRegistryProvider
  60. */
  61. public function __construct(
  62. StoreManagerInterface $storeManager,
  63. ScopeConfigInterface $scopeConfig,
  64. StatusFactory $stockStatusFactory,
  65. StockRegistryProviderInterface $stockRegistryProvider
  66. ) {
  67. $this->storeManager = $storeManager;
  68. $this->scopeConfig = $scopeConfig;
  69. $this->stockStatusFactory = $stockStatusFactory;
  70. $this->stockRegistryProvider = $stockRegistryProvider;
  71. }
  72. /**
  73. * Assign stock status information to product
  74. *
  75. * @param Product $product
  76. * @param int $status
  77. * @return void
  78. */
  79. public function assignStatusToProduct(Product $product, $status = null)
  80. {
  81. if ($status === null) {
  82. $scopeId = $this->getStockConfiguration()->getDefaultScopeId();
  83. $stockStatus = $this->stockRegistryProvider->getStockStatus($product->getId(), $scopeId);
  84. $status = $stockStatus->getStockStatus();
  85. }
  86. $product->setIsSalable($status);
  87. }
  88. /**
  89. * Add stock status information to products
  90. *
  91. * @param AbstractCollection $productCollection
  92. * @deprecated 100.1.0 Use Stock::addIsInStockFilterToCollection instead
  93. * @return void
  94. */
  95. public function addStockStatusToProducts(AbstractCollection $productCollection)
  96. {
  97. $scopeId = $this->getStockConfiguration()->getDefaultScopeId();
  98. foreach ($productCollection as $product) {
  99. $productId = $product->getId();
  100. $stockStatus = $this->stockRegistryProvider->getStockStatus($productId, $scopeId);
  101. $status = $stockStatus->getStockStatus();
  102. $product->setIsSalable($status);
  103. }
  104. }
  105. /**
  106. * Adds filtering for collection to return only in stock products
  107. *
  108. * @param \Magento\Catalog\Model\ResourceModel\Product\Link\Product\Collection $collection
  109. * @return void
  110. */
  111. public function addInStockFilterToCollection($collection)
  112. {
  113. $manageStock = $this->scopeConfig->getValue(
  114. \Magento\CatalogInventory\Model\Configuration::XML_PATH_MANAGE_STOCK,
  115. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  116. );
  117. $cond = [
  118. '{{table}}.use_config_manage_stock = 0 AND {{table}}.manage_stock=1 AND {{table}}.is_in_stock=1',
  119. '{{table}}.use_config_manage_stock = 0 AND {{table}}.manage_stock=0'
  120. ];
  121. if ($manageStock) {
  122. $cond[] = '{{table}}.use_config_manage_stock = 1 AND {{table}}.is_in_stock=1';
  123. } else {
  124. $cond[] = '{{table}}.use_config_manage_stock = 1';
  125. }
  126. $collection->joinField(
  127. 'inventory_in_stock',
  128. 'cataloginventory_stock_item',
  129. 'is_in_stock',
  130. 'product_id=entity_id',
  131. '(' . join(') OR (', $cond) . ')'
  132. );
  133. }
  134. /**
  135. * Add only is in stock products filter to product collection
  136. *
  137. * @param \Magento\Catalog\Model\ResourceModel\Product\Collection $collection
  138. * @return void
  139. */
  140. public function addIsInStockFilterToCollection($collection)
  141. {
  142. $stockFlag = 'has_stock_status_filter';
  143. if (!$collection->hasFlag($stockFlag)) {
  144. $isShowOutOfStock = $this->scopeConfig->getValue(
  145. \Magento\CatalogInventory\Model\Configuration::XML_PATH_SHOW_OUT_OF_STOCK,
  146. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  147. );
  148. $resource = $this->getStockStatusResource();
  149. $resource->addStockDataToCollection(
  150. $collection,
  151. !$isShowOutOfStock
  152. );
  153. $collection->setFlag($stockFlag, true);
  154. }
  155. }
  156. /**
  157. * @return Status
  158. */
  159. protected function getStockStatusResource()
  160. {
  161. if (empty($this->stockStatusResource)) {
  162. $this->stockStatusResource = $this->stockStatusFactory->create();
  163. }
  164. return $this->stockStatusResource;
  165. }
  166. /**
  167. * @return StockConfigurationInterface
  168. *
  169. * @deprecated 100.1.0
  170. */
  171. private function getStockConfiguration()
  172. {
  173. if ($this->stockConfiguration === null) {
  174. $this->stockConfiguration = \Magento\Framework\App\ObjectManager::getInstance()
  175. ->get(\Magento\CatalogInventory\Api\StockConfigurationInterface::class);
  176. }
  177. return $this->stockConfiguration;
  178. }
  179. }