Files
2026-09-15 10:35:56 +08:00

48 lines
1.7 KiB
PHP

<?php
declare(strict_types=1);
namespace Esun\ProductInquiry\Model;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\CatalogInventory\Api\StockRegistryInterface;
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
use Magento\InventorySalesApi\Api\IsProductSalableInterface;
use Magento\InventorySalesApi\Api\StockResolverInterface;
use Magento\Store\Api\Data\WebsiteInterface;
class AvailabilityResolver
{
public function __construct(
private readonly StockRegistryInterface $stockRegistry,
private readonly StockResolverInterface $stockResolver,
private readonly IsProductSalableInterface $isProductSalable
) {
}
public function resolve(ProductInterface $product, WebsiteInterface $website): array
{
$stockItem = $this->stockRegistry->getStockItem((int)$product->getId(), (int)$website->getId());
$stock = $this->stockResolver->execute(SalesChannelInterface::TYPE_WEBSITE, (string)$website->getCode());
$legacyInStock = (bool)$stockItem->getIsInStock();
$msiSalable = $this->isProductSalable->execute((string)$product->getSku(), (int)$stock->getStockId());
if (!$legacyInStock && !$msiSalable) {
$reason = 'both';
} elseif (!$legacyInStock) {
$reason = 'manual_out_of_stock';
} elseif (!$msiSalable) {
$reason = 'msi_not_salable';
} else {
$reason = 'salable';
}
return [
'stock_status' => (int)$legacyInStock,
'is_salable' => (int)$msiSalable,
'stock_id' => (int)$stock->getStockId(),
'reason' => $reason,
'requires_inquiry' => !$legacyInStock || !$msiSalable,
];
}
}