Magento 2: Сохранение значения пользовательского атрибута клиента программно
Как я могу сохранить пользовательское Значение атрибута клиента программно? Я пробовал код ниже, но это не сработало.
protected $customer;
public function __construct(
\Magento\Customer\Model\Customer $customer
)
{
$this->customer = $customer;
}
...
...
$customerId = "1";
$customer = $this->customer->load($customerId);
$data = "customer attribute value";
$customerData = $customer->getDataModel();
$customerData->setCustomAttribute('customer_attribute_code',$data);
$customer->updateData($customerData);
$customer->save();
Существует ли какой-либо другой способ сохранения атрибута клиента?
6
Author: Dinesh Yadav, 2017-11-07
4 answers
У меня есть решение
protected $customer;
protected $customerFactory;
public function __construct(
\Magento\Customer\Model\Customer $customer,
\Magento\Customer\Model\ResourceModel\CustomerFactory $customerFactory
)
{
$this->customer = $customer;
$this->customerFactory = $customerFactory;
}
...
...
$customerId = "1";
$customer = $this->customer->load($customerId);
$data = "customer attribute value";
$customerData = $customer->getDataModel();
$customerData->setCustomAttribute('customer_attribute_code',$data);
$customer->updateData($customerData);
$customerResource = $this->customerFactory->create();
$customerResource->saveAttribute($customer, 'customer_attribute_code');
11
Author: Dinesh Yadav, 2021-01-15 12:46:08
Вам нужно сделать это странным способом Magento 2:-
public function __construct( \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository, ) { $this->_customerRepository = $customerRepository; } public function execute() { $customer = $this->_customerRepository->getById($customerId); $customer->setDob($data['dob']) ->setCustomAttribute('medicare_number',$data['medicare_number']) ->setCustomAttribute('medicare_reference',$data['medicare_reference']) ->setCustomAttribute('medicare_exp',$data['medicare_exp']); $this->_customerRepository->save($customer); }
5
Author: Dallas Clarke, 2019-01-04 06:44:14
Иногда может потребоваться сохранить несколько атрибутов одновременно. После прохождения кода Magento я получаю этот код. Здесь medicare_number и medicare_reference являются пользовательскими атрибутами.
$customerModel = $this->_customerFactory->create();
$customerModel->getResource()->load($customerModel, $customerId);
$customerModel->setData('dob', $this->getRequest()->getParam('dob'))
->setData('gender', $this->getRequest()->getParam('gender'))
->setData('medicare_number', $this->getRequest()->getParam('medicare_number'))
->setData('medicare_reference', $this->getRequest()->getParam('medicare_reference'))
->setAttributeSetId(\Magento\Customer\Api\CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER);
$customerModel->getResource()->save($customerModel);
1
Author: user1554046, 2018-08-24 04:48:27
protected $customer;
protected $customerFactory;
public function __construct(
\Magento\Customer\Model\Customer $customer
\Magento\Customer\Model\ResourceModel\CustomerFactory $customerFactory
)
{
$this->customer = $customer;
$this->customerFactory = $customerFactory;
}
...
...
$mobile="1234567890";
$custom = $this->_customerFactory ->create();
$custom = $custom->setWebsiteId($helperData->getWebsiteId());
$custom = $custom->loadByEmail("[email protected]");
$customerData = $custom->getDataModel();
$customerData->setCustomAttribute('custom_attribute code', $mobile);
$custom->updateData($customerData);
$custom->save();
0
Author: Sanjay Chauhan, 2018-06-11 11:26:38