![]() 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/PdfCustomiser/ViewModel/ |
<?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]> */ namespace Cnc\PdfCustomiser\ViewModel; use Cnc\Catalog\Model\Config; use Cnc\Catalog\Model\Manufacturer; use Cnc\Catalog\Model\ResourceModel\Manufacturer\CollectionFactory; use Fooman\PdfCore\Block\Pdf\Column; use Magento\Catalog\Api\ProductRepositoryInterface; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\View\Element\Block\ArgumentInterface; use Magento\Sales\Model\Order\Item; use Magento\Store\Model\ScopeInterface; class ColumnConverter implements ArgumentInterface { private $collection; /** * @var ScopeConfigInterface */ private $scopeConfig; /** * @var ProductRepositoryInterface */ private $productRepository; /** * ColumnConverter constructor. * @param CollectionFactory $collection * @param ScopeConfigInterface $scopeConfig * @param ProductRepositoryInterface $productRepository */ public function __construct( CollectionFactory $collection, ScopeConfigInterface $scopeConfig, ProductRepositoryInterface $productRepository ) { $this->collection = $collection->create(); $this->scopeConfig = $scopeConfig; $this->productRepository = $productRepository; } /** * @param Column $column * @param Item $item * @return string */ public function convert($column, $item) { if ($column->getIndex() === Config::PRODUCT_ATTRIBUTE_CODE_MANUFACTURER) { $manufacturerId = $this->getManufacturerId($item); if ($manufacturerId) { /** @var Manufacturer $manufacturer */ foreach ($this->collection as $manufacturer) { if ($manufacturer->getId() == $manufacturerId) { return $manufacturer->getCountry(); } } } return ''; } elseif ($column->getIndex() === 'weight' && $item->getData('weight')) { $weight = $item->getData('weight'); return ($weight >= 1) ? (($weight* 100) / 100.0) . ' ' . $this->getWeightUnit() : $weight . ' ' . $this->getWeightUnit(); } elseif (in_array($column->getIndex(), ['cnc_guarantee_type', 'cnc_guarantee_duration'])) { return __($column->getRowField($item)); } return $column->getRowField($item); } private function getManufacturerId($item) { $productId = $item->getProductId(); try { $product = $this->productRepository->getById($productId); return $product->getData(Config::PRODUCT_ATTRIBUTE_CODE_MANUFACTURER); } catch (NoSuchEntityException $e) { return null; } } /** * @return mixed */ private function getWeightUnit() { return $this->scopeConfig->getValue( 'general/locale/weight_unit', ScopeInterface::SCOPE_STORE ); } }