0

I am using Magento 2 Cloud. I have created a Customer Address Attribute customer_shipping_date using Admin Menu.

This attribute is not visible on frontend.

In sales_order_save_after observer I try to set/update it's value programmitically but when I edit order address in admin, it shows blank.

How can I set the value after order has been created.

Thanks

0

1 Answer 1

0

I have struggled with this as well, because it is a custom attribute you need to load 2 models in your constructor:

protected $_customer;
protected $_customerFactory;

public function __construct(
    \Magento\Customer\Model\Customer $customer,
    \Magento\Customer\Model\ResourceModel\CustomerFactory
){
    $this->_customer = $customer;
    $this->_customerFactory = $customerFactory;
}

public function updateCustomerAttribute($customerId)
{
    $customAttribute = 'your_custom_attribute_goes_here';
    $customAttributeValue = 'your_custom_attribute_value_goes_here';

    $customermodel = $this->_customer;
    $customer = $customermodel->load(customerId);
    $customerData = $customermodel->getDataModel();
    $customerData->setId($customerId);
    $customerData->setCustomAttribute($customAttribute, $customAttributeValue);
    $customermodel->updateData($customerData);

    $customerResource = $this->_customerFactory->create();
    $customerResource->saveAttribute($customermodel, $customAttribute);
}

I have tested this personally and it worked after a lot of hours of research and testing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.