I need to get the guest billing address on the success.phml so I can send to a third party app.

I have managed to use this:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$orderData = $objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId($block->getOrderId());
echo "<pre>";
print_r($orderData->getData());
echo "</pre>"; 
echo $orderData->getData('customer_email');

But the billing address is blank - so I assume that the addresses from the code above is only for registered customers or customers that have registered an address at least.

So my question is how do I get the guest address/data?

It must be able to be pulled as its on the page "checkout/#payment"

My PHP knowledge is pretty basic when it comes to OOP.

Thanks in advance

up vote 0 down vote accepted

You can get billing info by below way,

$billing = $orderData->getBillingAddress();
echo "<pre>";print_r($billing->getData());
  • How to get a country_code from order data? – Dhaduk Mitesh May 4 at 9:39
  • Not get data. I want country_name and country_code. Do you have solution for this? – Dhaduk Mitesh May 4 at 9:56

First of all, I would not recommend to use ObjectManager as it is considered bad practice.

Regarding your question, and instead of using the success.phtml to get the billing address, you could use and observer or a plugin to get the info you need.

Observer Approach

Create an observer to observe event checkout_submit_all_after.

By observing this event you get access to an array with

['order' => $order, 'quote' => $quote]

every time someone tries to place an order. Then you can use those two objects to place your logic, and extract the information you need to send to the 3rd party app.

Plugin Approach

A plugin in placeOrder method in \Magento\Quote\Model\QuoteManagement class.

Similar to the observer approach, by creating a plugin around this method, you get access to the quote and the order object, you can later use to extract the information you need.

Important: If using this approach, remember call the $proceed as shown bellow:

 public function aroundPlaceOrder(QuoteManagement $subject, callable $proceed, $cartId, PaymentInterface $paymentMethod)
    {
        $this->doSmthBeforeProductIsSaved();
        $returnValue = $proceed(); // <--- important
        if ($returnValue) {
            $this->postProductToFacebook();
        }
        return $returnValue;
    }

because if you don't, you will prevent the execution of all the plugins next in the chain and the original method call.

Some related links

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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