I need to add custom attribute for each customer. So admin can apply Customer Type for each customer. I've created module for this. It adds a select dropdown at the backend/adminhtml. but I cant save selected value to the database, It shows Add successful message, but When I check again, Value in select dropdown still is default value(which is an empty value).
Here is my code, I've used two files.
CustomerSetup.php
<?php
namespace MyTheme\CustomerTypes\Setup;
use Magento\Eav\Model\Config;
use Magento\Eav\Model\Entity\Setup\Context;
use Magento\Eav\Setup\EavSetup;
use Magento\Framework\App\CacheInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory;
class CustomerSetup extends EavSetup {
protected $eavConfig;
public function __construct(
ModuleDataSetupInterface $setup,
Context $context,
CacheInterface $cache,
CollectionFactory $attrGroupCollectionFactory,
Config $eavConfig
) {
$this -> eavConfig = $eavConfig;
parent :: __construct($setup, $context, $cache, $attrGroupCollectionFactory);
}
public function installAttributes($customerSetup) {
$this -> installCustomerAttributes($customerSetup);
$this -> installCustomerAddressAttributes($customerSetup);
}
public function installCustomerAttributes($customerSetup) {
$customerSetup -> addAttribute(\Magento\Customer\Model\Customer::ENTITY,
'customer_pay_type',
[
'type' => 'int',
'label' => 'Set Customer Type',
'input' => 'select',
'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Table',
'required' => false,
'visible' => true,
'user_defined' => true,
'sort_order' => 200,
'position' => 200,
'system' => 0,
'option' =>
array (
'values' =>
array (
0 => 'Customer Type1',
1 => 'Customer Type2',
2 => 'Customer Type3',
),
),
]
);
$customerSetup -> getEavConfig() -> getAttribute('customer', 'customer_pay_type')->setData('is_user_defined',1)->setData('is_required',0)->setData('default_value','')->setData('used_in_forms', ['adminhtml_customer', 'adminhtml_checkout']) -> save();
}
public function installCustomerAddressAttributes($customerSetup) {
}
public function getEavConfig() {
return $this -> eavConfig;
}
}
InstallData.php
<?php
namespace MyTheme\CustomerTypes\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;
/**
* @codeCoverageIgnore
*/
class InstallData implements InstallDataInterface
{
/**
* EAV setup factory
*
* @var EavSetupFactory
*/
private $eavSetupFactory;
/**
* Init
*
* @param EavSetupFactory $eavSetupFactory
*/
public function __construct(EavSetupFactory $eavSetupFactory)
{
$this->eavSetupFactory = $eavSetupFactory;
}
/**
* {@inheritdoc}
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
/** @var EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
if (version_compare($context->getVersion(), '1.0.0') < 0){
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSetup = $objectManager->create('MyTheme\CustomerTypes\Setup\CustomerSetup');
$customerSetup->installAttributes($customerSetup);
}
}
}
Can someone help me with this please? What is the problem here?
-
did you check if your field is created in customer_eav_attribute table ?Naveed Asim– Naveed Asim2017年12月07日 08:28:00 +00:00Commented Dec 7, 2017 at 8:28
-
@Naveed yes, the field is created. But when I check after saving for certain, customer nothing gets saved and it shows select dropdown box with default valueJoey– Joey2017年12月07日 08:31:34 +00:00Commented Dec 7, 2017 at 8:31
-
@Naveed Hi, I checked again, it created my field in eav_attribute table. not in customer_eav_attribute . Am I missing something here? or doing something wrong?Joey– Joey2017年12月07日 08:53:40 +00:00Commented Dec 7, 2017 at 8:53
2 Answers 2
Replace the code:
'type' => 'int',
'input' => 'select',
'source' => 'ModuleNameSpace\YourModuleName\Model\Config\Source\Options',
$customerSetup = $objectManager->create('Magento\Customer\Setup\CustomerSetup');
Also create file Options.php
<?php
namespace ModuleNameSpace\YourModuleName\Model\Config\Source;
use Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory;
use Magento\Framework\DB\Ddl\Table;
class Options extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{
public function getAllOptions()
{
$this->_options=[ ['label'=>'Select Options', 'value'=>''],
['label'=>'Option1', 'value'=>'1']
['label'=>'Option2', 'value'=>'2']
['label'=>'Option3', 'value'=>'3']
];
return $this->_options;
}
/**
* Get a text for option value
*
* @param string|integer $value
* @return string|bool
*/
public function getOptionText($value)
{
foreach ($this->getAllOptions() as $option) {
if ($option['value'] == $value) {
return $option['label'];
}
}
return false;
}
/**
* Retrieve flat column definition
*
* @return array
*/
public function getFlatColumns()
{
$attributeCode = $this->getAttribute()->getAttributeCode();
return [
$attributeCode => [
'unsigned' => false,
'default' => null,
'extra' => null,
'type' => Table::TYPE_INTEGER,
'nullable' => true,
'comment' => 'Custom Attribute Options ' . $attributeCode . ' column',
],
];
}
}
-
It didnt work. Still showing default value when I re check after the save customer. could you help me?Joey– Joey2017年12月07日 08:51:33 +00:00Commented Dec 7, 2017 at 8:51
-
Do you delete the old attribute in database?Nero Phung– Nero Phung2017年12月07日 08:55:26 +00:00Commented Dec 7, 2017 at 8:55
-
yes, I delete the row in eav_attribute table, then did setup:upgrade. after that my field is not showing at adminhtml/backendJoey– Joey2017年12月07日 08:57:24 +00:00Commented Dec 7, 2017 at 8:57
-
I have updated my answer.Nero Phung– Nero Phung2017年12月07日 09:08:43 +00:00Commented Dec 7, 2017 at 9:08
Try below code
UpgradeData.php
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace MyTheme\CustomerTypes\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\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;
class UpgradeData implements UpgradeDataInterface
{
/**
* @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;
}
/**
* put your comment there...
*
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
*/
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
if (version_compare($context->getVersion(), '1.0.1') < 0) {
/** @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, 'customer_pay_type', [
'type' => 'int',
'label' => 'Set Customer Type',
'input' => 'select',
'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Table',
'required' => false,
'user_defined' => true,
'sort_order' => 110,
'position' => 110,
'system' => false,
'option' => ['values' => ['Customer Type1', 'Customer Type2', 'Customer Type3']],
]);
$attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'customer_pay_type')->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['adminhtml_customer', 'customer_account_create', 'customer_account_edit', 'checkout_register']
]);
$attribute->save();
}
$setup->endSetup();
}
}
Updates setup_version 1.0.1 in your module.xml file
After run command:
php bin/magento setup:upgrade
Explore related questions
See similar questions with these tags.