从前端的“客户我的帐户”部分更新客户信息时遇到问题,即“客户自定义”属性在更新或保存客户信息时被删除。
为此,我根据此链接创建了一个插件-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>
请提供一些建议。
先谢谢了。