Magento2移行されたカスタム顧客属性が値を保存していません

Aug 21 2020

Magentoデータをバージョン1.9.0.1からバージョン2.3.5-p1に移行しました。1.9.0.1のセットアップでは、mobile_noと呼ばれるカスタムの顧客属性が作成されました。M2セットアップで自動的に作成されたデータと属性。mobile_noデータは、M2管理者にも表示されます。しかし、番号を編集して保存しようとすると、更新された値を取得せずに古い値が保存されます。誰かがこれで私を助けることができますか?フロントエンド登録フォームでこの属性を呼び出す必要があります。

回答

2 PratsMagento Aug 27 2020 at 08:04

複数のことをチェックしてデータベースを掘り下げた後、最終的に問題を見つけたので、答えを共有しました。問題は、移行後eav_entity_attributeに、それぞれの属性のテーブルにエントリがないことでした。以下のデータパッチを使用して属性を更新し、問題を修正しました。これで、値を保存できます。

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