![]() 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/Product/ |
<?php /** * Copyright (c) 2020 Kaliop Digital Commerce (https://digitalcommerce.kaliop.com) All Rights Reserved. * https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) * cnc_catalog_m2 * <[email protected]> */ declare(strict_types=1); namespace Cnc\Catalog\Model\Product; use Cnc\Catalog\Helper\Data as CncCatalogHelper; use Cnc\Catalog\Setup\Patch\Data\UpdateCustomTablesAttributesData; use Magento\Catalog\Api\Data\ProductInterface; use Magento\Framework\Exception\FileSystemException; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Filesystem\Driver\File; use Magento\Framework\Setup\SampleData\FixtureManager; use Magento\Framework\View\Element\Block\ArgumentInterface; use Magento\GroupedProduct\Model\Product\Type\Grouped; use Magento\GroupedProduct\Model\ResourceModel\Product\Link; use Magento\Store\Model\StoreManagerInterface; class States implements ArgumentInterface { public const STATES_ID_MAPPING = [ 1 => self::STATE_NEW, 2 => self::STATE_USED, 3 => self::STATE_REFURBISHED, 4 => self::STATE_EXCHANGE ]; public const STATE_NEW = 'new'; public const STATE_USED = 'used'; public const STATE_REFURBISHED = 'refurbished'; public const STATE_EXCHANGE = 'exchange'; public const ATTRIBUTE_CODE = 'cnc_state_of_wear'; /** * @var CncCatalogHelper */ protected $cncCatalogHelper; /** * @var StoreManagerInterface */ private $storeManager; /** * @var File */ private $file; /** * @var FixtureManager */ private $fixtureManager; /** * States constructor. * @param CncCatalogHelper $cncCatalogHelper * @param StoreManagerInterface $storeManager * @param File $file * @param FixtureManager $fixtureManager */ public function __construct( CncCatalogHelper $cncCatalogHelper, StoreManagerInterface $storeManager, File $file, FixtureManager $fixtureManager ) { $this->cncCatalogHelper = $cncCatalogHelper; $this->storeManager = $storeManager; $this->file = $file; $this->fixtureManager = $fixtureManager; } /** * @param ProductInterface $product * @param string $keyPrefix * @param bool $withTranslate * @return array * @throws LocalizedException */ public function getProductStates( ProductInterface $product, $keyPrefix = '', $withTranslate = true ): array { $result = []; $productStateId = $product->getCncStateOfWear(); $importFileData = $this->getDataFromFile( UpdateCustomTablesAttributesData::FIXTURE_STATE_OF_WEAR_DATA_FILE_PATH ); if ($productStateId) { $productStateLabel = $this->cncCatalogHelper->getAttributeText($product, self::ATTRIBUTE_CODE); // Get base values for cnc attributes to match ID mapping $baseProductStateId = $this->getBaseProductStateId($importFileData, $productStateLabel); if ($baseProductStateId) { $stateCode = self::STATES_ID_MAPPING[$baseProductStateId]; $result[$keyPrefix . $stateCode] = $withTranslate ? __($productStateLabel) : $productStateLabel; } } if ($product->getTypeId() == 'grouped') { /** @var Grouped $type */ $type = $product->getTypeInstance(); $children = $type->getChildrenIds($product->getId()); $storeId = $this->storeManager->getStore()->getId(); foreach ($children[Link::LINK_TYPE_GROUPED] as $childId) { /** @var \Magento\Catalog\Model\ResourceModel\Product $resource */ $resource = $product->getResource(); $productStateId = $resource->getAttributeRawValue($childId, self::ATTRIBUTE_CODE, $storeId); if ($productStateId) { $productStateLabel = $this->cncCatalogHelper->getAttributeTextByValue( $productStateId, self::ATTRIBUTE_CODE ); // Get base values for cnc attributes to match ID mapping $baseProductStateId = $this->getBaseProductStateId($importFileData, $productStateLabel); if ($baseProductStateId) { $stateCode = self::STATES_ID_MAPPING[$baseProductStateId]; $result[$keyPrefix . $stateCode] = $withTranslate ? __($productStateLabel) : $productStateLabel; } } } } return $result; } /** * @param $name * @return array * @throws LocalizedException * @throws FileSystemException */ public function getDataFromFile($name) { $data = []; $fileName = $this->fixtureManager->getFixture($name); $file = $this->file->fileOpen($fileName, "r"); $header = $this->file->fileGetCsv($file, 0, ","); while ($row = $this->file->fileGetCsv($file, 0, ",")) { $data[] = array_filter(array_combine($header, $row)); } return $data; } /** * @param $importFileData * @param $productStateLabel * @return string|false */ public function getBaseProductStateId($importFileData, $productStateLabel) { $result = false; foreach ($importFileData as $stateData) { if ($stateData['etats_name'] == $productStateLabel) { $result = $stateData['etats_id']; } } return $result; } }