Domanda

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

È stato utile?

Soluzione

You can get billing info by below way,

$billing = $orderData->getBillingAddress();
echo "<pre>";print_r($billing->getData());

Altri suggerimenti

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange
scroll top