L'attributo cliente personalizzato Magento2 migrato non salva il valore

Aug 21 2020

Ho migrato i dati di Magento dalla versione 1.9.0.1 alla versione 2.3.5-p1. Nella configurazione 1.9.0.1 è stato creato un attributo cliente personalizzato chiamato mobile_no. I dati e l'attributo creati automaticamente nella mia configurazione M2. I dati mobile_no sono visibili anche nell'amministratore di M2. Ma quando provo a modificare il numero e a salvarlo, salva il vecchio valore, non prende quello aggiornato. Qualcuno mi può aiutare con questo? Devo chiamare questo attributo nel modulo di registrazione del frontend.

Risposte

2 PratsMagento Aug 27 2020 at 08:04

Dopo aver controllato più cose e scavato nel database, finalmente ho trovato il problema, condividendo quindi la risposta. Il problema era che dopo la migrazione mancava la voce nella eav_entity_attributetabella per il rispettivo attributo. Aggiornato l'attributo utilizzando la patch di dati di seguito e risolto il problema. Ora sono in grado di salvare il valore.

<?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 [];
    }
}