0

We are facing an issue while updating customer information from Customer My account section on frontend that is Customer Custom attributes gets deleted upon update or save customer information.

For this I have created a Plugin as per this link - https://github.com/magento/magento2/issues/6411

After code compile I am facing below error -

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

Below is my code -

Vendor/Customization/Plugin/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;
    }
}

Vendor/Customization/etc/di.xml

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

Please provide some suggestion.

Thanks in advance.

  • have you tried deleting generated folder? – Priyank Dec 24 '18 at 9:09
  • @Priyank Yes I deleted generation directory cleared cached and code compile as well. Tried everything but still not working – Rahul Dec 24 '18 at 9:27
  • @Priyank any suggestions please. – Rahul Dec 26 '18 at 4:58
  • @web-developer any suggestions please – Rahul Dec 26 '18 at 4:59
  • @Rahul. You can use this marketplace.magento.com/clarion-customer-attribute.html. If you are free to allow 3rd party extension. – Vivek Dec 26 '18 at 5:16

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.