123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\CatalogInventory\Helper;
- use Magento\Catalog\Model\Product;
- use Magento\Catalog\Model\ResourceModel\Collection\AbstractCollection;
- use Magento\CatalogInventory\Api\StockConfigurationInterface;
- use Magento\CatalogInventory\Model\ResourceModel\Stock\Status;
- use Magento\CatalogInventory\Model\ResourceModel\Stock\StatusFactory;
- use Magento\CatalogInventory\Model\Spi\StockRegistryProviderInterface;
- use Magento\Framework\App\Config\ScopeConfigInterface;
- use Magento\Store\Model\StoreManagerInterface;
- /**
- * Class Stock
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- * @api
- *
- * @deprecated 100.3.0 Replaced with Multi Source Inventory
- * @link https://devdocs.magento.com/guides/v2.3/inventory/index.html
- * @link https://devdocs.magento.com/guides/v2.3/inventory/catalog-inventory-replacements.html
- * @since 100.0.2
- */
- class Stock
- {
- /**
- * Store model manager
- *
- * @var StoreManagerInterface
- */
- protected $storeManager;
- /**
- * Core store config
- *
- * @var ScopeConfigInterface
- */
- protected $scopeConfig;
- /**
- * @var Status
- */
- protected $stockStatusResource;
- /**
- * @var StatusFactory
- */
- protected $stockStatusFactory;
- /**
- * @var StockRegistryProviderInterface
- */
- private $stockRegistryProvider;
- /**
- * @var StockConfigurationInterface
- */
- private $stockConfiguration;
- /**
- * @param StoreManagerInterface $storeManager
- * @param ScopeConfigInterface $scopeConfig
- * @param StatusFactory $stockStatusFactory
- * @param StockRegistryProviderInterface $stockRegistryProvider
- */
- public function __construct(
- StoreManagerInterface $storeManager,
- ScopeConfigInterface $scopeConfig,
- StatusFactory $stockStatusFactory,
- StockRegistryProviderInterface $stockRegistryProvider
- ) {
- $this->storeManager = $storeManager;
- $this->scopeConfig = $scopeConfig;
- $this->stockStatusFactory = $stockStatusFactory;
- $this->stockRegistryProvider = $stockRegistryProvider;
- }
- /**
- * Assign stock status information to product
- *
- * @param Product $product
- * @param int $status
- * @return void
- */
- public function assignStatusToProduct(Product $product, $status = null)
- {
- if ($status === null) {
- $scopeId = $this->getStockConfiguration()->getDefaultScopeId();
- $stockStatus = $this->stockRegistryProvider->getStockStatus($product->getId(), $scopeId);
- $status = $stockStatus->getStockStatus();
- }
- $product->setIsSalable($status);
- }
- /**
- * Add stock status information to products
- *
- * @param AbstractCollection $productCollection
- * @deprecated 100.1.0 Use Stock::addIsInStockFilterToCollection instead
- * @return void
- */
- public function addStockStatusToProducts(AbstractCollection $productCollection)
- {
- $scopeId = $this->getStockConfiguration()->getDefaultScopeId();
- foreach ($productCollection as $product) {
- $productId = $product->getId();
- $stockStatus = $this->stockRegistryProvider->getStockStatus($productId, $scopeId);
- $status = $stockStatus->getStockStatus();
- $product->setIsSalable($status);
- }
- }
- /**
- * Adds filtering for collection to return only in stock products
- *
- * @param \Magento\Catalog\Model\ResourceModel\Product\Link\Product\Collection $collection
- * @return void
- */
- public function addInStockFilterToCollection($collection)
- {
- $manageStock = $this->scopeConfig->getValue(
- \Magento\CatalogInventory\Model\Configuration::XML_PATH_MANAGE_STOCK,
- \Magento\Store\Model\ScopeInterface::SCOPE_STORE
- );
- $cond = [
- '{{table}}.use_config_manage_stock = 0 AND {{table}}.manage_stock=1 AND {{table}}.is_in_stock=1',
- '{{table}}.use_config_manage_stock = 0 AND {{table}}.manage_stock=0'
- ];
- if ($manageStock) {
- $cond[] = '{{table}}.use_config_manage_stock = 1 AND {{table}}.is_in_stock=1';
- } else {
- $cond[] = '{{table}}.use_config_manage_stock = 1';
- }
- $collection->joinField(
- 'inventory_in_stock',
- 'cataloginventory_stock_item',
- 'is_in_stock',
- 'product_id=entity_id',
- '(' . join(') OR (', $cond) . ')'
- );
- }
- /**
- * Add only is in stock products filter to product collection
- *
- * @param \Magento\Catalog\Model\ResourceModel\Product\Collection $collection
- * @return void
- */
- public function addIsInStockFilterToCollection($collection)
- {
- $stockFlag = 'has_stock_status_filter';
- if (!$collection->hasFlag($stockFlag)) {
- $isShowOutOfStock = $this->scopeConfig->getValue(
- \Magento\CatalogInventory\Model\Configuration::XML_PATH_SHOW_OUT_OF_STOCK,
- \Magento\Store\Model\ScopeInterface::SCOPE_STORE
- );
- $resource = $this->getStockStatusResource();
- $resource->addStockDataToCollection(
- $collection,
- !$isShowOutOfStock
- );
- $collection->setFlag($stockFlag, true);
- }
- }
- /**
- * @return Status
- */
- protected function getStockStatusResource()
- {
- if (empty($this->stockStatusResource)) {
- $this->stockStatusResource = $this->stockStatusFactory->create();
- }
- return $this->stockStatusResource;
- }
- /**
- * @return StockConfigurationInterface
- *
- * @deprecated 100.1.0
- */
- private function getStockConfiguration()
- {
- if ($this->stockConfiguration === null) {
- $this->stockConfiguration = \Magento\Framework\App\ObjectManager::getInstance()
- ->get(\Magento\CatalogInventory\Api\StockConfigurationInterface::class);
- }
- return $this->stockConfiguration;
- }
- }
|