0

I have an issue I can't find why. I tried the issue on Magento 2.3.1 and 2.3.2.

I have created a module to add a new customer attribute. No issue for the moment. When I try to save the customer from the admin the value is not saved even when the field is in the admin form. Can you find my mistake?

enter image description here

namespace Vendor\Module\Setup;

use Magento\Customer\Model\Customer;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;

class UpgradeData implements \Magento\Framework\Setup\UpgradeDataInterface
{
    private $customerSetupFactory;
    protected $eavConfig;
    private $attributeSetFactory;

    public function __construct(
        CustomerSetupFactory $customerSetupFactory,
        \Magento\Eav\Model\Config $eavConfig,
        AttributeSetFactory $attributeSetFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
        $this->eavConfig = $eavConfig;
        $this->attributeSetFactory = $attributeSetFactory;

    }

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

        if (version_compare($context->getVersion(), '1.0.1') < 0) {
            $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
            $customerSetup->addAttribute(
                Customer::ENTITY,
                'exported_to_erp_at',
                [
                    'type' => 'varchar',
                    'label' => 'Exported to erp at',
                    'input' => 'text',
                    'required' => false,
                    'sort_order' => 88,
                    'visible' => true,
                    'system' => false,
                    'user_defined' => false,
                ]
            );

            $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
            $attributeSetId = $customerEntity->getDefaultAttributeSetId();

            /** @var $attributeSet AttributeSet */
            $attributeSet = $this->attributeSetFactory->create();
            $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

            $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'exported_to_erp_at')
                ->addData([
                    'attribute_set_id' => $attributeSetId,
                    'attribute_group_id' => $attributeGroupId,
                    'used_in_forms' => ['adminhtml_customer'],
                ]);

            $attribute->save();

        }
    }
}

As I said I tried to save with no success from the admin so I tried in Code. No success either.

$customer = $this->_customerFactory->create()->load(46431);
        $customer->setData("exported_to_erp_at", "toto");
        $customer->setCustomAttribute("exported_to_erp_at", "toto");
        $customer->save();

    //should work
    $customer = $this->_customerRepository->getById(46431);
    $customer->setCustomAttribute("exported_to_erp_at", "toto");
    $this->_customerRepository->save($customer);

Do you have an idea?

EDIT

If this help I have not a clean installation. This is a migration with the magento migration tool from 1.7.x to 2.3.x

  • Any exception you are getting? – jafar pinjar Aug 26 at 7:27
  • Nothing, not in system.log nor in exception.log. I have the same issue with 4 others attributes created by the migration tool – Taschert Aug 26 at 7:37
  • Can you try my answer once? – jafar pinjar Aug 26 at 7:38
  • No change for the save. But the attribute disapeared in the customer admin form. – Taschert Aug 26 at 7:47
  • Give a try with vanila instance, For now put aside migrated. Your code is correct no issue once confirm with vanila for sure :) – anonymous Aug 30 at 7:29
1

So I found the issue with the custom customer attribute.

My process was :

  1. Create all my attributes with install/updrage scripts in my magento mpdules
  2. Use the Magento migration tool to migrate from Magento 1 & 2
  3. Test my code

The issue is that after the migration my attributes lost the used_in_forms value, were not assigned to any website.

I had to add a step in my migration to reset my customer attributes.

$attribute = $this->_eavConfig->getAttribute('customer', $attributeCode);
        $attribute->setData('used_in_forms', ['adminhtml_customer']);
        $attribute->setData('is_user_defined', false);
        $attribute->setData('is_system', false);
        $attribute->save();

This solved the problème for all the attributes needed.

Thx for your help.

  • Good point to share. +1 for answer – Sumit Aug 30 at 15:10
0

Please change user_defined to true in UpgradeData.php as like below

$customerSetup->addAttribute(
            Customer::ENTITY,
            'exported_to_erp_at',
            [
                'type' => 'varchar',
                'label' => 'Exported to erp at',
                'input' => 'text',
                'required' => false,
                'sort_order' => 88,
                'visible' => true,
                'system' => false,
                'user_defined' => true,
            ]
        );
  • It does not work for me, the only change is that the attribute is not displayed in the customer form in the admin anymore. – Taschert Aug 26 at 7:46
  • @Taschert, It should work, have you checked in the table, is_user_defined is set? – jafar pinjar Aug 26 at 7:51
  • Yes, I have modified the value in "eav_attribute" – Taschert Aug 26 at 7:56
  • Did you modify now? or it was set once u run the upgradeData? – jafar pinjar Aug 26 at 7:57
  • After the updrade date it was set to "0". I changed the value to 1 in the DB, emptied the cache, no change. – Taschert Aug 26 at 8:00
0

I did a script about Customer attribute in the past. My script worked perfectly so I put it on my gist. Take a look, maybe it will help you.

https://gist.github.com/0-Sony/252311391be9e434a53e522709cc11be

Hope it helps

0

I have created a script and able to get value after save here is my script hope this helps

   <?php
        use Magento\Framework\App\Bootstrap;

        require '../../app/bootstrap.php';
        $bootstrap = Bootstrap::create(BP, $_SERVER);
        $obj = $bootstrap->getObjectManager();
        $state = $obj->get('Magento\Framework\App\State');
        $state->setAreaCode('frontend');

        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();

        error_reporting(E_ALL);
        ini_set('display_errors', 1);

        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $eavSetup = $objectManager->create('Magento\Eav\Setup\EavSetupFactory');
        $setup = $objectManager->create('Magento\Framework\Setup\ModuleDataSetupInterface');
        $config = $objectManager->create('Magento\Eav\Model\Config');

        $setup->startSetup();

        $eavSetup = $eavSetup->create(['setup' => $setup]);

        $eavSetup->addAttribute('customer_address', 'start_time', [
            'type' => 'varchar',
            'input' => 'text',
            'label' => 'Start Time',
            'visible' => true,
            'required' => false,
            'user_defined' => true,
            'system'=> false,
            'group'=> 'General',
            'position' => 160,
            'global' => true,
            'visible_on_front' => true,
        ]);
        $eavSetup->addAttribute('customer_address', 'end_time', [
            'type' => 'varchar',
            'input' => 'text',
            'label' => 'End Time',
            'visible' => true,
            'required' => false,
            'user_defined' => true,
            'system'=> false,
            'position' => 160,
            'group'=> 'General',
            'global' => true,
            'visible_on_front' => true,
        ]);
        $eavSetup->addAttribute('customer_address', 'tail_lift', [
            'type' => 'int',
            'label' => 'Tail-Lift',
            'input' => 'select',
            'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Table',
            'visible' => true,
            'required' => false,
            'user_defined' => true,
            'system'=> false,
            'position' => 160,
            'group'=> 'General',
            'global' => true,
            'visible_on_front' => true,
            'option' =>
                array (
                    'values' =>
                        array (
                            0 => 'Yes',
                            1 => 'No',
                        ),
                ),
        ]);

        $customAttribute =$config->getAttribute('customer_address', 'start_time');
        $customAttribute->setData(
            'used_in_forms',
            ['adminhtml_customer_address','customer_address_edit','customer_register_address'] //list of forms where you want to display the custom attribute
        );
        $customAttribute->save();

        $customAttribute =$config->getAttribute('customer_address', 'end_time');
        $customAttribute->setData(
            'used_in_forms',
            ['adminhtml_customer_address','customer_address_edit','customer_register_address'] //list of forms where you want to display the custom attribute
        );
        $customAttribute->save();

        $customAttribute =$config->getAttribute('customer_address', 'tail_lift');
        $customAttribute->setData(
            'used_in_forms',
            ['adminhtml_customer_address','customer_address_edit','customer_register_address'] //list of forms where you want to display the custom attribute
        );
        $customAttribute->save();

//$eavSetup->removeAttribute('customer_address', 'tail_lift');

        $setup->endSetup();
echo "attribute adasddas";

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.