Latest Blogs
Application Modernization
Arpatech Website
Jan 10, 2025
Artificial Intelligence in Application Modernization: Key Benefits and Real-World Applications
Read More...
How to add customer attribute in Magento 2.x
Many of us are facing problems in adding custom attributes to customer profile in Magento 2. Here is how to add a custom field in Magento 2. Thanks to sashas extension for providing easy solution on adding a custom field.
There are many ways to add custom field for customers in Magento 2. Today we will focus on extension for Magento 2 which adds attribute to customer edit page at admin.
To begin with you will need Magento 2 installed with demo data. For more information you can see the Magento 2 installation guide.
Our step by step instruction allow you to create a successful attribute of Customer in admin panel.
You must have heard about the change in architecture of Magento with the release of Magento 2, but you don’t need to worry about it now. The extension files are placed exactly same way as it was in Magento 1.x. The extension files will be under app/code/(CompanyName)/(ExtensionName). For this tutorial the company name we will use is Sashas and extension name will be customerAttribute.
You can use you own company/extension name as per your need. We will create folders and path to the extension will be app/code/Sashas/CustomerAttribute/
The folder structure inside the extension folder in Magento 2 is quiet similar to Magento 1.x. We will need to create ‘etc’ and ‘setup’ folder inside the Magento extension folder.
The folder structure will look like this
In next step you will need to create module.xml file for extension to declare the version and visibility for Magento2. The module.xml file is replacement of for the previous module file under app/etc/modules folder. This way will lead all files of extension to be in one folder.
For this tutorial, I assume you know how to create a file in Magento 2. Read here to know more about Magento file structure.
To begin with, we need to create the file app/code/Sashas/CustomerAttribute/etc/module.xml. This file will make the extension visible.
module.xml
[xml]
<?xml version=”1.0″ encoding=”UTF-8″?>
<!–
/**
* @author Sashas
* @category Sashas
* @package Sashas_CustomerAttribute
* @copyright Copyright (c) 2015 Sashas IT Support Inc. (http://www.extensions.sashas.org)
*/
–>
<config xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:noNamespaceSchemaLocation=”../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd”>
<module name=”Sashas_CustomerAttribute” setup_version=”1.0.0″>
<sequence>
<module name=”Magento_Customer”/>
</sequence>
</module>
</config>
[/xml]
In the section we have defined the extension name and version. Here we used the name “Arpatech_CustomerAttr” and version = 1.0.0. You can use your own name of extension. Now we will add the list file. It will have the path app/code/Arpatech/customerAttr/Setup/InstallData.php. In next step we will create InstallData.php.
The install function here is used to add attribute for entity “customer” and after it we set it to be used in form adminhtml_customer.
As per your requirement the customer attribute can be used in different forms. For this tutorial we will use this attribute in customer edit page in admin panel.
“used_in_forms” => [‘adminhtml_customer_address’, ‘customer_address_edit’, ‘customer_register_address’]
[php]
<?php
/**
* @author Sashas
* @category Sashas
* @package Sashas_CustomerAttribute
* @copyright Copyright (c) 2015 Sashas IT Support Inc. (http://www.extensions.sashas.org)
*/
namespace Sashas\CustomerAttribute\Setup;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Customer\Model\Customer;
use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;</pre>
<pre>
/**
* @codeCoverageIgnore
*/
class InstallData implements InstallDataInterface
{
/**
* @var CustomerSetupFactory
*/
protected $customerSetupFactory;
/**
* @var AttributeSetFactory
*/
private $attributeSetFactory;
/**
* @param CustomerSetupFactory $customerSetupFactory
* @param AttributeSetFactory $attributeSetFactory
*/
public function __construct(
CustomerSetupFactory $customerSetupFactory,
AttributeSetFactory $attributeSetFactory
) {
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
}
/**
* {@inheritdoc}
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
/** @var CustomerSetup $customerSetup */
$customerSetup = $this->customerSetupFactory->create([‘setup’ => $setup]);
$customerEntity = $customerSetup->getEavConfig()->getEntityType(‘customer’);
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
/** @var $attributeSet AttributeSet */
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
$customerSetup->addAttribute(Customer::ENTITY, ‘magento_username’, [
‘type’ => ‘varchar’,
‘label’ => ‘Magento Username’,
‘input’ => ‘text’,
‘required’ => false,
‘visible’ => true,
‘user_defined’ => true,
‘sort_order’ => 1000,
‘position’ => 1000,
‘system’ => 0,
]);
$attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, ‘magento_username’)
->addData([
‘attribute_set_id’ => $attributeSetId,
‘attribute_group_id’ => $attributeGroupId,
‘used_in_forms’ => [‘adminhtml_customer’],
]);
$attribute->save();
}
}
[/php]
As you can see we have used attribute code magento_name and we set the properties in similar way as Magento 1.x. After you get through with the code you need to launch Magento Upgrade and clean cache.
You can use shell command to launch Magento Upgrade: php bin/magento setup:upgrade. This will perform all necessary upgrade for extension. After upgrading you will need to clear all the cache.
To clear the cache use the following shell command: php bin/magento cache:flush-all
I hope now you will be able to create customized plugin in Magento 2.
Know how to save your store against brute force attack.
Arpatech Website
Sep 19, 2016
“As with everything Magento, it’s how customiza...
William Boudle has vast years of experience in ecommerce industry and he is Certified Magento Solution Specialist. Currently he is working in JSR Direct as Director of Ecommerce. In this interview William shares his thoughts about “Magento issues”. He also shares his suggestions for peoples who want to become Magento certified and recommends useful Magento platform for learning about Magento and to get involve in Magento community.
William said “Don’t give up! Magento, at first can seem daunting, but once things start to click, you’ll wonder why people use any other eCommerce system.”
William also shares his personal life with some excited moment with our readers. Find more about William in this interview 🙂
Arpatech: William, currently you work at JSR Direct as Director of Ecommerce, since 2011. How did you start your career with Magento? What challenges did you face in the initial years of your career? Share your job role at JSR Direct with our readers.
William: Director of Ecommerce translates to “I wear many hats” from frontend & backend to marketing, SEO and everything in between. I primarily deal with musicians & entertainers as an industry, but their target demographics can sometimes be worlds apart. I work with clients ranging from Weird Al, The Zombies & Tiesto; to Margaret Cho, Andrea Bocelli and numerous broadway shows including Hamilton the Musical & CATS. Our primary niche however is heavy metal, with products that often need to be kept isolated from our more reserved clients’ catalogs.
When I started at JSR, they were using a SaaS eCommerce platform, but had a laundry list of requirements and ideals they wanted out of an eCommerce site. My prior experience had been with osCommerce, but by 2011, Magento was the clear winner in highly customizable eCommerce.
There were so many challenges moving to Magento that there were a few times I wondered what I had gotten myself into. We had dozens of microsites and both B2B and B2C sites to convert. As mentioned above our clients have their own separate target demographics. Then there was an ERP integration, which always present their own challenges. We also aren’t always told when a client is going to promote their store, so we always need to be prepared for a spike in traffic.
Through the challenges though, I’ve become a huge fan of Magento and am still in awe of what an extremely powerful framework it is.
Arpatech: William you are a certified Magento solution specialist. What are the 3 most viable tips for newbies who want to involve themselves in Magento? Could you recommend a useful platform for Magento training? What is the reason behind connecting yourself with Magento?
William: Read and answer on magento.stackexchange.com. Sort by votes and you’ll see answers to some of the most popular questions you’ll face when starting Magento.
– The magento community is very tight knit, but also very welcoming. People who may appear to be Magento gurus are accessible via twitter and reddit and put a lot of their own time into providing free and helpful information.
– Don’t give up. Magento, at first can seem daunting, but once things start to click, you’ll wonder why people use any other eCommerce system.
There are a ton of great resource for Magento training. Definitely check out Alan Storm, his blog and modules are an excellent place to start. Another great one I’ve found is MageStudyGuide.com. It goes over the fundamentals necessary to know how Magento is working “under the hood”.
Arpatech: Nowadays, Magento is a very popular ecommerce platform. What are some useful front-end features in Magento 1X for developers?
William: As with everything Magento, it’s how customizable it is. Particularly with custom attributes and layout xml. We needed branded ecommerce microsites for musicians, along with links to, and feeds from, their social media accounts. This was achieved with custom attributes that could link products and categories to clients, and custom layout updates to stylize their individual stores.
Most people who begin Magento front-end development quickly find that it is a little overwhelming, and that it has its own approach to MVC. Finding which file information on your product page is loading from can seem daunting. Template hints were a huge help to me when trying to grasp Magento’s fall back structure. I especially like the Aoe_templateHints module (https://github.com/AOEpeople/Aoe_TemplateHints) as it looks cleaner and shows more information, like cache status of blocks.
Arpatech: William, as a Director of Ecommerce what are the most common questions you face from your customers? Magento is a very popular ecommerce platform. How do you see Magento growth in the ecommerce industry? What are the main problems Magento Merchants face?
William: The most common request I get is for more or better reporting. While Magento has some pretty decent reporting functionality built in, working with a large number of clients means lots of requests for different sales metrics. Kalen Jordan’s Custom Reports are a lifesaver for this. I’ve been able to make dozens of custom reports for clients with Google Charts and all.
I am a little worried that Magento 2’s complexity will drive developers to use other platforms. Similarly, the problem I think most merchants face is Magento’s steep learning curve. You are almost required to have a developer on staff if you want to run a small to mid-size Magento site and there’s so much going on behind the scenes that there are features even long term Magento developer’s haven’t used.
Arpatech: What’s your advice for merchants who run their store on Magento and want to increase their store’s sale? Does SEO play a significant role in store sales? What is the best method of store marketing?
William: SEO is huge. So many eCommerce sites fall short in this area and it’s another one where I think Magento shines if you take advantage of its features. Sitemaps, SEF & canonical URL. It’s easy to build landing pages and blocks with the CMS. Another good SEO technique worth mentioning is Rich Snippets, though it requires an extension or some simple site modifications. They can really help your products stand out in Google with reviews, pricing, stock availability and more.
I think is also worth considering other popular platforms such as Amazon & Ebay, especially if you have a unique product. Both of those can be integrated into Magento using M2E so you can continue to use Magento as a hub for online orders and product management.
Arpatech: Magento 2 is on the market. How will Magento 2 impact the ecommerce industry? What’s your advice for SEOs who want to improve their ecommerce store ranking? In your opinion, how can merchants improve their store with Magento 2?
William: Things I’m excited about in Magento 2 is the use of HTML5, LESS CSS and the switch from Prototype.js to jQuery. I still think it will take some time for those with large sums of money and time invested in Magento 1 to make the switch.
I think Magento 2 will be an improvement for many merchants because they will be easier to update, and easier to manage for store admins. Updating has historically been an area of concern for Magento, but Magento 2 and composer should make the task far less cumbersome. Also a full page caching
Arpatech: What are some useful features in Magento 2.x for developers? What are your thoughts about the front-end of Magento 2? As a Magento Solution Specialist what tools would you recommend to speed up Magento websites?
William: The built in CLI tools will be super helpful. I also think the new file structure is a little easier to grasp as the files aren’t as spread out across the filesystem.
I really like the new Luma theme and think the tile design is a step in the right direction towards a cleaner, mobile-first eCommerce experience. I’m still just in the “playing around” phase with Magento 2, but can say that I miss having a local.xml file a little. I also wish they would have chosen Sass over LESS for CSS, as I think it’s come out as the clear leader of the two. Hopefully they will make the change sooner rather than later, while Magento 2 is still in its infancy.
There are several great options for full page caching, some free, others are premium. I would definitely recommend taking a look at Varnish. Probably one of the most important requirements to get good performance from your site is to use a good hosting provider.
Arpatech: Magento has a vast community and really helpful members. How do you connect yourself in the community? Which social network do you prefer to connect yourself with the community?
William: The Magento Community is very active on Twitter, LinkedIn and even pretty active on Reddit. I’ve also had some good interactions on Quora. Truth told, there are some great Magento Community members available on almost all the major social media sites. “Magento” is one of the first phrases I search when checking out a new social network.
Arpatech: Magento Imagine, Meet Magento, Magento hackathon, Mage Titan are the best platforms for people who want to connect with the Magento Community. What’s your advice for amateur developers who want to involve themselves in the Magento Community? Have you been a part of any Meet Magento or Magento Conference? Or, in the future do you plan to attend Meet Magento or any other Magento related event?
William: I’ve only had the opportunity to connect online so far. Where I currently live, there are not many Magento Meetups and the few there are just can’t seem to take off. I plan on moving to the Orlando area this year, and if there isn’t one there, then I plan on starting one! I’ve also already told my company I’m attending Magento Imagine 2017.
Arpatech: William, let’s put Magento aside and talk about your personal interests :). I follow you on Twitter, and I think you love to spend time with your family. What’s your favorite place and how many trips have you taken so far? How do you spend your free time? Share some interesting moment from your personal life with our readers. 🙂
William: My family are big Disney geeks. Now that Disney owns Star Wars and Marvel, I think they get all my extra money, lol. I love how Disney blends engineering, design, and tells a story to create a user experience. Gee, it’s almost like web design! I think balance in work life is important, especially to those of us in tech. I will be moving to the Orlando area soon, because there is so much to do there for families. Life is short, and I want to blur the line between vacation and daily life. It’s like that Confucius quote, “Choose a job you love and you will never have to work a day in your life”. That’s what I’m striving for, with Disney World on the weekends, lol.
Arpatech: There are always some individuals who stand out in the crowd. Name some people that inspired and influenced you in the Magento world?
William: Magento definitely has some stand out people and thriving community. In fact Sherrie Rohde, Magento Community Manager, put together the “Magento Masters” to highlight some of the best magenticians and it’s most helpful. I will say that anyone on that list, particularly the Mentors, are worth following.
Another helpful way to find the Magento influencers is to follow everyone on the Magento Stack Exchange Reputation list. (http://magento.stackexchange.com/users?tab=Reputation&filter=all)
Phil “Winkle” Jackson: One half of the MageTalk.com podcast and a guy with some of the best answers on Magento StackExchange. He and Kalen Jordan are just great to listen to when you’re want to stay inspired without feeling like you’re listening to a work podcast.
Alan Storm: If you climbed to the top of Magento Mountain, you might find Alan Storm, almost everyone in the Magento community has probably found an answer in one of his posts and he has probably helped launch entire Magento careers.
Marius Strajeru: If you sort by reputation on Magento StackExchange, Marius takes the gold. His answers to some of the most popular questions are well thought out. Oh yeah, and his Ultimate Module Creator, which is free, is just amazing if you’re looking to get into Magento Module development…
Ben Marks: He is currently a Magento Community Manager and is active and helpful on Twitter and Reddit. He and Sherry Rohde are the glue that binds the Magento community to Magento corporate. He also deserves honorable mention for being the teacher in the Fundamentals of Magento Development course.
Fabrizio Branca: I actually have an IFTTT alert to let me know whenever AOE adds a new extension to GitHub, because they are just that awesome.
Arpatech Website
Sep 16, 2016
Design
Art
AI
Development
Apps