2

In my success.phtml , $block->getViewOrderUrl() gives a order url with order id(entity id) .

dev/tests/functional/tests/app/Magento/Checkout/Test/Block/Onepage/Success.php

/sales/order/view/order_id/173569/

How to get this order id alone - 173569 in success.phtml.

I don't see any functions in success.php

1

You can try this one to get the order Id alone:

$block->getOrderId()

UPDATE:

You can override the Block file of checkout success one page using preference and define your own method to get the order Id.
Try this:
In your custom module's di.xml file add this:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Checkout\Block\Onepage\Success" type="Vendor\Module\Block\Onepage\Success" />
</config>  

Now create Success.php Block file in Folder Vendor\Module\Block\Onepage

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Vendor\Module\Block\Onepage;

use Magento\Customer\Model\Context;
use Magento\Sales\Model\Order;

/**
 * One page checkout success page
 *
 * @api
 * @since 100.0.2
 */
class Success extends \Magento\Framework\View\Element\Template
{
    /**
     * @var \Magento\Checkout\Model\Session
     */
    protected $_checkoutSession;

    /**
     * @var \Magento\Sales\Model\Order\Config
     */
    protected $_orderConfig;

    /**
     * @var \Magento\Framework\App\Http\Context
     */
    protected $httpContext;

    /**
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param \Magento\Checkout\Model\Session $checkoutSession
     * @param \Magento\Sales\Model\Order\Config $orderConfig
     * @param \Magento\Framework\App\Http\Context $httpContext
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Sales\Model\Order\Config $orderConfig,
        \Magento\Framework\App\Http\Context $httpContext,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->_checkoutSession = $checkoutSession;
        $this->_orderConfig = $orderConfig;
        $this->_isScopePrivate = true;
        $this->httpContext = $httpContext;
    }

    /**
     * Render additional order information lines and return result html
     *
     * @return string
     */
    public function getAdditionalInfoHtml()
    {
        return $this->_layout->renderElement('order.success.additional.info');
    }

    /**
     * Initialize data and prepare it for output
     *
     * @return string
     */
    protected function _beforeToHtml()
    {
        $this->prepareBlockData();
        return parent::_beforeToHtml();
    }

    /**
     * Prepares block data
     *
     * @return void
     */
    protected function prepareBlockData()
    {
        $order = $this->_checkoutSession->getLastRealOrder();

        $this->addData(
            [
                'is_order_visible' => $this->isVisible($order),
                'view_order_url' => $this->getUrl(
                    'sales/order/view/',
                    ['order_id' => $order->getEntityId()]
                ),
                'print_url' => $this->getUrl(
                    'sales/order/print',
                    ['order_id' => $order->getEntityId()]
                ),
                'can_print_order' => $this->isVisible($order),
                'can_view_order'  => $this->canViewOrder($order),
                'order_id'  => $order->getIncrementId()
            ]
        );
    }

    /**
     * Is order visible
     *
     * @param Order $order
     * @return bool
     */
    protected function isVisible(Order $order)
    {
        return !in_array(
            $order->getStatus(),
            $this->_orderConfig->getInvisibleOnFrontStatuses()
        );
    }

    /**
     * Can view order
     *
     * @param Order $order
     * @return bool
     */
    protected function canViewOrder(Order $order)
    {
        return $this->httpContext->getValue(Context::CONTEXT_AUTH)
            && $this->isVisible($order);
    }

    /**
     * @return string
     * @since 100.2.0
     */
    public function getContinueUrl()
    {
        return $this->_storeManager->getStore()->getBaseUrl();
    }


    /**
    * @return string
    * getOrigId
    */
    public function getOrigId()
    {
        return $this->_checkoutSession->getLastRealOrder()->getEntityId();
    }

}

Then you can now get the Order Id(Entity Id) in your .phtml template like this:

$block->getOrigId()
  • $block->getOrderId() will get me entity id ? whats the difference between $block->getViewOrderUrl()->getId() and $block->getOrderId() – summu Apr 7 at 10:22
  • Yeah, the $block->getOrderId() will get the entity id. Actually, I am not sure with the $block->getViewOrderUrl()->getId() but you can check. – magefms Apr 7 at 10:26
  • $block->getOrderId() gave me increment id not entitty id, /sales/order/view/order_id/173569/ , this 173569 is entity id which output of $block->getViewOrderUrl() , i just wanted 173569 – summu Apr 7 at 12:04
  • I think better if you would override the Block file for checkout success.phtml file and create your own method to get order Id – magefms Apr 7 at 12:50
  • 1
    @summu updated my answer please check – magefms Apr 7 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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