getSourceItemsBySku = $getSourceItemsBySku; $this->sourceRepository = $sourceRepository; $this->customerSession = $customerSession; $this->sourceCustomerRepository = $sourceCustomerRepository; $this->showProductOrNot = $showProductOrNot; $this->stock = $stock; $this->getProductSalableQty = $getProductSalableQty; $this->logger = $logger; } protected function getStockBySku(string $sku): float|int { try { $stock = $this->stock->load(self::MAIN_STOCK, 'name'); return $this->getProductSalableQty->execute($sku, $stock->getId()); } catch (InputException | LocalizedException $e) { } return 0; } public function getStockDetails(Product $product) { $sourceItems = $this->getSourceItemsBySku->execute($product->getSku()); $stockData = []; $stockStatus = 'Out of Stock'; $total = 0; if (!$product->isSalable()) { return [ 'show' => false, 'status' => $stockStatus, 'data' => [], 'total' => $total, ]; } $preferSourceCode = null; if ($this->customerSession->isLoggedIn()) { try { $preferSource = $this->sourceCustomerRepository->getFromCustomerId($this->customerSession->getCustomerId()); $preferSourceCode = $preferSource->getSourceCode(); } catch (LocalizedException $e) { } } // $stockQty = $this->getStockBySku($sku); foreach ($sourceItems as $sourceItem) { $sourceCode = $sourceItem->getSourceCode(); $source = $this->sourceRepository->get($sourceCode); if ($sourceCode == 'default') { continue; } $status = $sourceItem->getStatus() == SourceItemInterface::STATUS_IN_STOCK ? 'In Stock' : 'Out of Stock'; if ($status == 'In Stock' && $sourceCode != 'default') { $stockStatus = 'In Stock'; } $quantity = $sourceItem->getQuantity(); $total += $quantity; if ($sourceCode == $preferSourceCode && $quantity) { $stockData[$source->getName()] = [ 'status' => $status, 'quantity' => $quantity, ]; } } return [ 'show' => $this->showProductOrNot->checkByNetsuiteStatus(), 'status' => $stockStatus, 'data' => $stockData, 'total' => $total, ]; } }