Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,18 @@
use Magento\Catalog\Model\Product;
use Magento\CatalogInventory\Helper\Stock;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\InventoryCatalog\Model\GetStockIdForCurrentWebsite;
use Magento\InventoryCatalogApi\Api\DefaultStockProviderInterface;
use Magento\InventoryCatalogApi\Model\GetProductIdsBySkusInterface;
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
use Magento\InventorySalesApi\Api\IsProductSalableInterface;
use Magento\InventorySalesApi\Api\StockResolverInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
* Adapt assignStatusToProduct for multi stocks.
*/
class AdaptAssignStatusToProductPlugin
{
/**
* @var GetStockIdForCurrentWebsite
*/
private $getStockIdForCurrentWebsite;

/**
* @var IsProductSalableInterface
*/
Expand All @@ -41,21 +38,34 @@ class AdaptAssignStatusToProductPlugin
private $getProductIdsBySkus;

/**
* @param GetStockIdForCurrentWebsite $getStockIdForCurrentWebsite
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @var StockResolverInterface
*/
private $stockResolver;

/**
* @param IsProductSalableInterface $isProductSalable
* @param DefaultStockProviderInterface $defaultStockProvider
* @param GetProductIdsBySkusInterface $getProductIdsBySkus
* @param StoreManagerInterface $storeManager
* @param StockResolverInterface $stockResolver
*/
public function __construct(
GetStockIdForCurrentWebsite $getStockIdForCurrentWebsite,
IsProductSalableInterface $isProductSalable,
DefaultStockProviderInterface $defaultStockProvider,
GetProductIdsBySkusInterface $getProductIdsBySkus
GetProductIdsBySkusInterface $getProductIdsBySkus,
StoreManagerInterface $storeManager,
StockResolverInterface $stockResolver
) {
$this->getStockIdForCurrentWebsite = $getStockIdForCurrentWebsite;
$this->isProductSalable = $isProductSalable;
$this->defaultStockProvider = $defaultStockProvider;
$this->getProductIdsBySkus = $getProductIdsBySkus;
$this->storeManager = $storeManager;
$this->stockResolver = $stockResolver;
}

/**
Expand All @@ -81,8 +91,9 @@ public function aroundAssignStatusToProduct(
$this->getProductIdsBySkus->execute([$product->getSku()]);

if (null === $status) {
$stockId = $this->getStockIdForCurrentWebsite->execute();
$status = (int)$this->isProductSalable->execute($product->getSku(), $stockId);
$website = $product->getStore()->getWebsite() ?: $this->storeManager->getWebsite();
$stock = $this->stockResolver->execute(SalesChannelInterface::TYPE_WEBSITE, $website->getCode());
$status = (int)$this->isProductSalable->execute($product->getSku(), $stock->getStockId());
}

$proceed($product, $status);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function beforeAssignStatusToProduct(
$status = null
): array {
if ($product->getTypeId() === Configurable::TYPE_CODE) {
$website = $this->storeManager->getWebsite();
$website = $product->getStore()->getWebsite() ?: $this->storeManager->getWebsite();
$stock = $this->stockResolver->execute(SalesChannelInterface::TYPE_WEBSITE, $website->getCode());
$options = $this->configurable->getConfigurableOptions($product);
$status = 0;
Expand Down