Quantcast
Channel: Webkul Blog
Viewing all articles
Browse latest Browse all 5490

Create Customer Attribute Without using Installer in Magento 2.

$
0
0

Create Customer Attribute Without using Installer in Magento 2

Here we will learn, how to create customer attribute without using installer in magento 2.0
1) Create a controller file: Webkul/CustomerAttribute/Controller/Adminhtml/Save.php

<?php

namespace Webkul\CustomerAttribute\Controller\Adminhtml\
use Magento\Backend\App\Action;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Model\Config;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Customer\Setup\CustomerSetupFactory;

class Save extends \Magento\Backend\App\Action
{
    /**
     * @var \Magento\Framework\Stdlib\DateTime\DateTime
     */
    protected $_moduleDataSetup;
    /**
     * @var CustomerSetupFactory
     */
    protected $_customerSetupFactory;


    protected $_eavSetup;

    protected $_eavConfig;
    /**
     * @var AttributeMetadataDataProvider
     */
    private $_attributeMetaData;
    /**
     * @param Action\Context $context
     */
    public function __construct(
        Action\Context $context,
        ModuleDataSetupInterface $moduleDataSetup,
        CustomerSetupFactory $customerSetupFactory,
        EavSetup $eavSetup,
        Config $eavConfig
    ) {
        $this->_moduleDataSetup = $moduleDataSetup;
        $this->_customerSetupFactory = $customerSetupFactory;
        $this->_eavSetup = $eavSetup;
        $this->_eavConfig = $eavConfig;
        parent::__construct($context);
    }

    /**
     * Save action.
     *
     * @return \Magento\Framework\Controller\ResultInterface
     */
    public function execute()
    {
        /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
        $resultRedirect = $this->resultRedirectFactory->create();

        $customerSetup = $this->_customerSetupFactory->create(['setup' => $this->_moduleDataSetup]);
        $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
        $customer = $this->_eavSetup->getEntityTypeId('customer');
        $attributeSetId = $customerEntity->getDefaultAttributeSetId();
        $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
        /*
        * Creating Custom Customer Attribute
        */
        $attribute = $this->_eavConfig->getAttribute('customer', 'custom_attribute');
        $attribute->addData('text' => [
                'frontend_type' => 'varchar',
                'backend_type' => 'varchar',
                'frontend_label' => 'Custom Attribute',
                'frontend_input' => 'text',
                'frontend_class' => '',
                'sort_order' => 1000,
                'is_visible' => 0,
                'is_system' => false,
                'is_user_defined' => true,
                'position' => 1000,
                'attribute_set_id' => $attributeSetId,
                'attribute_group_id' => $attributeGroupId,
                'used_in_forms' => ['adminhtml_customer'],

            ]
        );
        $attribute->save();

        return $resultRedirect->setPath('*/*/');
    }
}

Viewing all articles
Browse latest Browse all 5490

Trending Articles