Spamworldpro Mini Shell
Spamworldpro


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/Customer/Setup/Patch/Data/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/old/app/code/Cnc/Customer/Setup/Patch/Data/TranslateCustomerAttributes.php
<?php
/**
 * @license http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 * @author Krzysztof Majkowski <[email protected]>
 * @copyright Copyright (c) 2020 Kaliop Digital Commerce (https://digitalcommerce.kaliop.com)
 */
declare(strict_types=1);

namespace Cnc\Customer\Setup\Patch\Data;

use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;

class TranslateCustomerAttributes implements DataPatchInterface
{
    private $attributes = [
        'customer' => [
            'cnc_industry_sector' => [
                'option' => [
                    'values' => [
                        'Aerospace' => 'Aéronautique & Spatial',
                        'Automotive' => 'Automobile',
                        'Agri-food' => 'Agroalimentaire',
                        'Metallurgy' => 'Métallurgie',
                        'Pharmaceutical' => 'Pharmaceutique',
                        'Metal transformation' => 'Transformation métallique',
                        'Precision mechanics' => 'Mécanique de précision',
                        'Naval' => 'Navale'
                    ]
                ],
            ],
            'cnc_source' => [
                'option' => [
                    'values' => [
                        'Google' => 'Google',
                        'Social media' => 'Réseaux Sociaux',
                        'Third party' => 'Intermédiaire',
                        'Advertising' => 'Publicité',
                        'Word of mouth' => 'Bouches à oreilles'
                    ]
                ],
            ]
        ]
    ];

    /**
     * @var ModuleDataSetupInterface
     */
    private $moduleDataSetup;

    /**
     * @var CustomerSetupFactory
     */
    private $customerSetupFactory;

    /**
     * @var ResourceConnection
     */
    private $resourceConnection;

    /**
     * CustomerAttributes constructor.
     * @param ModuleDataSetupInterface $moduleDataSetup
     * @param CustomerSetupFactory $customerSetupFactory
     * @param ResourceConnection $resourceConnection
     */
    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        CustomerSetupFactory $customerSetupFactory,
        ResourceConnection $resourceConnection
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->customerSetupFactory = $customerSetupFactory;
        $this->resourceConnection = $resourceConnection;
    }

    public function apply(): void
    {
        $customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);

        $customerAttributes = $this->attributes['customer'];
        foreach ($customerAttributes as $attributeName => $attributeData) {
            /** @var array $attribute */
            $attribute = $customerSetup->getAttribute(Customer::ENTITY, $attributeName);
            $conn = $this->resourceConnection->getConnection();

            $sql = 'SELECT option_id FROM eav_attribute_option WHERE attribute_id = ' . $attribute['attribute_id'];
            $options = $conn->fetchCol($sql);

            $sql = 'SELECT * FROM eav_attribute_option_value WHERE option_id IN (' . implode(',', $options) . ')';
            $optionValues = $conn->fetchAll($sql);

            $keys = array_keys($attributeData['option']['values']);
            foreach ($optionValues as $optionValue) {
                if (in_array($optionValue['value'], $keys)) {
                    $translatedValue = $attributeData['option']['values'][$optionValue['value']];

                    $deleteWhere = "option_id = {$optionValue['option_id']} AND store_id = 1";
                    $conn->delete('eav_attribute_option_value', $deleteWhere);
                    $conn->insert(
                        'eav_attribute_option_value',
                        [
                            'option_id' => $optionValue['option_id'],
                            'store_id' => 1,
                            'value' => $translatedValue
                        ]
                    );
                }
            }
        }
    }

    public static function getDependencies(): array
    {
        return [];
    }

    public function getAliases(): array
    {
        return [];
    }
}

Spamworldpro Mini