0

I have migrated Magento data from version 1.9.0.1 to version 2.3.5-p1. In 1.9.0.1 setup there was a custom customer attribute created called mobile_no. The data and the attribute automatically created in my M2 setup. The mobile_no data is also visible in the M2 admin. But when I try to edit the number and save it, It saves the old value its not taking the updated one. Can anyone help me with this? I have to call this attribute in the frontend registration form.

1
  • Thank you. This look like the answer for my issue. Where, I'm sorry that I'm new to magento 2 development. Can you please let me know where do i place this data patch file, and how to run it? Commented Sep 15, 2023 at 23:54

1 Answer 1

3

After checking multiple things and digging down the database finally I found the issue, hence sharing the answer. The issue was after the migration the entry was missing in the eav_entity_attribute table for the respective attribute. Updated the attribute using the below Data patch and fixed the issue. Now I am able to save the value.

<?php declare(strict_types=1);

/**
 * Patch for the Customer Attribute
 *
 * Patch Class to update the migrated field mobile_no in the `eav_entity_attribute` table. 
 */

namespace VendorName\ModuleName\Setup\Patch\Data;

use Zend_Validate_Exception;
use Magento\Eav\Model\Config;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Eav\Model\Entity\Attribute\Source\Boolean;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;

/**
 * Patch Class to update the mobile_no in the `eav_entity_attribute` table.
 */
class UpdateEavMobileNoAttribute implements DataPatchInterface
{
    /**
     * @var Config
     */
    private $eavConfig;

    /**
     * @var EavSetupFactory
     */
    private $eavSetupFactory;

    /**
     * @var AttributeSetFactory
     */
    private $attributeSetFactory;

    /**
     * AddressAttribute constructor.
     *
     * @param Config              $eavConfig
     * @param EavSetupFactory     $eavSetupFactory
     * @param AttributeSetFactory $attributeSetFactory
     */
    public function __construct(
        Config $eavConfig,
        EavSetupFactory $eavSetupFactory,
        AttributeSetFactory $attributeSetFactory
    ) {
        $this->eavConfig = $eavConfig;
        $this->eavSetupFactory = $eavSetupFactory;
        $this->attributeSetFactory = $attributeSetFactory;
    }

    /**
     * {@inheritdoc}
     */
    public static function getDependencies(): array
    {
        return [];
    }

    /**
     * Create strategic account customer attribute
     * @return void
     * @throws LocalizedException
     * @throws Zend_Validate_Exception
     */
    public function apply(): void
    {
        $eavSetup = $this->eavSetupFactory->create();

        $customerEntity = $this->eavConfig->getEntityType('customer');
        $attributeSetId = $customerEntity->getDefaultAttributeSetId();

        $attributeSet = $this->attributeSetFactory->create();
        $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

        $customAttribute = $this->eavConfig->getAttribute('customer', 'mobile_no');

        $customAttribute->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' => ['adminhtml_customer', 'customer_account_edit']
        ]);
        $customAttribute->save();

    }

    /**
     * {@inheritdoc}
     */
    public function getAliases(): array
    {
        return [];
    }
}
2
  • 1
    Superb!! kudos! Commented Aug 27, 2020 at 8:11
  • Worked perfectly to solve my problem, thanks. Just one thing I noticed: I don't think you're actually using the EavSetupFactory you're loading here!
    – Louis B.
    Commented Sep 16, 2021 at 11:47

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.