0

I have a custom Order model, it has a method in it @method int getIncrementId()

I have a template to transfer the id of the current order/app/code/Qq/Www/view/frontend/templates/success/index/success_page.phtml

How do I get the ID after the form is submitted by this controller.

        if ($data) {
            /** @var \Qq\Www\Model\Order $model */
            $model = $this->_objectManager->create('Qq\Www\Model\Order');
            $model->setData($data);
            $model->setStoreId($this->_storeManager->getStore()->getId());

            try {


                $this->objectRepository->save($model);
                $this->messageManager->addSuccess(__('Request was added to processing.'));
                $this->dataPersistor->clear('dv_sample_order');

                // Send email
                $this->helper->sendEmail($model);

            } catch (\Magento\Framework\Exception\LocalizedException $e) {
                $this->messageManager->addError($e->getMessage());
            } catch (\Exception $e) {
                $this->messageManager->addException($e, __('Something went wrong while saving the data.'));
            }
        }
        return $resultRedirect->setPath('qq_success_page/success/index');

update


There is already a method in my Model

    public function getLastIncrementId()
    {
        $result = null;
        try {
            $result = $this->collection->create()->getLastIncrementId();
        } catch (\Exception $e) {
            $this->_logger->error($e->getMessage());
        }

        return $result;
    }

I create a block, pass such data to it

namespace Vendor\Module\Block;

class Success
{
    protected $_orderData;

    public function __construct(
        \Vendor\Module\Model\Order $_orderData
    ) {
        $this->_orderData = $_orderData;
    }

    public function getLastOrderId():? int
    {
        return $this->_orderData->getLastIncrementId();
    }
}

xml for my controller

    <referenceBlock name="content">
        <block class="Vendor\Module\Block\Success" template="Vendor_Module::success/index/success_page.phtml" name="vendor_success_page" />
    </referenceBlock>

my template

<?php echo $block->escapeHtml($block->getLastOrderId());?>

that not work for me

3

2 Answers 2

0

Have you checked the value of this function.

  $result = $this->collection->create()->getLastIncrementId();

Are you sure that it's returning the result you expect?

You don't normally create an instance of a collection to then call a function or expect a single value. Collections are usually for fetching a set of results, or applying filters on for searching.

0

According to your Scenario, you want to get order id inside the success_page.phtml. you can achieve this functionality in two ways if your success page inherits the Magento core block so you can simply call this method which will return you the last order id.

<?php echo $block->escapeHtml($block->getOrderId());?>

or if you are using your own custom module so you can create a helper or your custom block and then simply create a public method

namespace Vendor\Module\Block;

    class Success
    {
        protected $_orderData;

        public function __construct(
            \Magento\Checkout\Model\Session $_orderData
        ) {
            $this->_orderData = $_orderData;
        }

        public function getLastOrderId():? int
        {
            $_order = $this->_orderData->getLastRealOrder();
            if ($_order !== null && $_order->getId() !== null) {
                return (int)$_order->getId();
            }
            return null;
        }
    }

and then in your .phtml file, it will be like:

<?php echo $block->escapeHtml($block->getLastOrderId());?>

Here I am using /** @var \Magento\Checkout\Model\Session */ which is always use shared object and returns the last incremental id of the order you can also check the getLastRealOrder method implementation which will give you an idea how its working.

or if you want to get whole order data like payment method etc just simply replace the block function with this:

public function getLastOrder():? \Magento\Sales\Model\Order
{
    $_order = $this->_orderData->getLastRealOrder();
        if ($_order !== null && $_order->getId() !== null) {
            return $_order;
        }
        return null;
}
2
  • Please read update question Mar 15, 2020 at 20:08
  • @annkarsksya I updated my code according to your block there is no need for using model class here we can directly inject as a dependency $checkoutSession in our class and then with that, we can fetch last order data because $checkoutSession use shared object and updated when an order has been placed. Mar 16, 2020 at 7:21

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.