Magento 2 Sales Order – Get Order Billing Details

billingmagento2sales-order

So I am working on this export for a SOAP web service, I am currently getting the order information from the following namespace \Magento\Sales\Api\OrderRepositoryInterface but the thing is it doesn't return more specific that to the billing.

How can I get information like, postcode, order email etc? Is there any class for example BillingOrderRepositoryInterface?

Thank you.

Best Answer

EDIT:

There is not need to do all of that, you can simply call the following function $order->getBillingAddress(); and you'll get all the billing information. This is as long you are using the \Magento\Sales\Api\OrderRepositoryInterface class.

OLD ANSWER

So after a while of search the core code I found this class \Magento\Sales\Api\OrderAddressRepositoryInterface, returns all the billing information.

Here is the implementation in case someone needs it.

use \Magento\Sales\Api\OrderAddressRepositoryInterface;
use \Magento\Framework\Api\SearchCriteriaBuilder;
use \Magento\Sales\Api\OrderRepositoryInterface;

class MyClass 
{
    protected $_orderBilling;
    protected $_searchCriteria;
    protected $_orderCollection;

    public function __construct(OrderRepositoryInterface $orderRepository, OrderAddressRepositoryInterface $orderAddressRepositoryInterface, SearchCriteriaBuilder $criteria) 
    {
        $this->_searchCriteria  = $criteria;
        $this->_orderCollection = $orderRepository;
        $this->_orderAddress    = $orderAddressRepositoryInterface;
    }

    public function myFunctionName()
    {
        $orderFilter = $this->_searchCriteria
            ->addFilter("export_status", 0)
            ->create();

        $orders = $this->_orderCollection
            ->getList($orderFilter)
            ->getItems();

        foreach ($orders as $order) {
            $billingFilter = $this->_searchCriteria
                ->addFilter("parent_id", $order->getData("entity_id"))
                ->create();

            $billing = $this->_orderBilling
                ->getList($billingFilter)
                ->getItems();
        }
    }
}
Related Topic