1

I created new custom customer attribute, it is added to admin edit customer page but its value cannot be saved.

In /Vendor/Module/Setup/CustomerSetup.php

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,
        'invoice_email',
        [
        'label' => 'invoice email',
        'system' => 0,
        'position' => 100,
        'sort_order' =>100,
        'visible' =>  true,
        'note' => '',
        'type' => 'varchar',
        'input' => 'text',

        ]
        );

    $customerSetup -> getEavConfig() -> getAttribute('customer', 'invoice_email')->setData('is_user_defined',1)->setData('is_required',0)->setData('default_value','')->setData('used_in_forms', ['customer_account_edit']) -> save();


} 

public function installCustomerAddressAttributes($customerSetup) {

} 

public function getEavConfig() {
    return $this -> eavConfig;
} 
} 

And in Vendor/Module/Setup/InstallData.php

namespace Wetag\InvoiceEmail\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('Wetag\InvoiceEmail\Setup\CustomerSetup');
            $customerSetup->installAttributes($customerSetup);



    }

}
}

How can i edit the attribute so it can be saved in front end and baackend (i tried to create an observer but this return an infinite page loading when saving the customer: infinite loop)

2 Answers 2

1

You define what forms appear in as part of setup. Have you missed this part?

https://github.com/DominicWatts/EmailCC/blob/master/Setup/InstallData.php

<?php

namespace Xigen\CC\Setup;

use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

/**
 * Install email_cc customer attribute
 */
class InstallData implements InstallDataInterface
{
    /**
     * @var CustomerSetupFactory
     */
    private $customerSetupFactory;

    /**
     * Constructor
     * @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
     */
    public function __construct(
        CustomerSetupFactory $customerSetupFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function install(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        $customerSetup->addAttribute(
            \Magento\Customer\Model\Customer::ENTITY,
            'email_cc', [
                'type' => 'varchar',
                'label' => 'Email CC',
                'input' => 'text',
                'source' => '',
                'required' => false,
                'visible' => true,
                'position' => 500,
                'system' => false,
                'backend' => '',
                'user_defined' => true,
                'note' => __("Comma separated"),
            ]
        );

        $attribute = $customerSetup->getEavConfig()->getAttribute('customer', 'email_cc')
            ->addData(
                [
                    'used_in_forms' => [
                        'adminhtml_customer',
                        'adminhtml_checkout',
                        'customer_account_create',
                        'customer_account_edit',
                    ],
                ]
            );

        $attribute->save();
    }
}
2
  • I missed which part? Sep 16, 2019 at 20:20
  • @tarekfellah used in forms part. have you got all the forms? Sep 17, 2019 at 10:19
0

I solved the issue, i changed the file Vendor\Module\Setup\UpgradeData.php and execute

php bin/magento setup:upgrade 

Vendor\Module\Setup\UpgradeData.php

namespace Vendor\Module\Setup;

use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetup;
use \Magento\Framework\Setup\UpgradeDataInterface;
use \Magento\Framework\Setup\ModuleContextInterface;
use \Magento\Framework\Setup\ModuleDataSetupInterface;

use Magento\Customer\Api\CustomerMetadataInterface;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;



class UpgradeData implements UpgradeDataInterface
{
/**
 * Customer setup factory
 *
 * @var \Magento\Customer\Setup\CustomerSetupFactory
 */
private $customerSetupFactory;

/**
 * Init
 *
 * @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
 */
public function __construct(\Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory)
{
    $this->customerSetupFactory = $customerSetupFactory;
}

/**
 * Upgrade data for a module
 *
 * @param ModuleDataSetupInterface $setup
 * @param ModuleContextInterface $context
 * @return void
 */
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{


    if (version_compare($context->getVersion(), '0.0.3', '<')) {

        $this->createMobileAttribute($setup);
    }
}


public function createMobileAttribute($setup)
{
    /** @var CustomerSetup $customerSetup */
    $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

    // Add new customer attribute
    $customerSetup->addAttribute(
        Customer::ENTITY,
        'invoice_email_1',
        [

            'label'                 => 'Invoice Email ',
            'input'                 => 'text',
            'required'              => false,
            'sort_order'            => 1000,
            'position'              => 1000,
            'visible'               => true,
            'system'                => false,
            'is_used_in_grid'       => true,
            'is_visible_in_grid'    => false,
            'is_filterable_in_grid' => false,
            'is_searchable_in_grid' => false,
            'default'               => '0'
        ]
    );

    // add attribute to form
    /** @var  $attribute */
    $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'invoice_email_1');
    $attribute->setData('used_in_forms', ['adminhtml_customer', 'customer_account_create', 'customer_account_edit']);
    $attribute->save();

    $setup->endSetup();
}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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