![]() 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/SerialNumber/Ui/Component/Listing/Column/ |
<?php /** * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) * @author Radosław Łańcucki <[email protected]> * @copyright Copyright (c) 2022 Kaliop Digital Commerce (https://digitalcommerce.kaliop.com) */ declare(strict_types=1); namespace Cnc\SerialNumber\Ui\Component\Listing\Column; use Magento\Catalog\Api\ProductRepositoryInterface; use Magento\Catalog\Model\ResourceModel\Product; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\View\Element\UiComponent\ContextInterface; use Magento\Framework\View\Element\UiComponentFactory; use Magento\Ui\Component\Listing\Columns\Column; class StateOfWear extends Column { /** * @var ProductRepositoryInterface */ private $productRepository; /** * @var Product */ private $productResource; /** * @var array */ private $stateOfWearLabels = []; /** * ProductId constructor. * @param ContextInterface $context * @param UiComponentFactory $uiComponentFactory * @param ProductRepositoryInterface $productRepository * @param Product $productResource * @param array $components * @param array $data */ public function __construct( ContextInterface $context, UiComponentFactory $uiComponentFactory, ProductRepositoryInterface $productRepository, Product $productResource, array $components = [], array $data = [] ) { parent::__construct($context, $uiComponentFactory, $components, $data); $this->productRepository = $productRepository; $this->productResource = $productResource; } /** * @param array $dataSource * @return array */ public function prepareDataSource(array $dataSource): array { foreach ($dataSource['data']['items'] as $index => $item) { $dataSource['data']['items'][$index]['cnc_state_of_wear'] = $this->getStateOfWearValue($item['sku']); } return $dataSource; } /** * @param string $sku * @return string */ private function getStateOfWearValue(string $sku): string { try { $product = $this->productRepository->get($sku); $stateOfWearAttribute = $product->getCustomAttribute('cnc_state_of_wear'); $attributeValue = $stateOfWearAttribute ? (string)$stateOfWearAttribute->getValue() : ''; $value = $this->getStateOfWearLabel($attributeValue); } catch (\Exception $e) { $value = ''; } return $value; } /** * @param string $value * @return string * @throws \Magento\Framework\Exception\LocalizedException */ private function getStateOfWearLabel(string $value): string { if (!isset($this->stateOfWearLabels[$value])) { $this->stateOfWearLabels[$value] = $this->productResource->getAttribute('cnc_state_of_wear') ->getSource() ->getOptionText($value); } return $this->stateOfWearLabels[$value]; } }