1

I am using Magento 2.1.0, and i created two customer attribute, it succefully created in backend, and i am that in database, customer grid and customer information page.

but when i enter any value in that, it is not save.

<?php

 namespace xxxxxx\CustomerAttr\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;
 use Magento\Eav\Model\Config;



 /**
     * @codeCoverageIgnore
     */
       class InstallData implements InstallDataInterface
         {
/**
 * Customer setup factory
 *
 * @var CustomerSetupFactory
 */
private $eavSetupFactory;

/**
 * Init
 *
 * @param CustomerSetupFactory $customerSetupFactory
 */
           public function __construct(EavSetupFactory $eavSetupFactory, Config $eavConfig)
{
    $this->eavSetupFactory = $eavSetupFactory;
    $this->eavConfig = $eavConfig;
}


public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
    $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
    $eavSetup->addAttribute(
        \Magento\Customer\Model\Customer::ENTITY,
        'city',
        [
            'type' => 'varchar',
            'label' => 'city',
            'input' => 'text',
            'required' => false,
            'sort_order' => 100,
            'system' => false,
            'position' => 100,
            'is_used_in_grid' => true,
            'is_visible_in_grid' => true,
            'is_filterable_in_grid' => true,
            'is_searchable_in_grid' => true,
        ]
    );
    $eavSetup->addAttribute(
        \Magento\Customer\Model\Customer::ENTITY,
        'area',
        [
            'type' => 'varchar',
            'label' => 'area',
            'input' => 'text',
            'required' => false,
            'sort_order' => 100,
            'system' => false,
            'position' => 100,
            'is_used_in_grid' => true,
            'is_visible_in_grid' => true,
            'is_filterable_in_grid' => true,
            'is_searchable_in_grid' => true,
        ]
    );
    $sampleAttribute = $this->eavConfig->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'city');
    $sampleAttribute->setData(
        'used_in_forms',
        ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address' ,'customer_account_create']
    );
    $sampleAttribute->save();
    $sampleAttribute = $this->eavConfig->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'area');
    $sampleAttribute->setData(
        'used_in_forms',
        ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address', 'customer_account_create']
    );
    $sampleAttribute->save();

}
}

even, i have two field on registration page, and i want this attribute take data from field, how to make relation between this???

1 Answer 1

3

To Save your attribute value in backend , You also need to pass Parameter backend with its value and also there are few modifications needed in your parameters Which i have done below .

So try below code :

 $eavSetup->addAttribute(
        \Magento\Customer\Model\Customer::ENTITY,
        'area',
        [
            'type' => 'varchar',
            'label' => 'area',
            'input' => 'text',
            'class' => '',
            'source' => '',
            'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
            'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_GLOBAL,
            'required' => false,
            'sort_order' => 100,
            'system' => false,
            'visible' => true,
            'required' => false,
            'user_defined' => false,
            'default' => 0,
            'searchable' => true,
            'filterable' => true,
            'comparable' => true,
            'visible_on_front' => true,
            'used_in_product_listing' => true,
            'unique' => false,
            'apply_to' => ''
        ]
    );
5
  • What is for Apply to is used???
    – sam
    Commented Jan 18, 2017 at 11:12
  • thank you, one more thing only, which class and source i have to mention here?
    – sam
    Commented Jan 18, 2017 at 11:31
  • I applied this, but now also value is not saved .
    – sam
    Commented Jan 18, 2017 at 11:37
  • yes , now it is working, actually, i tries two attribute in on file, so its creating problem. but now the attribute is not visible on frontend. may be issue with additional info block, because due to required field, form is not submitted and showing error. so can you please tell me how to enable additional info block???
    – sam
    Commented Jan 18, 2017 at 14:06
  • @Mathan: i tried, but i don't have sufficient reputation to mark as answer.
    – sam
    Commented Jan 19, 2017 at 4:49

Your Answer

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

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