Skip to content
Closed
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

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -78,26 +78,17 @@ public function aroundExecute(
int $stockId
): bool {
try {
$types = $this->getProductTypesBySkus->execute([$sku]);

$isProductSalable = $proceed($sku, $stockId);
if (!isset($types[$sku]) || $types[$sku] !== Type::TYPE_CODE || !$isProductSalable) {
if (!$isProductSalable) {
return $isProductSalable;
}

// TODO: remove in https://github.com/magento/inventory/issues/3201
// Product salability MUST NOT BE CALLED during product load.
// Tests stabilization.
/** @var \Magento\Framework\Registry $registry */
$registry = ObjectManager::getInstance()->get(\Magento\Framework\Registry::class);
$key = 'inventory_check_product' . $sku;

if ($registry->registry($key)) {
$product = $registry->registry($key);
} else {
$product = $this->productRepository->get($sku);
$types = $this->getProductTypesBySkus->execute([$sku]);
if (!isset($types[$sku]) || $types[$sku] !== Type::TYPE_CODE) {
return $isProductSalable;
}

$product = $this->productRepository->get($sku);
/** @noinspection PhpParamsInspection */
$options = $this->bundleProductType->getOptionsCollection($product);
$status = $this->getBundleProductStockStatus->execute(
Expand Down
3 changes: 0 additions & 3 deletions InventoryBundleProduct/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@
</argument>
</arguments>
</type>
<type name="Magento\CatalogInventory\Helper\Stock">
<plugin name="adapt_assign_stock_status_to_bundle_product" type="Magento\InventoryBundleProduct\Plugin\CatalogInventory\Helper\Stock\AdaptAssignStatusToProductPlugin"/>
</type>
<type name="Magento\InventorySales\Model\IsProductSalableCondition\IsProductSalableConditionChain">
<plugin name="is_bundle_product_salable" type="Magento\InventoryBundleProduct\Plugin\InventorySales\IsBundleProductSalable" />
</type>
Expand Down
73 changes: 73 additions & 0 deletions InventoryCatalog/Model/IsProductSalable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\InventoryCatalog\Model;

use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\InventorySalesApi\Api\AreProductsSalableInterface;

/**
* Get salable product status service.
*/
class IsProductSalable
{
/**
* @var GetStockIdForCurrentWebsite
*/
private $getStockIdForCurrentWebsite;

/**
* @var AreProductsSalableInterface
*/
private $areProductsSalable;

/**
* @var array
*/
private $productStatusCache;

/**
* @param GetStockIdForCurrentWebsite $getStockIdForCurrentWebsite
* @param AreProductsSalableInterface $areProductsSalable
*/
public function __construct(
GetStockIdForCurrentWebsite $getStockIdForCurrentWebsite,
AreProductsSalableInterface $areProductsSalable
) {
$this->getStockIdForCurrentWebsite = $getStockIdForCurrentWebsite;
$this->areProductsSalable = $areProductsSalable;
}

/**
* Verify product salable status.
*
* @param Product $product
* @return bool
*/
public function execute(Product $product): bool
{
if (null === $product->getSku() || (int)$product->getStatus() === Status::STATUS_DISABLED) {
return false;
}
if ($product->getData('is_salable') !== null) {
return (bool)$product->getData('is_salable');
}
$stockId = $this->getStockIdForCurrentWebsite->execute();
if (isset($this->productStatusCache[$stockId][$product->getSku()])) {
return $this->productStatusCache[$stockId][$product->getSku()];
}

$stockId = $this->getStockIdForCurrentWebsite->execute();
$result = current($this->areProductsSalable->execute([$product->getSku()], $stockId));
$salabilityStatus = $result->isSalable();
$this->productStatusCache[$stockId][$product->getSku()] = $salabilityStatus;

return $salabilityStatus;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryCatalog\Plugin\Catalog\Model\Type\Simple;

use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Type\Simple;
use Magento\InventoryCatalog\Model\IsProductSalable;

/**
* Apply the inventory is-salable result to the according method of the product type model.
*/
class IsSalablePlugin
{
/**
* @var IsProductSalable
*/
private $isProductSalable;

/**
* @param IsProductSalable $isProductSalable
*/
public function __construct(
IsProductSalable $isProductSalable
) {
$this->isProductSalable = $isProductSalable;
}

/**
* Fetches is salable status from multi-stock.
*
* @param Simple $subject
* @param \Closure $proceed
* @param Product $product
* @return bool
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function aroundIsSalable(Simple $subject, \Closure $proceed, Product $product): bool
{
return $this->isProductSalable->execute($product);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryCatalog\Plugin\Catalog\Model\Type\Virtual;

use Magento\Catalog\Model\Product;
use Magento\InventoryCatalog\Model\IsProductSalable;

/**
* Apply the inventory is-salable result to the according method of the product type model.
*/
class IsSalablePlugin
{
/**
* @var IsProductSalable
*/
private $isProductSalable;

/**
* @param IsProductSalable $isProductSalable
*/
public function __construct(
IsProductSalable $isProductSalable
) {
$this->isProductSalable = $isProductSalable;
}

/**
* Fetches is salable status from multi-stock.
*
* @param Product\Type\Virtual $subject
* @param \Closure $proceed
* @param Product $product
* @return bool
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function aroundIsSalable(Product\Type\Virtual $subject, \Closure $proceed, Product $product): bool
{
return $this->isProductSalable->execute($product);
}
}
Loading