Connect with us

Blog

magento2 – How To Create Customer Custom Attribute In Magento 2.0

mm

Published

on

create customer attribute in Magento 2

This article gives you dynamic code to will show how to create customer attribute in Magento 2. Please follow the steps to create the setup script classes.

Overview of Adding Customer Attribute In magento 2.0

  • Create setup file InstallData.php
  • Define the install() method
  • Create custom attribute

Create setup file InstallData.php

First, we will create the InstallData.php file in below mentioned path:

File Path: app/code/Webetutorial/Example/Setup/InstallData.php

<?php
namespace Webetutorial\Example\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;

class InstallData implements InstallDataInterface
{
	private $eavSetupFactory;

	public function __construct(EavSetupFactory $eavSetupFactory)
	{
		$this->eavSetupFactory = $eavSetupFactory;
	}

}

Define the install() method

After creating InstallData.php file, we have to define the install method and create eav setup model:

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
	{
		$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
	}

//Next,use eavSetup object to add attribute

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
	{
		$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
		$eavSetup->addAttribute(
			\Magento\Customer\Model\Customer::ENTITY,
			'color_attribute',
			[
				'type' => 'var',
				'label' => 'Color Attribute',
				'input' => 'select',
				'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
				'required' => true,
				'default' => 'red',
				'sort_order' => 100,
				'system' => false,
				'position' => 100
			]
		);
}

Create custom attribute

Finally, we have to set the forms where the custom attributes will be used. In this step, we will define the eavConfig object which allocates to call the attribute back and set the data for it. look below the following code:

Also Read :   How to Create Shipping Labels in Magento 2

File Path: app/code/Webetutorial/Example/Setup/InstallData.php

<?php
namespace Webetutorial\Example\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;

class InstallData implements InstallDataInterface
{
	private $eavSetupFactory;

	public function __construct(EavSetupFactory $eavSetupFactory, Config $eavConfig)
	{
		$this->eavSetupFactory = $eavSetupFactory;
		$this->eavConfig = $eavConfig;
	}

	public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
	{
		$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
		$eavSetup->addAttribute(
			\Magento\Customer\Model\Customer::ENTITY,
			'color_attribute',
			[
				'type' => 'var',
				'label' => 'Color Attribute',
				'input' => 'select',
				'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
				'required' => true,
				'default' => 'red',
				'sort_order' => 100,
				'system' => false,
				'position' => 100
			]
		);

		$sampleAttribute = $this->eavConfig->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'color_attribute');
		$sampleAttribute->setData(
			'used_in_forms',
			['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address']
		);
		$sampleAttribute->save();
	}
}

Now, your attribute ready to use just, run command to install the module:

php magento setup:upgrade

php bin/magento setup:static-content:deploy

If you still have any question or want to ask a question, you can contact us or ask your question with us. For more tutorials you can visit our other articles or visit our questions page to find out more questions.

You can join us on twitter or subscribe us on YouTube for more web design & development tutorials.

Hi, Thank you for reading my article. I am Victor, a professional blogger from Jaipur, India.I started webetutorial for blogging & sharing solution for developer questions, and now it’s empowering globally by helping them to make money.I am writing about starting & managing a blogs, WordPress, Magento, Social Media, SEO, Marketing, making money online, Investment, Finance, Gadgets, Fitness and more.

Continue Reading
Advertisement
Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

twenty + twenty =

Blog

Burj Al Arab Jumeirah – Luxurious Hotel in the World

mm

Published

on

Burj Al Arab Jumeirah – Luxurious Hotel in the World

Burj Al Arab Jumeirah is a world best luxurious hotel with their famous ultra-luxurious suites overlooking the sea. It is own island with nine signature restaurant. It has an opulent full-service spa and most attractive thing which guests may arrive to see the world’s largest chauffeur-driven fleets of Rolls-Royce’s.

The point which make luxurious to this property:

  • The roof terrace offers large swimming pools, 32 luxury cabanas same like a resort.
  • Floor have panoramic view of the Arabian Gulf
  • Each suite includes an iPad, WiFi, iMac and HD TV.
  • The hotel Bar is floating 200 m above sea level.
  • Guest may prefer to see sea in afternoon with tea and cocktails.
  • Talise Spa and ladies & gentlemen’s relaxation areas feature an aqua retreat.
  • It also includes separate indoor infinity pools facilities, hot tub and treatment room’s facilities, steam rooms and plunge pools facilities.
  • The property suite have private beach for special guests.
  • The property offers water sports activities at Wild Wadi Waterpark™.
Also Read :   How to Use Magento 2 Layouts, Blocks and Templates

Quick view on Property Amenities:

  • Pool
  • Room service
  • Restaurant
  • Free High Speed Internet (WiFi)
  • Fitness Centre with Gym / Workout Room
  • Free parking
  • Spa
  • Bar/Lounge
  • Adult pool
  • Airport transportation
  • Babysitting
  • Banquet Room
  • Breakfast Available
  • Business Centre with Internet Access
  • Children Activities (Kid / Family Friendly)
  • Concierge
  • Conference Facilities
  • Dry Cleaning
  • Heated pool
  • Hot Tub
  • Indoor pool
  • Laundry Service
  • Meeting rooms
  • Multilingual Staff
  • Non-smoking hotel
  • Outdoor pool
  • Sauna
  • Wheelchair Access

Quick view on Rooms:

  • Air conditioning
  • Minibar
  • Refrigerator in room
  • Accessible rooms
  • Family Rooms
  • Non-smoking rooms
  • Smoking rooms available
  • Suites

In the Dubai guests are getting more for their money when compared with other properties in the same location.

Subscribe us to get more hotels & resorts news or want to advertise your hostel with us, inquire now to get best quote from us.

Continue Reading

Trending