0

Magento 2.4

I know there is a way to make this work by adding a new address and setting the checkboxes accordingly, but is there really no way to change only the billing or shipping address without creating a new address?

The button says "Change billing address" and so I think it should really only change the billing address, not billing AND shipping? Has anyone build something to bypass the need to create an extra address? This is not really intuitive for the customer.

Thanks!

1 Answer 1

0

You can change the billing or shipping address for an order separately programmatically in Magento 2 by using the Magento\Sales\Api\OrderAddressRepositoryInterface interface.

Here is an example of how to change the billing address programmatically:

use Magento\Sales\Api\OrderAddressRepositoryInterface;

use Magento\Sales\Model\Order;

class ChangeBillingAddress { protected $orderAddressRepository;

public function __construct(
    OrderAddressRepositoryInterface $orderAddressRepository
) {
    $this->orderAddressRepository = $orderAddressRepository;
}

public function execute(Order $order, array $billingAddressData)
{
    $billingAddress = $order->getBillingAddress();
    $billingAddress->addData($billingAddressData);
    $this->orderAddressRepository->save($billingAddress);
}

}

In this example, the OrderAddressRepositoryInterface is injected in the class constructor, and the execute() method takes an Order object and an array of billing address data as parameters. The billing address data should include the fields that you want to update, such as the first name, last name, street address, city, etc.

To change the shipping address programmatically, you can use a similar approach, but instead of calling $order->getBillingAddress(), you would call $order->getShippingAddress() to get the shipping address object.

1
  • Thanks, but this is not what I meant ... I meant the customer account and the customer view so to say. Commented Feb 15, 2023 at 7:20

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.