Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Stock Status and Saleable Quantity Not Updated When Product Quantity is Set to 0 (#3364) #3414

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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 @@ -47,34 +47,53 @@ public function __construct(
*/
public function execute(array $sourceItems): void
{
$skus = array_map(fn ($sourceItem) => $sourceItem->getSku(), $sourceItems);
$skus = array_map(fn($sourceItem) => $sourceItem->getSku(), $sourceItems);
$legacyStockItemsByProductId = [];

// Fetch legacy stock items for the SKUs
foreach ($this->getLegacyStockItems->execute($skus) as $legacyStockItem) {
$legacyStockItemsByProductId[$legacyStockItem->getProductId()] = $legacyStockItem;
}

foreach ($sourceItems as $sourceItem) {
$sku = $sourceItem->getSku();
try {
$productId = (int)$this->getProductIdsBySkus->execute([$sku])[$sku];
} catch (NoSuchEntityException $e) {
// Skip synchronization of for not existed product
// Skip synchronization for non-existent product
continue;
}

$legacyStockItem = $legacyStockItemsByProductId[$productId] ?? null;
if (null === $legacyStockItem) {
continue;
}

// Update quantity and stock status based on source item
$legacyStockItem->setQty((float)$sourceItem->getQuantity());
$legacyStockItem->setIsInStock((int)$sourceItem->getStatus());

// Custom logic: If quantity is 0, set the stock status to 'in stock' or 'out of stock' as per requirements
if ($legacyStockItem->getQty() == 0) {
// Set the product as 'out of stock' or 'in stock' based on your logic
$legacyStockItem->setIsInStock(0); // 'out of stock'
} else {
$legacyStockItem->setIsInStock((int)$sourceItem->getStatus());
}

// Check if stock management is enabled and update stock status accordingly
if ($legacyStockItem->getManageStock() && !$this->stockStateProvider->verifyStock($legacyStockItem)) {
$legacyStockItem->setIsInStock(0);
}

// Execute the update to legacy stock item
$this->setDataToLegacyStockItem->execute(
(string)$sourceItem->getSku(),
(float)$legacyStockItem->getQty(),
(int) $legacyStockItem->getIsInStock()
(int)$legacyStockItem->getIsInStock()
);
}

// Update default stock and salability if needed
$affectedSkus = $this->updateDefaultStock->execute($skus);
if ($affectedSkus) {
$this->productSalabilityChangeProcessor->execute($affectedSkus);
Expand Down