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