Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Stock.php
Go to the documentation of this file.
1 <?php
7 
12 use Magento\CatalogInventory\Model\ResourceModel\Stock\StatusFactory;
16 
26 class Stock
27 {
33  protected $storeManager;
34 
40  protected $scopeConfig;
41 
46 
51 
55  private $stockRegistryProvider;
56 
60  private $stockConfiguration;
61 
68  public function __construct(
71  StatusFactory $stockStatusFactory,
72  StockRegistryProviderInterface $stockRegistryProvider
73  ) {
74  $this->storeManager = $storeManager;
75  $this->scopeConfig = $scopeConfig;
76  $this->stockStatusFactory = $stockStatusFactory;
77  $this->stockRegistryProvider = $stockRegistryProvider;
78  }
79 
87  public function assignStatusToProduct(Product $product, $status = null)
88  {
89  if ($status === null) {
90  $scopeId = $this->getStockConfiguration()->getDefaultScopeId();
91  $stockStatus = $this->stockRegistryProvider->getStockStatus($product->getId(), $scopeId);
92  $status = $stockStatus->getStockStatus();
93  }
94  $product->setIsSalable($status);
95  }
96 
105  {
106  $scopeId = $this->getStockConfiguration()->getDefaultScopeId();
107  foreach ($productCollection as $product) {
108  $productId = $product->getId();
109  $stockStatus = $this->stockRegistryProvider->getStockStatus($productId, $scopeId);
110  $status = $stockStatus->getStockStatus();
111  $product->setIsSalable($status);
112  }
113  }
114 
122  {
123  $manageStock = $this->scopeConfig->getValue(
124  \Magento\CatalogInventory\Model\Configuration::XML_PATH_MANAGE_STOCK,
126  );
127  $cond = [
128  '{{table}}.use_config_manage_stock = 0 AND {{table}}.manage_stock=1 AND {{table}}.is_in_stock=1',
129  '{{table}}.use_config_manage_stock = 0 AND {{table}}.manage_stock=0'
130  ];
131 
132  if ($manageStock) {
133  $cond[] = '{{table}}.use_config_manage_stock = 1 AND {{table}}.is_in_stock=1';
134  } else {
135  $cond[] = '{{table}}.use_config_manage_stock = 1';
136  }
137 
138  $collection->joinField(
139  'inventory_in_stock',
140  'cataloginventory_stock_item',
141  'is_in_stock',
142  'product_id=entity_id',
143  '(' . join(') OR (', $cond) . ')'
144  );
145  }
146 
154  {
155  $stockFlag = 'has_stock_status_filter';
156  if (!$collection->hasFlag($stockFlag)) {
157  $isShowOutOfStock = $this->scopeConfig->getValue(
158  \Magento\CatalogInventory\Model\Configuration::XML_PATH_SHOW_OUT_OF_STOCK,
160  );
162  $resource->addStockDataToCollection(
163  $collection,
164  !$isShowOutOfStock
165  );
166  $collection->setFlag($stockFlag, true);
167  }
168  }
169 
173  protected function getStockStatusResource()
174  {
175  if (empty($this->stockStatusResource)) {
176  $this->stockStatusResource = $this->stockStatusFactory->create();
177  }
179  }
180 
186  private function getStockConfiguration()
187  {
188  if ($this->stockConfiguration === null) {
189  $this->stockConfiguration = \Magento\Framework\App\ObjectManager::getInstance()
190  ->get(\Magento\CatalogInventory\Api\StockConfigurationInterface::class);
191  }
192  return $this->stockConfiguration;
193  }
194 }
__construct(StoreManagerInterface $storeManager, ScopeConfigInterface $scopeConfig, StatusFactory $stockStatusFactory, StockRegistryProviderInterface $stockRegistryProvider)
Definition: Stock.php:68
$resource
Definition: bulk.php:12
assignStatusToProduct(Product $product, $status=null)
Definition: Stock.php:87
$status
Definition: order_status.php:8
addInStockFilterToCollection($collection)
Definition: Stock.php:121
addIsInStockFilterToCollection($collection)
Definition: Stock.php:153
addStockStatusToProducts(AbstractCollection $productCollection)
Definition: Stock.php:104