Magento 2.2. Customer Custom Attribute
I want add bonus to my customer, in the internet i read that i need use custom attribute.
etc/Module.xml
<module name="Kt_Addbonus" setup_version="1.2.2">
<sequence>
<module name="Customer"/>
</sequence>
</module>
Setup/InstallData.php
<?php
use MagentoCustomerSetupCustomerSetupFactory;
use MagentoCustomerModelCustomer;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;
class InstallData implements InstallDataInterface
{
/**
* @var CustomerSetupFactory
*/
private $customerSetupFactory;
/**
* @var AttributeSetFactory
*/
private $attributeSetFactory;
public function __construct( CustomerSetupFactory $customerSetupFactory,
AttributeSetFactory $attributeSetFactory)
{
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
}
/**
* Installs data for a module
*
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
* @return void
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$setup->startSetup();
$attributesInfo = [
'customer_bonus' => [
'label' => 'Customer Bonus',
'type' => 'int',
'input' => 'text',
'position' => 1000,
'visible' => true,
'required' => false,
'system' => 0,
'user_defined' => true,
]
];
$customerEntity =$customerSetup->getEavConfig()->getEntityType('customer');
$attributeSetId = $customerEntity->getDefaultGroupId();
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
foreach ($attributesInfo as $attributeCode => $attributeParams) {
$customerSetup->addAttribute(Customer::ENTITY, $attributeCode, $attributeParams);
}
$customerBonusAttribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'customer_bonus');
$customerBonusAttribute->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms'=>['adminhtml_customer']
]);
$customerBonusAttribute->save();
$setup->endSetup();
}
}
Custom attribute dont add to db or admin`s create customer page.
And PhpStorm says me that CustomerSetupFactory undefind, i already tried
setup:di:compile
setup:upgrade and cache:flush
did too.
Update 1
CustomerSetupFactory was generated. But custom attribute still not added.
magento2 customer-attribute php-7
add a comment |
I want add bonus to my customer, in the internet i read that i need use custom attribute.
etc/Module.xml
<module name="Kt_Addbonus" setup_version="1.2.2">
<sequence>
<module name="Customer"/>
</sequence>
</module>
Setup/InstallData.php
<?php
use MagentoCustomerSetupCustomerSetupFactory;
use MagentoCustomerModelCustomer;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;
class InstallData implements InstallDataInterface
{
/**
* @var CustomerSetupFactory
*/
private $customerSetupFactory;
/**
* @var AttributeSetFactory
*/
private $attributeSetFactory;
public function __construct( CustomerSetupFactory $customerSetupFactory,
AttributeSetFactory $attributeSetFactory)
{
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
}
/**
* Installs data for a module
*
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
* @return void
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$setup->startSetup();
$attributesInfo = [
'customer_bonus' => [
'label' => 'Customer Bonus',
'type' => 'int',
'input' => 'text',
'position' => 1000,
'visible' => true,
'required' => false,
'system' => 0,
'user_defined' => true,
]
];
$customerEntity =$customerSetup->getEavConfig()->getEntityType('customer');
$attributeSetId = $customerEntity->getDefaultGroupId();
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
foreach ($attributesInfo as $attributeCode => $attributeParams) {
$customerSetup->addAttribute(Customer::ENTITY, $attributeCode, $attributeParams);
}
$customerBonusAttribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'customer_bonus');
$customerBonusAttribute->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms'=>['adminhtml_customer']
]);
$customerBonusAttribute->save();
$setup->endSetup();
}
}
Custom attribute dont add to db or admin`s create customer page.
And PhpStorm says me that CustomerSetupFactory undefind, i already tried
setup:di:compile
setup:upgrade and cache:flush
did too.
Update 1
CustomerSetupFactory was generated. But custom attribute still not added.
magento2 customer-attribute php-7
add a comment |
I want add bonus to my customer, in the internet i read that i need use custom attribute.
etc/Module.xml
<module name="Kt_Addbonus" setup_version="1.2.2">
<sequence>
<module name="Customer"/>
</sequence>
</module>
Setup/InstallData.php
<?php
use MagentoCustomerSetupCustomerSetupFactory;
use MagentoCustomerModelCustomer;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;
class InstallData implements InstallDataInterface
{
/**
* @var CustomerSetupFactory
*/
private $customerSetupFactory;
/**
* @var AttributeSetFactory
*/
private $attributeSetFactory;
public function __construct( CustomerSetupFactory $customerSetupFactory,
AttributeSetFactory $attributeSetFactory)
{
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
}
/**
* Installs data for a module
*
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
* @return void
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$setup->startSetup();
$attributesInfo = [
'customer_bonus' => [
'label' => 'Customer Bonus',
'type' => 'int',
'input' => 'text',
'position' => 1000,
'visible' => true,
'required' => false,
'system' => 0,
'user_defined' => true,
]
];
$customerEntity =$customerSetup->getEavConfig()->getEntityType('customer');
$attributeSetId = $customerEntity->getDefaultGroupId();
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
foreach ($attributesInfo as $attributeCode => $attributeParams) {
$customerSetup->addAttribute(Customer::ENTITY, $attributeCode, $attributeParams);
}
$customerBonusAttribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'customer_bonus');
$customerBonusAttribute->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms'=>['adminhtml_customer']
]);
$customerBonusAttribute->save();
$setup->endSetup();
}
}
Custom attribute dont add to db or admin`s create customer page.
And PhpStorm says me that CustomerSetupFactory undefind, i already tried
setup:di:compile
setup:upgrade and cache:flush
did too.
Update 1
CustomerSetupFactory was generated. But custom attribute still not added.
magento2 customer-attribute php-7
I want add bonus to my customer, in the internet i read that i need use custom attribute.
etc/Module.xml
<module name="Kt_Addbonus" setup_version="1.2.2">
<sequence>
<module name="Customer"/>
</sequence>
</module>
Setup/InstallData.php
<?php
use MagentoCustomerSetupCustomerSetupFactory;
use MagentoCustomerModelCustomer;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;
class InstallData implements InstallDataInterface
{
/**
* @var CustomerSetupFactory
*/
private $customerSetupFactory;
/**
* @var AttributeSetFactory
*/
private $attributeSetFactory;
public function __construct( CustomerSetupFactory $customerSetupFactory,
AttributeSetFactory $attributeSetFactory)
{
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
}
/**
* Installs data for a module
*
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
* @return void
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$setup->startSetup();
$attributesInfo = [
'customer_bonus' => [
'label' => 'Customer Bonus',
'type' => 'int',
'input' => 'text',
'position' => 1000,
'visible' => true,
'required' => false,
'system' => 0,
'user_defined' => true,
]
];
$customerEntity =$customerSetup->getEavConfig()->getEntityType('customer');
$attributeSetId = $customerEntity->getDefaultGroupId();
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
foreach ($attributesInfo as $attributeCode => $attributeParams) {
$customerSetup->addAttribute(Customer::ENTITY, $attributeCode, $attributeParams);
}
$customerBonusAttribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'customer_bonus');
$customerBonusAttribute->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms'=>['adminhtml_customer']
]);
$customerBonusAttribute->save();
$setup->endSetup();
}
}
Custom attribute dont add to db or admin`s create customer page.
And PhpStorm says me that CustomerSetupFactory undefind, i already tried
setup:di:compile
setup:upgrade and cache:flush
did too.
Update 1
CustomerSetupFactory was generated. But custom attribute still not added.
magento2 customer-attribute php-7
magento2 customer-attribute php-7
edited 30 mins ago
Teja Bhagavan Kollepara
2,96341847
2,96341847
asked Mar 6 '18 at 7:00
newakkoffnewakkoff
1086
1086
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Please confirm that have you created registration.php on your module root folder like :
<?php
MagentoFrameworkComponentComponentRegistrar::register(
MagentoFrameworkComponentComponentRegistrar::MODULE, 'Kt_Addbonus', __DIR__
);
Also replace following code into your InstallData.php
<?php
namespace KtAddbonusSetup;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{
private $customerSetupFactory;
private $attributeSetFactory;
public function __construct(MagentoCustomerSetupCustomerSetupFactory $customerSetupFactory, MagentoEavModelEntityAttributeSetFactory $attributeSetFactory)
{
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$installer = $setup;
$installer->startSetup();
/** @var CustomerSetup $customerSetup */
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerSetup->removeAttribute(MagentoCustomerModelCustomer::ENTITY, "customer_bonus");
$customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
$customerSetup->addAttribute(MagentoCustomerModelCustomer::ENTITY, 'customer_bonus', [
'type' => 'varchar',
'label' => 'Customer Bonus',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'system' => false,
]);
$attribute = $customerSetup->getEavConfig()->getAttribute(MagentoCustomerModelCustomer::ENTITY, 'customer_bonus')
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms'=>['adminhtml_customer']
]);
$attribute->save();
$installer->endSetup();
}
}
After above you have to run your extension again you need to remove your extension entry from setup_module table also then run below command
php bin/magento setup:upgrade
Thanks
Means attribute is not created or you got any error ?
– Hardik Patel
Mar 6 '18 at 8:01
attribute is not created, i have no any error
– newakkoff
Mar 6 '18 at 8:01
oh no im sorry, its work, your answer added customer_bonus attribute to eav_attribute and customer_eav_attribute tables, thank you very much.
– newakkoff
Mar 6 '18 at 8:16
add a comment |
I think your missing namespace
in your setup script,
<?php
namespace KtAddbonusSetup;
use MagentoCustomerSetupCustomerSetupFactory;
use MagentoCustomerModelCustomer;
use MagentoEavModelEntityAttributeSet as AttributeSet;
use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
/**
* @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;
}
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, 'custom_attribute', [
'type' => 'varchar',
'label' => 'Customer Bonus',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'position' =>999,
'system' => 0,
]);
$attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'custom_attribute')
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['adminhtml_customer'],//you can use other forms also ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address']
]);
$attribute->save();
}
}
thank you for your answer, unfortunately it did not help.
– newakkoff
Mar 6 '18 at 7:23
what error coming same?
– Jeeva Chezhiyan
Mar 6 '18 at 7:29
@newakkoff check this mageplaza.com/magento-2-module-development/…
– Jeeva Chezhiyan
Mar 6 '18 at 7:38
thank you, but i already tried all guides in internet)
– newakkoff
Mar 6 '18 at 7:39
@newakkoff mage2gen.com try this extension creator. Create the extension and update it. This extension will give clear definition for you..
– Jeeva Chezhiyan
Mar 6 '18 at 7:43
|
show 3 more comments
You can not easily use custom customer attributes in Magento 2 community edition.
It's a paid feature in Enterprise Edition.
You can add it programmatically but in my opinion they done a lot to make it as difficult as possible.
See this thread for reference:
https://github.com/magento/magento2/issues/6575
thanks if it's a paid function, then it's better to add a column to customer_entity, right? and extend CustomerInterface, Can I do that?
– newakkoff
Mar 6 '18 at 7:37
1
However you do it, you have to extend and overwrite a lot of stuff backend and frontend wise to make it work. See the linked thread or maybe look at an extension that does it. If you are not deep in the topic and do not want to dive deep. I'd rather suggest using an extension.
– steros
Mar 6 '18 at 7:48
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "479"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f216104%2fmagento-2-2-customer-custom-attribute%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Please confirm that have you created registration.php on your module root folder like :
<?php
MagentoFrameworkComponentComponentRegistrar::register(
MagentoFrameworkComponentComponentRegistrar::MODULE, 'Kt_Addbonus', __DIR__
);
Also replace following code into your InstallData.php
<?php
namespace KtAddbonusSetup;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{
private $customerSetupFactory;
private $attributeSetFactory;
public function __construct(MagentoCustomerSetupCustomerSetupFactory $customerSetupFactory, MagentoEavModelEntityAttributeSetFactory $attributeSetFactory)
{
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$installer = $setup;
$installer->startSetup();
/** @var CustomerSetup $customerSetup */
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerSetup->removeAttribute(MagentoCustomerModelCustomer::ENTITY, "customer_bonus");
$customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
$customerSetup->addAttribute(MagentoCustomerModelCustomer::ENTITY, 'customer_bonus', [
'type' => 'varchar',
'label' => 'Customer Bonus',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'system' => false,
]);
$attribute = $customerSetup->getEavConfig()->getAttribute(MagentoCustomerModelCustomer::ENTITY, 'customer_bonus')
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms'=>['adminhtml_customer']
]);
$attribute->save();
$installer->endSetup();
}
}
After above you have to run your extension again you need to remove your extension entry from setup_module table also then run below command
php bin/magento setup:upgrade
Thanks
Means attribute is not created or you got any error ?
– Hardik Patel
Mar 6 '18 at 8:01
attribute is not created, i have no any error
– newakkoff
Mar 6 '18 at 8:01
oh no im sorry, its work, your answer added customer_bonus attribute to eav_attribute and customer_eav_attribute tables, thank you very much.
– newakkoff
Mar 6 '18 at 8:16
add a comment |
Please confirm that have you created registration.php on your module root folder like :
<?php
MagentoFrameworkComponentComponentRegistrar::register(
MagentoFrameworkComponentComponentRegistrar::MODULE, 'Kt_Addbonus', __DIR__
);
Also replace following code into your InstallData.php
<?php
namespace KtAddbonusSetup;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{
private $customerSetupFactory;
private $attributeSetFactory;
public function __construct(MagentoCustomerSetupCustomerSetupFactory $customerSetupFactory, MagentoEavModelEntityAttributeSetFactory $attributeSetFactory)
{
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$installer = $setup;
$installer->startSetup();
/** @var CustomerSetup $customerSetup */
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerSetup->removeAttribute(MagentoCustomerModelCustomer::ENTITY, "customer_bonus");
$customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
$customerSetup->addAttribute(MagentoCustomerModelCustomer::ENTITY, 'customer_bonus', [
'type' => 'varchar',
'label' => 'Customer Bonus',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'system' => false,
]);
$attribute = $customerSetup->getEavConfig()->getAttribute(MagentoCustomerModelCustomer::ENTITY, 'customer_bonus')
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms'=>['adminhtml_customer']
]);
$attribute->save();
$installer->endSetup();
}
}
After above you have to run your extension again you need to remove your extension entry from setup_module table also then run below command
php bin/magento setup:upgrade
Thanks
Means attribute is not created or you got any error ?
– Hardik Patel
Mar 6 '18 at 8:01
attribute is not created, i have no any error
– newakkoff
Mar 6 '18 at 8:01
oh no im sorry, its work, your answer added customer_bonus attribute to eav_attribute and customer_eav_attribute tables, thank you very much.
– newakkoff
Mar 6 '18 at 8:16
add a comment |
Please confirm that have you created registration.php on your module root folder like :
<?php
MagentoFrameworkComponentComponentRegistrar::register(
MagentoFrameworkComponentComponentRegistrar::MODULE, 'Kt_Addbonus', __DIR__
);
Also replace following code into your InstallData.php
<?php
namespace KtAddbonusSetup;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{
private $customerSetupFactory;
private $attributeSetFactory;
public function __construct(MagentoCustomerSetupCustomerSetupFactory $customerSetupFactory, MagentoEavModelEntityAttributeSetFactory $attributeSetFactory)
{
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$installer = $setup;
$installer->startSetup();
/** @var CustomerSetup $customerSetup */
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerSetup->removeAttribute(MagentoCustomerModelCustomer::ENTITY, "customer_bonus");
$customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
$customerSetup->addAttribute(MagentoCustomerModelCustomer::ENTITY, 'customer_bonus', [
'type' => 'varchar',
'label' => 'Customer Bonus',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'system' => false,
]);
$attribute = $customerSetup->getEavConfig()->getAttribute(MagentoCustomerModelCustomer::ENTITY, 'customer_bonus')
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms'=>['adminhtml_customer']
]);
$attribute->save();
$installer->endSetup();
}
}
After above you have to run your extension again you need to remove your extension entry from setup_module table also then run below command
php bin/magento setup:upgrade
Thanks
Please confirm that have you created registration.php on your module root folder like :
<?php
MagentoFrameworkComponentComponentRegistrar::register(
MagentoFrameworkComponentComponentRegistrar::MODULE, 'Kt_Addbonus', __DIR__
);
Also replace following code into your InstallData.php
<?php
namespace KtAddbonusSetup;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{
private $customerSetupFactory;
private $attributeSetFactory;
public function __construct(MagentoCustomerSetupCustomerSetupFactory $customerSetupFactory, MagentoEavModelEntityAttributeSetFactory $attributeSetFactory)
{
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$installer = $setup;
$installer->startSetup();
/** @var CustomerSetup $customerSetup */
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerSetup->removeAttribute(MagentoCustomerModelCustomer::ENTITY, "customer_bonus");
$customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
$customerSetup->addAttribute(MagentoCustomerModelCustomer::ENTITY, 'customer_bonus', [
'type' => 'varchar',
'label' => 'Customer Bonus',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'system' => false,
]);
$attribute = $customerSetup->getEavConfig()->getAttribute(MagentoCustomerModelCustomer::ENTITY, 'customer_bonus')
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms'=>['adminhtml_customer']
]);
$attribute->save();
$installer->endSetup();
}
}
After above you have to run your extension again you need to remove your extension entry from setup_module table also then run below command
php bin/magento setup:upgrade
Thanks
answered Mar 6 '18 at 7:26
Hardik PatelHardik Patel
508313
508313
Means attribute is not created or you got any error ?
– Hardik Patel
Mar 6 '18 at 8:01
attribute is not created, i have no any error
– newakkoff
Mar 6 '18 at 8:01
oh no im sorry, its work, your answer added customer_bonus attribute to eav_attribute and customer_eav_attribute tables, thank you very much.
– newakkoff
Mar 6 '18 at 8:16
add a comment |
Means attribute is not created or you got any error ?
– Hardik Patel
Mar 6 '18 at 8:01
attribute is not created, i have no any error
– newakkoff
Mar 6 '18 at 8:01
oh no im sorry, its work, your answer added customer_bonus attribute to eav_attribute and customer_eav_attribute tables, thank you very much.
– newakkoff
Mar 6 '18 at 8:16
Means attribute is not created or you got any error ?
– Hardik Patel
Mar 6 '18 at 8:01
Means attribute is not created or you got any error ?
– Hardik Patel
Mar 6 '18 at 8:01
attribute is not created, i have no any error
– newakkoff
Mar 6 '18 at 8:01
attribute is not created, i have no any error
– newakkoff
Mar 6 '18 at 8:01
oh no im sorry, its work, your answer added customer_bonus attribute to eav_attribute and customer_eav_attribute tables, thank you very much.
– newakkoff
Mar 6 '18 at 8:16
oh no im sorry, its work, your answer added customer_bonus attribute to eav_attribute and customer_eav_attribute tables, thank you very much.
– newakkoff
Mar 6 '18 at 8:16
add a comment |
I think your missing namespace
in your setup script,
<?php
namespace KtAddbonusSetup;
use MagentoCustomerSetupCustomerSetupFactory;
use MagentoCustomerModelCustomer;
use MagentoEavModelEntityAttributeSet as AttributeSet;
use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
/**
* @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;
}
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, 'custom_attribute', [
'type' => 'varchar',
'label' => 'Customer Bonus',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'position' =>999,
'system' => 0,
]);
$attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'custom_attribute')
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['adminhtml_customer'],//you can use other forms also ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address']
]);
$attribute->save();
}
}
thank you for your answer, unfortunately it did not help.
– newakkoff
Mar 6 '18 at 7:23
what error coming same?
– Jeeva Chezhiyan
Mar 6 '18 at 7:29
@newakkoff check this mageplaza.com/magento-2-module-development/…
– Jeeva Chezhiyan
Mar 6 '18 at 7:38
thank you, but i already tried all guides in internet)
– newakkoff
Mar 6 '18 at 7:39
@newakkoff mage2gen.com try this extension creator. Create the extension and update it. This extension will give clear definition for you..
– Jeeva Chezhiyan
Mar 6 '18 at 7:43
|
show 3 more comments
I think your missing namespace
in your setup script,
<?php
namespace KtAddbonusSetup;
use MagentoCustomerSetupCustomerSetupFactory;
use MagentoCustomerModelCustomer;
use MagentoEavModelEntityAttributeSet as AttributeSet;
use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
/**
* @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;
}
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, 'custom_attribute', [
'type' => 'varchar',
'label' => 'Customer Bonus',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'position' =>999,
'system' => 0,
]);
$attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'custom_attribute')
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['adminhtml_customer'],//you can use other forms also ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address']
]);
$attribute->save();
}
}
thank you for your answer, unfortunately it did not help.
– newakkoff
Mar 6 '18 at 7:23
what error coming same?
– Jeeva Chezhiyan
Mar 6 '18 at 7:29
@newakkoff check this mageplaza.com/magento-2-module-development/…
– Jeeva Chezhiyan
Mar 6 '18 at 7:38
thank you, but i already tried all guides in internet)
– newakkoff
Mar 6 '18 at 7:39
@newakkoff mage2gen.com try this extension creator. Create the extension and update it. This extension will give clear definition for you..
– Jeeva Chezhiyan
Mar 6 '18 at 7:43
|
show 3 more comments
I think your missing namespace
in your setup script,
<?php
namespace KtAddbonusSetup;
use MagentoCustomerSetupCustomerSetupFactory;
use MagentoCustomerModelCustomer;
use MagentoEavModelEntityAttributeSet as AttributeSet;
use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
/**
* @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;
}
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, 'custom_attribute', [
'type' => 'varchar',
'label' => 'Customer Bonus',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'position' =>999,
'system' => 0,
]);
$attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'custom_attribute')
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['adminhtml_customer'],//you can use other forms also ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address']
]);
$attribute->save();
}
}
I think your missing namespace
in your setup script,
<?php
namespace KtAddbonusSetup;
use MagentoCustomerSetupCustomerSetupFactory;
use MagentoCustomerModelCustomer;
use MagentoEavModelEntityAttributeSet as AttributeSet;
use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
/**
* @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;
}
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, 'custom_attribute', [
'type' => 'varchar',
'label' => 'Customer Bonus',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'position' =>999,
'system' => 0,
]);
$attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'custom_attribute')
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['adminhtml_customer'],//you can use other forms also ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address']
]);
$attribute->save();
}
}
edited Mar 6 '18 at 7:52
answered Mar 6 '18 at 7:04
Jeeva ChezhiyanJeeva Chezhiyan
732524
732524
thank you for your answer, unfortunately it did not help.
– newakkoff
Mar 6 '18 at 7:23
what error coming same?
– Jeeva Chezhiyan
Mar 6 '18 at 7:29
@newakkoff check this mageplaza.com/magento-2-module-development/…
– Jeeva Chezhiyan
Mar 6 '18 at 7:38
thank you, but i already tried all guides in internet)
– newakkoff
Mar 6 '18 at 7:39
@newakkoff mage2gen.com try this extension creator. Create the extension and update it. This extension will give clear definition for you..
– Jeeva Chezhiyan
Mar 6 '18 at 7:43
|
show 3 more comments
thank you for your answer, unfortunately it did not help.
– newakkoff
Mar 6 '18 at 7:23
what error coming same?
– Jeeva Chezhiyan
Mar 6 '18 at 7:29
@newakkoff check this mageplaza.com/magento-2-module-development/…
– Jeeva Chezhiyan
Mar 6 '18 at 7:38
thank you, but i already tried all guides in internet)
– newakkoff
Mar 6 '18 at 7:39
@newakkoff mage2gen.com try this extension creator. Create the extension and update it. This extension will give clear definition for you..
– Jeeva Chezhiyan
Mar 6 '18 at 7:43
thank you for your answer, unfortunately it did not help.
– newakkoff
Mar 6 '18 at 7:23
thank you for your answer, unfortunately it did not help.
– newakkoff
Mar 6 '18 at 7:23
what error coming same?
– Jeeva Chezhiyan
Mar 6 '18 at 7:29
what error coming same?
– Jeeva Chezhiyan
Mar 6 '18 at 7:29
@newakkoff check this mageplaza.com/magento-2-module-development/…
– Jeeva Chezhiyan
Mar 6 '18 at 7:38
@newakkoff check this mageplaza.com/magento-2-module-development/…
– Jeeva Chezhiyan
Mar 6 '18 at 7:38
thank you, but i already tried all guides in internet)
– newakkoff
Mar 6 '18 at 7:39
thank you, but i already tried all guides in internet)
– newakkoff
Mar 6 '18 at 7:39
@newakkoff mage2gen.com try this extension creator. Create the extension and update it. This extension will give clear definition for you..
– Jeeva Chezhiyan
Mar 6 '18 at 7:43
@newakkoff mage2gen.com try this extension creator. Create the extension and update it. This extension will give clear definition for you..
– Jeeva Chezhiyan
Mar 6 '18 at 7:43
|
show 3 more comments
You can not easily use custom customer attributes in Magento 2 community edition.
It's a paid feature in Enterprise Edition.
You can add it programmatically but in my opinion they done a lot to make it as difficult as possible.
See this thread for reference:
https://github.com/magento/magento2/issues/6575
thanks if it's a paid function, then it's better to add a column to customer_entity, right? and extend CustomerInterface, Can I do that?
– newakkoff
Mar 6 '18 at 7:37
1
However you do it, you have to extend and overwrite a lot of stuff backend and frontend wise to make it work. See the linked thread or maybe look at an extension that does it. If you are not deep in the topic and do not want to dive deep. I'd rather suggest using an extension.
– steros
Mar 6 '18 at 7:48
add a comment |
You can not easily use custom customer attributes in Magento 2 community edition.
It's a paid feature in Enterprise Edition.
You can add it programmatically but in my opinion they done a lot to make it as difficult as possible.
See this thread for reference:
https://github.com/magento/magento2/issues/6575
thanks if it's a paid function, then it's better to add a column to customer_entity, right? and extend CustomerInterface, Can I do that?
– newakkoff
Mar 6 '18 at 7:37
1
However you do it, you have to extend and overwrite a lot of stuff backend and frontend wise to make it work. See the linked thread or maybe look at an extension that does it. If you are not deep in the topic and do not want to dive deep. I'd rather suggest using an extension.
– steros
Mar 6 '18 at 7:48
add a comment |
You can not easily use custom customer attributes in Magento 2 community edition.
It's a paid feature in Enterprise Edition.
You can add it programmatically but in my opinion they done a lot to make it as difficult as possible.
See this thread for reference:
https://github.com/magento/magento2/issues/6575
You can not easily use custom customer attributes in Magento 2 community edition.
It's a paid feature in Enterprise Edition.
You can add it programmatically but in my opinion they done a lot to make it as difficult as possible.
See this thread for reference:
https://github.com/magento/magento2/issues/6575
answered Mar 6 '18 at 7:27
sterossteros
822629
822629
thanks if it's a paid function, then it's better to add a column to customer_entity, right? and extend CustomerInterface, Can I do that?
– newakkoff
Mar 6 '18 at 7:37
1
However you do it, you have to extend and overwrite a lot of stuff backend and frontend wise to make it work. See the linked thread or maybe look at an extension that does it. If you are not deep in the topic and do not want to dive deep. I'd rather suggest using an extension.
– steros
Mar 6 '18 at 7:48
add a comment |
thanks if it's a paid function, then it's better to add a column to customer_entity, right? and extend CustomerInterface, Can I do that?
– newakkoff
Mar 6 '18 at 7:37
1
However you do it, you have to extend and overwrite a lot of stuff backend and frontend wise to make it work. See the linked thread or maybe look at an extension that does it. If you are not deep in the topic and do not want to dive deep. I'd rather suggest using an extension.
– steros
Mar 6 '18 at 7:48
thanks if it's a paid function, then it's better to add a column to customer_entity, right? and extend CustomerInterface, Can I do that?
– newakkoff
Mar 6 '18 at 7:37
thanks if it's a paid function, then it's better to add a column to customer_entity, right? and extend CustomerInterface, Can I do that?
– newakkoff
Mar 6 '18 at 7:37
1
1
However you do it, you have to extend and overwrite a lot of stuff backend and frontend wise to make it work. See the linked thread or maybe look at an extension that does it. If you are not deep in the topic and do not want to dive deep. I'd rather suggest using an extension.
– steros
Mar 6 '18 at 7:48
However you do it, you have to extend and overwrite a lot of stuff backend and frontend wise to make it work. See the linked thread or maybe look at an extension that does it. If you are not deep in the topic and do not want to dive deep. I'd rather suggest using an extension.
– steros
Mar 6 '18 at 7:48
add a comment |
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f216104%2fmagento-2-2-customer-custom-attribute%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown