1

Create a custom customer attribute and this attribute is showing at backend.

but the issue is that this attribute does not save value.

It has backed model for validation.

Installer

<?php
/* 
 * @category   Devamitbera
 * @package    DevamitBera_AdvanceCustomer
 * @author     Amit Bera <dev.amitbera@gmail.com>
 * @website    http://www.amitbera.com
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */

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

class InstallData implements InstallDataInterface{

    /**
     * Customer setup factory
     *
     * @var CustomerSetupFactory
     */
    protected $customerSetupFactory;

    public function __construct(CustomerSetupFactory $customerSetupFactory
          ) {
            $this->customerSetupFactory = $customerSetupFactory;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {

        /* @var customer Magento\Customer\Setup\CustomerSetupFactory */
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
        $setup->startSetup();
        $customerSetup->addAttribute(
                Customer::ENTITY,
                'group_code',
                [
                    'type' =>'static',
                    'label' => 'Group code',
                    'input' => 'text',
                    'required' => false,
                    'sort_order' => 100,
                    'visiable' => true,
                    'position' => 100,
                    'system' =>  false,
                    'is_used_in_grid' => true,
                    'is_visible_in_grid' => true,
                    'is_filterable_in_grid' => true,
                    'is_searchable_in_grid' => true,
                    'backend' => 'DevamitBera\AdvanceCustomer\Model\Attribute\Backend\Groupcode'
                ]
                );

        /* Assign the attribute into form */
        $group_codeAttribute = $customerSetup->getEavConfig()
                                ->getAttribute(Customer::ENTITY, 'group_code');
        $usedInForms = ['customer_account_create', 'checkout_register',
                        'adminhtml_customer'];
        $group_codeAttribute->setData('used_in_forms',$usedInForms);
        $group_codeAttribute->save();
        /* Assign Group id  to Form */
         $group_IdAttribute = $customerSetup->getEavConfig()
                                ->getAttribute(Customer::ENTITY, 'group_id');
        $usedInForms = ['customer_account_create', 'customer_account_edit',
                        'adminhtml_customer'];
        $group_IdAttribute->setData('used_in_forms',$usedInForms);
        $group_IdAttribute->save();       


        $setup->endSetup();
    }
}

Backend model:

<?php
/* 
 * @category   Devamitbera
 * @package    DevamitBera_AdvanceCustomer
 * @author     Amit Bera <dev.amitbera@gmail.com>
 * @website    http://www.amitbera.com
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
namespace DevamitBera\AdvanceCustomer\Model\Attribute\Backend;
class Groupcode extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend{

    /**
     * @var \DevamitBera\AdvanceCustomer\Helper\Config
     */
    protected $_groupSwitcherHelperConfig;

    /**
     * @var \DevamitBera\AdvanceCustomer\Helper\Groupcodes
     */
    protected $_groupCodesHelper;

    /**
     * Magento string lib
     *
     * @var \Magento\Framework\Stdlib\StringUtils
     */
    protected $string;

    /**
     * @param \Magento\Framework\Stdlib\StringUtils $string
     */
    public function __construct(\Magento\Framework\Stdlib\StringUtils $string,
     \DevamitBera\AdvanceCustomer\Helper\Groupcodes $_groupCodesHelper,
     \DevamitBera\AdvanceCustomer\Helper\Config $_groupSwitcherHelperConfig     
      )
    {
        $this->string = $string;
        $this->_groupCodesHelper = $_groupCodesHelper;
        $this->_groupSwitcherHelperConfig = $_groupSwitcherHelperConfig;
    }   
    /**
     * Validate Group code
     *
     * @param Customer $object
     * @return bool
     * @throws \Magento\Framework\Exception\LocalizedException
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    public function validate($object)
    {
        return parent::validate($object);
        }
        return true;
    }
}
  • Did you try to reindex database? – Khoa TruongDinh Feb 17 '17 at 5:48
  • y ..i have done...php bin/magento indexer:reindex – Amit Bera Feb 17 '17 at 5:50
  • no result........ – Amit Bera Feb 17 '17 at 6:02
  • $model->save(); $model->load($model->getId())->setData('<attribute>',<value>)->save(); – Ankit Shah Feb 17 '17 at 6:54
  • This is not the solution – Amit Bera Feb 17 '17 at 6:55

Your Answer

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

Browse other questions tagged or ask your own question.