Magento 2 - billing address not changed
Question
following code:
<?php
namespace UV\Adressen\Observer;
use Magento\Framework\Event\ObserverInterface;
class LieferadresseEditObserver implements ObserverInterface
{
private $customerRepository;
public function __construct()
{
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
/*
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$orderRepository = $objectManager->get('\Magento\Sales\Model\OrderRepository')->create();
print_r(get_class_methods($order)); exit;
$addressRepository->save($billingAddress);
*/
$address = $observer->getEvent()->getAddress();
if($address->getAddressType() == 'shipping') {
error_log("Das ist eine Lieferadresse");
$order = $address->getOrder();
#print_r(get_class_methods($order)); exit;
$order->setBillingAddress($order->getShippingAddress());
#$order->setShippingAddress($address);
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$orderRepository = $objectManager->get('\Magento\Sales\Model\OrderRepository');
#print_r(get_class_methods($orderRepository)); exit;
$orderRepository->save($order);
}
}
}
?>
Why is the billing address not being changed to shipping address? They should be equal after the code has been executed. Thanks!
Solution
You don't need to use save() function.
$shippingaddressData = $order->getShippingAddress()->getData();
unset($shippingaddressData['entity_id]);
unset($shippingaddressData['parent_id]);
$order->setBillingAddress->setData($shippingaddressData);
instead of
$order->setBillingAddress($order->getShippingAddress());
#$order->setShippingAddress($address);
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$orderRepository = $objectManager->get('\Magento\Sales\Model\OrderRepository');
#print_r(get_class_methods($orderRepository)); exit;
$orderRepository->save($order);
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange