Frage

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

War es hilfreich?

Lösung

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.

Andere Tipps

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,
            ]
        );

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

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";
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit magento.stackexchange
scroll top