![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/app/code/Cnc/Catalog/Model/ |
<?php /** * Copyright (c) 2021 Kaliop Digital Commerce (https://digitalcommerce.kaliop.com) All Rights Reserved. * https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) * Krzysztof Majkowski <[email protected]> <[email protected]> */ declare(strict_types=1); namespace Cnc\Catalog\Model; use Cnc\Catalog\Api\IsSalableInterface; use Cnc\Catalog\Helper\Data as CncCatalogHelper; use Cnc\Catalog\Model\Attribute\MsiAttributes; use Cnc\Checkout\Api\GetAssignedSourceCodesBySkuInterface; use Cnc\Checkout\Api\GetAssignedSourceCodesInterface; use Magento\Catalog\Api\ProductRepositoryInterface; use Magento\Catalog\Model\ResourceModel\Product as ResourceProduct; use Magento\GroupedProduct\Model\Product\Type\Grouped; use Magento\GroupedProduct\Model\ResourceModel\Product\Link; use Magento\Store\Model\StoreManagerInterface; class IsSalable implements IsSalableInterface { /** * @var CachedGetProductSalableQty */ private $cachedGetProductSalableQty; /** * @var MsiAttributes */ private $msiAttributes; /** * @var ProductRepositoryInterface */ private $productRepository; /** * @var ResourceProduct */ private $resourceProduct; /** * @var CncCatalogHelper */ private $helper; /** * @var GetAssignedSourceCodesInterface */ private $getAssignedSourceCodes; /** * @var StoreManagerInterface */ private $storeManager; /** * @var GetAssignedSourceCodesBySkuInterface */ private $getAssignedSourceCodesBySku; /** * @var array */ private $cacheInstance = []; /** * GetProductQty constructor. * @param CachedGetProductSalableQty $cachedGetProductSalableQty * @param MsiAttributes $msiAttributes * @param ProductRepositoryInterface $productRepository * @param ResourceProduct $resourceProduct * @param CncCatalogHelper $helper * @param GetAssignedSourceCodesInterface $getAssignedSourceCodes * @param StoreManagerInterface $storeManager * @param GetAssignedSourceCodesBySkuInterface $getAssignedSourceCodesBySku */ public function __construct( CachedGetProductSalableQty $cachedGetProductSalableQty, MsiAttributes $msiAttributes, ProductRepositoryInterface $productRepository, ResourceProduct $resourceProduct, CncCatalogHelper $helper, GetAssignedSourceCodesInterface $getAssignedSourceCodes, StoreManagerInterface $storeManager, GetAssignedSourceCodesBySkuInterface $getAssignedSourceCodesBySku ) { $this->cachedGetProductSalableQty = $cachedGetProductSalableQty; $this->msiAttributes = $msiAttributes; $this->productRepository = $productRepository; $this->resourceProduct = $resourceProduct; $this->helper = $helper; $this->getAssignedSourceCodes = $getAssignedSourceCodes; $this->storeManager = $storeManager; $this->getAssignedSourceCodesBySku = $getAssignedSourceCodesBySku; } /** * @param string $sku * @return bool * @throws \Magento\Framework\Exception\InputException * @throws \Magento\Framework\Exception\LocalizedException */ public function execute(string $sku) { if (isset($this->cacheInstance[$sku])) { return $this->cacheInstance[$sku]; } $product = $this->productRepository->getById($this->resourceProduct->getIdBySku($sku)); if ($product->getTypeId() == 'grouped') { /** @var Grouped $type */ $type = $product->getTypeInstance(); $childrenIds = $type->getChildrenIds($product->getId()); if (is_array($childrenIds) && count($childrenIds)) { foreach ($childrenIds[Link::LINK_TYPE_GROUPED] as $childId) { $childSku = $this->helper->getSkuById($childId); $isChildSalable = $this->isSalable($childSku); if ($isChildSalable) { $this->cacheInstance[$sku] = true; return true; } } } } else { $isSalable = $this->isSalable($sku); $this->cacheInstance[$sku] = $isSalable; return $isSalable; } return false; } protected function isSalable(string $sku) { if (isset($this->cacheInstance[$sku])) { return $this->cacheInstance[$sku]; } $vhsAttributeId = $this->msiAttributes->getAttributeId('cnc_vhs'); $product = $this->productRepository->getById($this->resourceProduct->getIdBySku($sku)); if (!$product->isSalable()) { $this->cacheInstance[$sku] = false; return false; } $assignedSources = $this->getAssignedSourceCodesBySku->execute($sku); $sourceCodes = $this->getSourceCodes(); foreach ($sourceCodes as $sourceCode) { if ($this->helper->getCncAvailabilityValue($sku, $sourceCode) == 'Out of stock' || !in_array($sourceCode, $assignedSources)) { continue; } $qty = $this->cachedGetProductSalableQty->execute($sku, $sourceCode); if ($qty <= 0) { $vhsValue = $this->msiAttributes->getAttributeValue( $vhsAttributeId, $sku, $sourceCode )->getValue(); if ($vhsValue) { $this->cacheInstance[$sku] = true; return true; } } else { $this->cacheInstance[$sku] = true; return true; } } $this->cacheInstance[$sku] = false; return false; } private function getSourceCodes() { if (isset($this->cacheInstance['source_codes'])) { return $this->cacheInstance['source_codes']; } $code = $this->storeManager->getWebsite()->getCode(); $sourceCodes = $this->getAssignedSourceCodes->execute($code); $this->cacheInstance['source_codes'] = $sourceCodes; return $sourceCodes; } }