4

[magento2]I've added the custom attribute to customer_address_entity and display in UI form. how to save the custom attribute to database when submit form enter image description here

  • What code did you use to create and display the custom attributes? – Ryan Hoerr Sep 30 '15 at 12:13
1

Add following code in your Setup/UpgradeData.php if you don't have this file then create one.

<?php
namespace Namespace\Modulename\Setup;


use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetup;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Eav\Setup\EavSetup;
class UpgradeData implements UpgradeDataInterface
{
    private $eavSetupFactory;
    private $customerSetupFactory;
    public function __construct(CustomerSetupFactory $customerSetupFactory)
    {
        $this->customerSetupFactory = $customerSetupFactory;
    }
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        $customerSetup->addAttribute(
                Customer::ENTITY,
                'custom_attribute',
                [
                    'type' => 'text',
                    'label' => 'Customer Product Points',
                    'input' => 'text',
                    'required' => false,
                    'visible' => true,
                    'system' => 0,
                ]
            );
        $attribute = $customerSetup->getEavConfig()->getAttribute('customer', 'custom_attribute');
        $attribute->setData('used_in_forms', ['adminhtml_customer']);
        $attribute->save();
        $setup->endSetup();
    }
}

Try this code and value will be saved automatically.

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.