0

I created an own module for a new block in success.phtml. Read my old post here: Magento 2: Override success.phtml

Now, I want to go get the order id in my own success.phtml file.

Tried with:

    $block->getOrderId() and $this->getOrderId() 

but I don't get the id. Any idea what I need to call to get the order id?

2 Answers 2

1

You can use $block->getRealOrderId()

1

You will get Order Increament id from $block->getOrderId().

If you want get Order id ,object then you need to create an extension for create a plugin which change return value of AddData.

app\code\{VendorName}{ModuleName}\Plugin\SuccessPlugin.php

    <?php
    namespace {VendorName}\{ModuleName}\Plugin;

    class SuccessPlugin
{
    protected $orderFactory ;
    public function __construct(
        \Magento\Sales\Model\OrderFactory $orderFactory
    ) {
        $this->orderFactory = $orderFactory;

    }


    public  function afterAddData(
        \Magento\Checkout\Block\Onepage\Success $subject
    ){
        $orderIncrementId = $subject->getData('order_id');
        $order = $this->orderFactory->create()->loadByIncrementId($orderIncrementId);
        $subject->setData('current_order',$order);
    }


}

app\code{VendorName}{ModuleName}\etc\di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
        <type name="Magento\Checkout\Block\Onepage\Success">
      <plugin name="onepage_order_details" type="{VendorName}\{ModuleName}\Plugin\SuccessPlugin" sortOrder="1" />
    </type>
</config>

At success.phtml, you will get order current place Order object by

$order = $block->getCurrentOrder();
$orderId= $order->getId();
$Order_customer_email = $order->getCustomerEmail();
3
  • By using your code i get an error: Call to a member function getId() on null in my phtml file. Tryed also $this but same error
    – Tobi
    Oct 5, 2017 at 8:37
  • Can you please share folder structure of the module. Do you have create module.xml, registration.php,composer.json?
    – Amit Bera
    Oct 5, 2017 at 8:50
  • pastebin.com/ZBRRPhif all files and dirs
    – Tobi
    Oct 5, 2017 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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