![]() 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/Setup/Patch/Data/ |
<?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) * Cnc * Krzysztof Majkowski <[email protected]> */ namespace Cnc\Catalog\Setup\Patch\Data; use Cnc\Catalog\Model\Config; use Magento\Catalog\Api\Data\EavAttributeInterface; use Magento\Catalog\Api\ProductAttributeRepositoryInterface; use Magento\Catalog\Model\Product; use Magento\Eav\Api\Data\AttributeFrontendLabelInterfaceFactory; use Magento\Eav\Setup\EavSetup; use Magento\Eav\Setup\EavSetupFactory; use Magento\Framework\App\ResourceConnection; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Framework\Setup\Patch\DataPatchInterface; class UpdateAvailabilityIcon implements DataPatchInterface { /** * ModuleDataSetupInterface * * @var ModuleDataSetupInterface */ private $moduleDataSetup; /** * EavSetupFactory * * @var EavSetupFactory */ private $eavSetupFactory; /** * @var ProductAttributeRepositoryInterface */ private $productAttributeRepository; /** * @var AttributeFrontendLabelInterfaceFactory */ private $attributeFrontendLabelFactory; /** * @var \Magento\Eav\Model\Config */ private $eavConfig; /** * @var ResourceConnection */ private $resourceConnection; /** * AddRecommendedAttribute constructor. * * @param ModuleDataSetupInterface $moduleDataSetup * @param EavSetupFactory $eavSetupFactory * @param ProductAttributeRepositoryInterface $productAttributeRepository * @param AttributeFrontendLabelInterfaceFactory $attributeFrontendLabelFactory * @param \Magento\Eav\Model\Config $eavConfig * @param ResourceConnection $resourceConnection */ public function __construct( ModuleDataSetupInterface $moduleDataSetup, EavSetupFactory $eavSetupFactory, ProductAttributeRepositoryInterface $productAttributeRepository, AttributeFrontendLabelInterfaceFactory $attributeFrontendLabelFactory, \Magento\Eav\Model\Config $eavConfig, ResourceConnection $resourceConnection ) { $this->moduleDataSetup = $moduleDataSetup; $this->eavSetupFactory = $eavSetupFactory; $this->productAttributeRepository = $productAttributeRepository; $this->attributeFrontendLabelFactory = $attributeFrontendLabelFactory; $this->eavConfig = $eavConfig; $this->resourceConnection = $resourceConnection; } public function apply() { /** @var EavSetup $eavSetup */ $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]); $attribute = $this->productAttributeRepository->get(Config::PRODUCT_ATTRIBUTE_CODE_AVAILABILITY_ICON); $sourceModel = $attribute->getSourceModel(); $frontendLabels = [ $this->attributeFrontendLabelFactory->create() ->setStoreId(1) ->setLabel('Disponibilité'), ]; $attribute->setDefaultFrontendLabel('Availability'); $attribute->setFrontendLabels($frontendLabels); $this->productAttributeRepository->save($attribute); if ($sourceModel !== null) { $eavSetup->updateAttribute( Product::ENTITY, Config::PRODUCT_ATTRIBUTE_CODE_AVAILABILITY_ICON, EavAttributeInterface::SOURCE_MODEL, $sourceModel ); } $options = Config::PRODUCT_ATTRIBUTE_CODE_AVAILABILITY_ICON_OPTIONS['value']; $attribute = $this->eavConfig->getAttribute( Product::ENTITY, Config::PRODUCT_ATTRIBUTE_CODE_AVAILABILITY_ICON ); $attribute->setStoreId(0); foreach ($options as $option) { $eavOptionId = $attribute->getSource()->getOptionId($option[0]); if ($eavOptionId) { $this->resourceConnection->getConnection()->insertOnDuplicate( 'eav_attribute_option_value', [ 'option_id' => $eavOptionId, 'store_id' => 1, 'value' => $option[1] ] ); } } } /** * @return array|string[] */ public static function getDependencies() { return []; } /** * @return array|string[] */ public function getAliases() { return []; } }