1

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 Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Customer\Model\Customer;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\Entity\Attribute\SetFactory 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.

2

Please confirm that have you created registration.php on your module root folder like :

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
        \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Kt_Addbonus', __DIR__
);

Also replace following code into your InstallData.php

<?php

namespace Kt\Addbonus\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{

    private $customerSetupFactory;
    private $attributeSetFactory;

    public function __construct(\Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory, \Magento\Eav\Model\Entity\Attribute\SetFactory $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(\Magento\Customer\Model\Customer::ENTITY, "customer_bonus");

        $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
        $attributeSetId = $customerEntity->getDefaultAttributeSetId();
        $attributeSet = $this->attributeSetFactory->create();
        $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);


        $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'customer_bonus', [
            'type' => 'varchar',
            'label' => 'Customer Bonus',
            'input' => 'text',
            'required' => false,
            'visible' => true,
            'user_defined' => true,
            'system' => false,
        ]);

        $attribute = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::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
  • @HardikPatel, how to add two attributes,? – jafar pinjar Mar 7 at 6:58
1

I think your missing namespace in your setup script,

<?php
namespace Kt\Addbonus\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;

/**
 * @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();
    }
}
0

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
0

I created a module that allow to add customer/address attribute from backend here

Maybe someone else will need it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.