0

When viewing orders as customer order_id is visible in URL like this: /sales/order/view/order_id/20/

Is it possible to achieve that orders would be accessible by increment_id value instead? Otherwise there's not much sense in changing increment_id (order number) if customers are still able to see order_id values, so they still know how many orders and at what pace store is getting.

1 Answer 1

2

You need to change the URL from the sales template to add increment_id. After that, you can try the following changes, allowing you to load the order by increment_id instead of ID.

app/code/Vendor/Sales/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\Sales\Controller\AbstractController\OrderLoader">
        <plugin name="order_load_by_increment_id" type="Vendor\Sales\Controller\AbstractController\OrderLoader" sortOrder="1"/>
    </type>
</config>

And now:

app/code/Vendor/Sales/Controller/AbstractController/OrderLoader.php

<?php

namespace Vendor\Sales\Controller\AbstractController;

use Magento\Framework\App\RequestInterface;
use Magento\Sales\Controller\AbstractController\OrderLoader as BaseOrderLoader;
use Magento\Sales\Model\OrderFactory;

class OrderLoader
{
    /**
     * @param OrderFactory $orderFactory
     */
    public function __construct(
        private OrderFactory $orderFactory
    ) {

    }

    /**
     * @param BaseOrderLoader $subject
     * @param RequestInterface $request
     */
    public function beforeLoad(
        BaseOrderLoader $subject,
        RequestInterface $request
    ) {
        $orderId = $request->getParam('order_id');
        if ($orderId) {
            $order = $this->orderFactory->create();
            $order = $order->loadByIncrementId($orderId);
            if ($order && $order->getId()) {
                $request->setParam('order_id', $order->getId());
            }
        }
    }
}
1
  • Your code allows to get orders by increment_id, thanks for that. Yet orders may be accessed by order_id and some modifications to generating links under customer account to orders needed to insert increment_id in URL instead of order_id. I'll check that.
    – player
    Commented Mar 11 at 10:23

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.