0

I'm trying to (re)set the billing address to the customers default billing address after receiving an order (this is for B2B clients which are not allowed to change their billing address). To do this, I have the following code set in a 'checkout_onepage_controller_success_action' event listener. (Limited to customers of a specific customer group)

$customer = $this->customerFactory->create()->load($order->getCustomerId());
$order->setBillingAddress($customer->getDefaultBillingAddress());
$order->save();

But upon ordering this results in a TypeError

TypeError: Argument 1 passed to Magento\Sales\Model\Order::setBillingAddress() must be an instance of Magento\Sales\Api\Data\OrderAddressInterface or null, instance of Magento\Customer\Model\Address\Interceptor given

Is it possible to change the address from a Magento\Customer\Model\Address\Interceptor to Magento\Sales\Api\Data\OrderAddressInterface so this will be properly saved/updated?

Or is there another way I can ensure the orders come in with the default billing address set on the customer?

1 Answer 1

0

The easiest solution seems to be to get and set the values individually:

// Load customers default address data
$customer = $this->customerFactory->create()->load($order->getCustomerId()); 
$default = $customer->getDefaultBillingAddress();

// Grab the order billing address in a variable for ease-of-use
$billing = $order->getBillingAddress();

// Set the address details with the get-data
$billing->setStreet($default->getStreet());
$billing->setCity($default->getCity());
$billing->setPostcode($default->getPostcode());
$billing->setCountry($default->getCountry());
$billing->setCompany($default->getCompany());
$billing->setTelephone($default->getTelephone());
$billing->setFirstname($default->getFirstname());
$billing->setMiddlename($default->getMiddlename());
$billing->setLastname($default->getLastname());

// And make sure to save/store the data
$order->save();

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.