0
  • For billing issues i want to use default customer name as same billing address in invoice only.
  • i.e Customer name is John Doe and he use Johny Doe in billing address when he order. it is ok for order but when invoice will create it should update to John Doe in billing address in invoice only.
  • He may pay by online of offline whatever.

How can i achieve it?

1

Since the invoice uses the billing adress object of the order you can't save changes in the billing address which should affect only the invoice.

But you can try to use a plugin after the method getBillingAddress() in the invoice model (Magento\Sales\Model\Order\Invoice) and overwrite the name fields (prefix, firstname, lastname etc. depending on your configuration / used fields).

A method like this should work for the suggested approach (don't forget to add the plugin in your di.xml) :

public function afterGetBillingAddress(\Magento\Sales\Model\Order\Invoice $subject, $result)
{
    $customer = $subject->getOrder()->getCustomer();
    if ($customer->getId()){
        $result->setFirstname($customer->getFirstname());
        $result->setLastname($customer->getLastname());
        //... add other name fields like prefix, middlename, suffix if needed 
    }
    return $result;
}
  • i try this but getBillingAddress() is not working to retrive address in core file also – Ketan Borada Jun 5 at 8:50
  • I'm not sure I understand what you are meaning with "not working to retreive address in core file also" – HelgeB Jun 5 at 8:55
  • when i comment above function in Invoice.php although i get billing address – Ketan Borada Jun 5 at 8:58
  • So you have created a plugin class with the method I've suggested in my answer, configured that plugin class in your di.xml as plugin to Magento\Sales\Model\Order\Invoice::getBillingAddress(), run bin/magento di:setup:compile and don't get the changed values if you call getBillingAddress() on an invoice object of an order of a registered customer? – HelgeB Jun 5 at 9:24
  • 1
    That means that whereever the code you are using is located, it doesn't access the billing address through the invoice object but through the order. – HelgeB Jun 5 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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