Пользовательский атрибут клиента удаляется после обновления в Magento 2 - PullRequest
0 голосов
/ 24 декабря 2018

Мы столкнулись с проблемой при обновлении информации о клиенте из раздела «Моя учетная запись клиента» на веб-интерфейсе, в котором пользовательские атрибуты клиента удаляются при обновлении или сохранении информации о клиенте.

Для этого я создал плагин по этой ссылке - https://github.com/magento/magento2/issues/6411

После компиляции кода я сталкиваюсь с ошибкой ниже -

Fatal error: Uncaught TypeError: Argument 1 passed to Vendor\Customization\Plugin\saveCustomCustomerAttributes::__construct() must be an instance of Magento\Customer\Model\AttributeMetadataConverter, instance of Magento\Framework\ObjectManager\ObjectManager given, called in /htdocs/stage6/html/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php on line 93 and defined in /htdocs/stage6/html/app/code/Vendor/Customization/Plugin/SaveCustomCustomerAttributes.php:35 Stack trace: #0 /htdocs/stage6/html/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(93): Vendor\Customization\Plugin\saveCustomCustomerAttributes->__construct(Object(Magento\Framework\ObjectManager\ObjectManager)) #1 /htdocs/stage6/html/vendor/magento/framework/ObjectManager/Factory/Compiled.php(88): Magento\Framework\ObjectManager\Factory\AbstractFactory->createObject('Vendor\\Cust...', Array) #2 /htdocs/stage6/html/vendor/magento/framework/ObjectManager/ObjectManager.php(71): Magento\Framework\ObjectManager\Factor in /htdocs/stage6/html/app/code/Vendor/Customization/Plugin/SaveCustomCustomerAttributes.php on line 35

Ниже мой код -

Поставщик / Настройка / Плагин / SaveCustomCustomerAttributes.php

<?php

namespace Vendor\Customization\Plugin;

/*use Magento\Customer\Api\AddressMetadataInterface;
use Magento\Customer\Model\AttributeMetadataConverter;
use Magento\Customer\Model\AttributeMetadataDataProvider;*/

use Magento\Customer\Api\CustomerMetadataInterface;
use Magento\Customer\Model\AttributeMetadataConverter;
use Magento\Customer\Model\AttributeMetadataDataProvider;

/**
 * Class SaveCustomAddressAttributes
 * @package Vendor\Customization\Plugin
 * @author Rahul 
 */
class saveCustomCustomerAttributes
{
    //private $customerDataObjectMethods;
    /** @var AttributeMetadataConverter  */
    protected $attributeMetadataConverter;
    /** @var AttributeMetadataDataProvider  */
    protected $attributeMetadataDataProvider;
    /** @var array  */
    protected $additionalAddressAttributes = array(
        'preferredaccountnumber'
    );

    /**
     * SaveCustomAddressAttributes constructor.
     * @param AttributeMetadataConverter $attributeMetadataConverter
     * @param AttributeMetadataDataProvider $attributeMetadataDataProvider
     */
    public function __construct(
        AttributeMetadataConverter $attributeMetadataConverter,
        AttributeMetadataDataProvider $attributeMetadataDataProvider
    ) {
        $this->attributeMetadataConverter = $attributeMetadataConverter;
        $this->attributeMetadataDataProvider = $attributeMetadataDataProvider;
    }

    /**
     * Work around to ensure that custom address attribute get saved
     * @param \Magento\Customer\Model\Metadata\CustomerMetadata $subject
     * @param array $attributes
     * @return array
     */
    public function afterGetAttributes(\Magento\Customer\Model\Metadata\CustomerMetadata $subject, $attributes)
    {
        // Loop through our custom address attributes
        foreach ($this->additionalAddressAttributes as $additionalAddressAttribute) {
            // Only add attribute if ti hasn't already been added
            if (!isset($attributes[$additionalAddressAttribute])) {
                // Create an instance of our attribute
                try {
                    $attribute = $this->attributeMetadataDataProvider
                        ->getAttribute(
                            CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
                            $additionalAddressAttribute
                        );
                } catch (\Magento\Framework\Exception\LocalizedException $e) {
                    // If for whatever reason our attribute doesn't exist then return the original array
                    return $attributes;
                }

                if ($attribute) {
                    // If we have retrieved an attribute then get the meta and add it to the attributes array
                    $convertedAttribute = $this->attributeMetadataConverter->createMetadataAttribute($attribute);
                    if ($convertedAttribute) {
                        $attributes[$additionalAddressAttribute] = $convertedAttribute;
                    }
                }

            }
        }
        return $attributes;
    }
}

Поставщик / Настройка / etc / di.xml

<type name="\Magento\Customer\Model\Metadata\CustomerMetadata">
<plugin name="saveCustomCustomerAttributes" type="\Vendor\Customization\Plugin\SaveCustomCustomerAttributes" sortOrder="0" disabled="false"/>
</type>

Пожалуйста, предоставьте некоторые предложения.

Заранее спасибо.

...