![]() 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/Helper/ |
<?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 * Radosław Stępień <[email protected]> <[email protected]> */ namespace Cnc\Catalog\Helper; use Cnc\Catalog\Model\Attribute\MsiAttributes; use Magento\Catalog\Api\Data\ProductInterface; use Magento\Catalog\Model\ResourceModel\Product; use Magento\Framework\App\Helper\AbstractHelper; use Magento\Framework\App\Helper\Context; use Magento\Framework\App\ResourceConnection; class Data extends AbstractHelper { /** * @var array */ private $productAttributeTextCache = []; /** * @var ResourceConnection */ private $resourceConnection; /** * @var Product */ private $productResource; /** * @var MsiAttributes */ private $msiAttributes; /** * Data constructor. * @param Context $context * @param ResourceConnection $resourceConnection * @param Product $productResource * @param MsiAttributes $msiAttributes */ public function __construct( Context $context, ResourceConnection $resourceConnection, Product $productResource, MsiAttributes $msiAttributes ) { parent::__construct($context); $this->resourceConnection = $resourceConnection; $this->productResource = $productResource; $this->msiAttributes = $msiAttributes; } /** * Wrapper method for product entity getAttributeText method * In order to add cache mechanism, and not load multiple times the same text/label for the same product attribute * * @param ProductInterface $product * @param $attributeCode * @return mixed */ public function getAttributeText(ProductInterface $product, $attributeCode) { if (!$dataValue = $product->getData($attributeCode)) { return null; } if (!isset($this->productAttributeTextCache[$attributeCode . '_' . $dataValue])) { $this->productAttributeTextCache[$attributeCode . '_' . $dataValue] = $product->getAttributeText($attributeCode); } return $this->productAttributeTextCache[$attributeCode . '_' . $dataValue]; } /** * Get attribute text by attribute value. * * @param $value * @param $attributeCode * @return bool|mixed|string * @throws \Magento\Framework\Exception\LocalizedException */ public function getAttributeTextByValue($value, $attributeCode) { $key = $attributeCode . '_value' . $value; if (!isset($this->productAttributeTextCache[$key])) { $this->productAttributeTextCache[$key] = $this->productResource->getAttribute('cnc_state_of_wear')->getSource()->getOptionText($value); } return $this->productAttributeTextCache[$key]; } /** * Get cnc_availability value for given sku and source code. * * @param $sku * @param $sourceCode * @return mixed */ public function getCncAvailabilityValue($sku, $sourceCode) { $availabilityAttributeId = $this->msiAttributes->getAttributeId('cnc_availability'); $availabilityValue = (int) $this->msiAttributes->getAttributeValue( $availabilityAttributeId, $sku, $sourceCode )->getValue(); return $this->msiAttributes ->getDropdownValue((string) $availabilityValue, 0) ->getValue(); } /** * @param $path * @return bool|false|mixed|string */ public static function getRealPathCaseInsensitive($path) { $me = __METHOD__; $path = rtrim(preg_replace('#[/\\\\]+#', DIRECTORY_SEPARATOR, $path), DIRECTORY_SEPARATOR); $realPath = realpath($path); if ($realPath !== false) { return $realPath; } $dir = dirname($path); if ($dir === $path) { return false; } $dir = $me($dir); if ($dir === false) { return false; } $search = strtolower(basename($path)); $pattern = ''; for ($pos = 0; $pos < strlen($search); $pos++) { $pattern .= sprintf('[%s%s]', $search[$pos], strtoupper($search[$pos])); } return current(glob($dir . DIRECTORY_SEPARATOR . $pattern)); } /** * Get sku by Id. * * @param $id * @return string */ public function getSkuById($id) { $connection = $this->resourceConnection->getConnection(); $select = $connection->select()->from('catalog_product_entity', 'sku')->where('entity_id = :entity_id'); $bind = [':entity_id' => (string)$id]; return $connection->fetchOne($select, $bind); } }