:::: MENU ::::
Posts tagged with: order

Magento 2 current order

In this tutorial, we talk about how to get Magento 2 current order. Which includes order information current order id, Increment Id and other order-related data too like shipping information, billing information, and customer information. Now we start with our example.

You have to inject the checkout model session to get the latest order information.

protected $_checkoutSession;


public function __construct(
\Magento\Checkout\Model\Session $checkoutSession,

) {

        $this->_checkoutSession = $checkoutSession;


    }

Now with $_checkoutSession obejct you can get order following information.

 $order = $this->_checkoutSession->getLastRealOrder();
 $orderId=$order->getEntityId();  // order id
 $order->getIncrementId();  // order increment id
 $order->getShippingMethod(); // shippingm ehtod
 $order->getPayment();   // payment method
 $order->getCustomerEmail(); // customer email
Author: Qaisar Satti
Category: Magento 2
Last Modified: February 4, 2020


Magento 2 order PayPal information

Today we talk about how in Magento 2 order PayPal information. In this tutorial, we focus on how to get order PayPal information which includes the PayPal express token and PayPal correlation id. So let start with our example.

First, we get payment additional information.

[cc lang="php" tab_size="2" lines="40"]
$paymentAdditionalInfo = $order->getPayment()->getAdditionalInformation();

With the heplp of addiotnal information obejct we get paypal expreess token.

// get PayPal express checkout token
$token = $paymentAdditionalInfo['paypal_express_checkout_token'];

With the heplp of addiotnal information obejct we get paypal correlation id.

// get PayPal correlation ID
$correlationId = $paymentAdditionalInfo['paypal_correlation_id'];
Author: Qaisar Satti
Category: Magento 2
Last Modified: February 4, 2020


Magento 2 get order information by increment id

Today we discuss about Magento 2 get order information by increment id.This tutorial include how to change or get information of order by order increment id, how to load order by increment id. There are three ways to do that Api Repository, Factory method and object manager. So let start with our example.

Factory Method:

Best practice method always use this method

namespace QaisarSatti\Module\Block;
class Product extends \Magento\Framework\View\Element\Template
{
 protected $orderFactory;
public function __construct(

        \Magento\Sales\Model\OrderFactory $orderFactory,

    ) {


        $this->orderFactory = $orderFactory;

    }

public function getOrderByIncrementId(){
        $orderIncrementId = 10000003;
        $order = $this->orderFactory->create()->loadByIncrementId($orderIncrementId);

}


}

Api Repository:

This was introduce in Magento 2.1 by magento.

namespace QaisarSatti\Module\Block;
use Magento\Catalog\Api\ProductRepositoryInterface;
class Product extends \Magento\Framework\View\Element\Template
 {
     protected $order;
public function __construct(

\Magento\Sales\Api\Data\OrderInterface $order,

    ) {


        $this->order = $order;

    }

public function getOrderByIncrementId(){
        $orderIncrementId = 10000003;
        $order = $this->order->loadByIncrementId($orderIncrementId);

}
}

Object Manager:

Sometime you need to load order record in phtml file. Not recommended method to use in magento2 but so magento use it so here it is just for knowledge.

$orderIncrementId=10000003;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$orderInfo = $objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId($orderIncrementId);
Author: Qaisar Satti
Category: Magento 2
Last Modified: October 4, 2018