InstallData.php

 avatar
user_7608791
php
a year ago
2.0 kB
1
Indexable
Never
<?php

/**
 * make custom attr for user
 * 
 * yasin 30 mart 2023
 *  */ 

namespace Udigital\CustomUserAttr\Setup;

use Magento\Eav\Setup\EavSetup;
// use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\Config;
use Magento\Customer\Model\Customer;

class InstallData implements InstallDataInterface
{
	private $eavSetupFactory;
    private $eavConfig;

	public function __construct(\Magento\Eav\Setup\EavSetupFactory $eavSetupFactory,  Config $eavConfig)
	{
		$this->eavSetupFactory = $eavSetupFactory;
        $this->eavConfig       = $eavConfig;
	}
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
	{
		$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $eavSetup->removeAttribute(
            Customer::ENTITY,
            'transport'
        );
		$eavSetup->addAttribute(
			Customer::ENTITY,
			'transport',
			[
				'type'         => 'varchar',
				'label'        => 'Transport',
				'input'        => 'select',
				'required'     => false,
				'visible'      => true,
				'user_defined' => true,
				'position'     => 999,
				'system'       => 0,
                'option'        => [
                    'values' => [
                        __('instore')->getText(), 
                        __('based_on_order')->getText(), 
                        __('freeshipping')->getText()
                        ]
                    ]
			]
		);
		$sampleAttribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'transport');

		// more used_in_forms ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address']
		$sampleAttribute->setData(
			'used_in_forms',
			['adminhtml_customer']
		);
		$sampleAttribute->save();
	}
}