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/vendor/extmag/shiplab/Setup/Patch/Data/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/old/vendor/extmag/shiplab/Setup/Patch/Data/CreateEAVAttributes.php
<?php
/**
 * Copyright © Extmag. All rights reserved.
 */

namespace Extmag\Shiplab\Setup\Patch\Data;

use Exception;
use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchRevertableInterface;

class CreateEAVAttributes implements DataPatchInterface, PatchRevertableInterface
{
    /**
     * @var EavSetupFactory
     */
    private $eavSetupFactory;
    /**
     * @var ModuleDataSetupInterface
     */
    private $moduleDataSetup;

    /**
     * @param ModuleDataSetupInterface $moduleDataSetup
     * @param EavSetupFactory $eavSetupFactory
     */
    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        EavSetupFactory $eavSetupFactory
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->eavSetupFactory = $eavSetupFactory;
    }

    /**
     * @inheritDoc
     */
    public static function getDependencies()
    {
        return [
            /*SomeDependency::class*/
        ];
    }

    /**
     * @inheritDoc
     */
    public function apply()
    {
        $this->moduleDataSetup->getConnection()->startSetup();

        try {
            $this->setEAVAttribute('extmag_harmonized_code', 'Harmonized (Commodity) Code', 'text', 10);
            $this->setEAVAttribute('extmag_scheduleb_code', 'ScheduleB Code', 'text', 20);
        } catch (Exception $e) {
            throw new Exception($e->getMessage());
        }

        $this->moduleDataSetup->getConnection()->endSetup();
    }

    public function revert()
    {
        $this->moduleDataSetup->getConnection()->startSetup();
        $this->moduleDataSetup->getConnection()->endSetup();
    }

    /**
     * @inheritDoc
     */
    public function getAliases()
    {
        return [];
    }

    /**
     * @param $attributeName
     * @param $attributeLabel
     * @param string $type
     * @param int $sort
     * @param $options
     */
    private function setEAVAttribute($attributeName, $attributeLabel, $type = 'text', $sort = 1, $options = [])
    {
        $entityTypeId = ProductAttributeInterface::ENTITY_TYPE_CODE;
        $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);

        $attributeSetId = $eavSetup->getDefaultAttributeSetId($entityTypeId);

        /**
         * Adding an attribute to a custom group
         */
        $attributeId = $eavSetup->getAttributeId($entityTypeId, $attributeName);
        $customGroupName = 'Extmag';

        $arrOptions = [
            'label' => $attributeLabel,
            'user_defined' => 1,
            'searchable' => 1,
            'required' => 0,
            'visible_on_front' => 0,
            'visible_in_advanced_search' => 0,
            'group' => $customGroupName,
            'input' => $type,
            'sort_order' => $sort,
        ];

        if ($type == 'select' || $type == 'multiselect') {
            $arrOptions['option'] = ['values' => $options];
        }

        $eavSetup->addAttribute(
            $entityTypeId,
            $attributeName,
            $arrOptions
        );
        $eavSetup->addAttributeGroup($entityTypeId, $attributeSetId, $customGroupName, 10);
        $eavSetup->addAttributeToGroup($entityTypeId, $attributeSetId, $customGroupName, $attributeId);
    }
}

Spamworldpro Mini