Cheerymagicsoftware official page


Programming is a cheery magic!


Custom url for checkout and cart in Magento 2

Author Poliakov Vladimir.
Suppose you need change checkout’s url (/checkout to /customcheckout) and cart (/checkout/cart to /customcheckout/basket) in Magento 2 shop. The first thing that comes to mind use Url Rewrite Module (Admin->Marketing->Url Rewrites)

Add url rewrite in Magento 2

For these two links need set Redirect type = “No” and redefine new links in your theme (suppose you can do it). During use will be that the collapses in the shipping or payment you will always return to the page “checkout/cart” - this is hardcoded in Magento 2:

Search cart in vendor Magento 2

Also /customcheckout#shipping after estimate, you redirect to checkout/#payment. So let’s make module, which by url define if /customcheckout then needed load module /checkout and accordingly /customcheckout/basket -> /checkout/cart, аnd also redirect /checkout –> /customcheckout and /checkout/cart –>/customcheckout/basket. At the same time must work functions such /checkout/cart/add, /checkout/cart/delete, /checkout/cart/updatepost… and others.

Create a standard set of files for our module

/app/code/CheeryMagic/CustomCheckout/registration.php


        <?php
    \Magento\Framework\Component\ComponentRegistrar::register(
        \Magento\Framework\Component\ComponentRegistrar::MODULE,
        'CheeryMagic_CustomCheckout',
        __DIR__
    );

/app/code/CheeryMagic/CustomCheckout/composer.json


        {
    "name": "cheerymagic/module-customcheckout",
        "description": "",
        "license": "",
        "authors": [
            {
                "name": "CustomCheckout",
                "email": "cheerymagicsoftware@gmail.com"
            }
        ],
        "minimum-stability": "dev",
        "require": {},
        "autoload": {
            "psr-4": {
                "CheeryMagic\\CustomCheckout\\": ""
            },
            "files": [
                "registration.php"
            ]
        }
    }

/app/code/CheeryMagic/CustomCheckout/etc/module.xml


        <?xml version="1.0" ?>
    
        <module name="CheeryMagic_CustomCheckout" setup_version="1.0.0" />
    

/app/code/CheeryMagic/CustomCheckout/etc/frontend/routes.xml


        <?xml version="1.0" ?>
    
        
            
                
            
        
    

Now we show that we have to use a router class to call /customcheckout and /customcheckout/basket:

/app/code/CheeryMagic/CustomCheckout/etc/frontend/di.xml


        <?xml version="1.0" ?>
    
        
            
                
                    
                        CheeryMagic\CustomCheckout\Controller\Router
                        false
                        60
                    
                
            
        
    

Class router, which will be targeted to load our checkout/index/index and checkout/cart/index:

/app/code/CheeryMagic/CustomCheckout/Controller/Router.php


        <?php

    namespace CheeryMagic\CustomCheckout\Controller;

    class Router implements \Magento\Framework\App\RouterInterface
    {

        /**
         * @var \Magento\Framework\App\ActionFactory
         */

        protected $actionFactory;

        /**
         * @param \Magento\Framework\App\ActionFactory $actionFactory
         */

        public function __construct(
            \Magento\Framework\App\ActionFactory $actionFactory
        )
        {
            $this->actionFactory = $actionFactory;
        }

        /**
         * @param \Magento\Framework\App\RequestInterface $request
         * @return bool
         */
        public function match(\Magento\Framework\App\RequestInterface $request)
        {

            $uri = \CheeryMagic\CustomCheckout\Plugin\Helper::getArrayPathInfo($request->getPathInfo());

            $route = [];
            if (
                count($uri) == 2 &&
                ($uri[0] == 'checkout' && $uri[1] == 'cart' || $uri[0] == 'customcheckout' && $uri[1] == 'basket')
            ) {
                $route = [
                    'module' => 'checkout',
                    'controller' => 'cart',
                    'action' => 'index'
                ];
            }
            if (count($uri) == 1 && ($uri[0] == 'checkout' || $uri[0] == 'customcheckout')) {
                $route = [
                    'module' => 'checkout',
                    'controller' => 'index',
                    'action' => 'index'
                ];
            }

            if (!empty($route)) {
                $module = $request->setModuleName($route['module']);
                $module->setControllerName($route['controller'])->setActionName($route['action']);
                $request->setAlias(\Magento\Framework\Url::REWRITE_REQUEST_PATH_ALIAS, $uri);

                return $this->actionFactory->create('Magento\Framework\App\Action\Forward');
            } else {
                return null;
            }

        }

    }

We also use a little helper function which will choose from our $request array to determine this /checkout or /checkout/cart:

/app/code/CheeryMagic/CustomCheckout/Plugin/Helper.php


        <?php

    namespace CheeryMagic\CustomCheckout\Plugin;

    class Helper
    {

        /**
         * @param $pathInfo
         * @return array
         */

        public static function getArrayPathInfo($pathInfo)
        {
            $array = [];
            if ($pathInfo === '')
                return $array;
            $segs = explode('/', $pathInfo . '/');
            $n = count($segs);
            for ($i = 0; $i < $n - 1; $i++) {
                $key = $segs[$i];
                if ($key === '') continue;
                if (!(($pos = strpos($key, '[')) !== false && ($m = preg_match_all('/\[(.*?)\]/', $key, $matches)) > 0))
                    $array[] = $key;
            }
            return $array;
        }
    }

Now the problem to intercept the path /checkout and /checkout/cart on which can throw us Magento 2 and make redirect to Route Class of our module. For this add plugins ‘before’ on function dispatch() in abstract classes. Magento\Checkout\Controller\Cart and Magento\Checkout\Controller\Action .

/app/code/CheeryMagic/CustomCheckout/etc/di.xml


        <?xml version="1.0" ?>
    
        
            
        
        
            
        
    

/app/code/CheeryMagic/CustomCheckout/Plugin/Magento/Checkout/Controller/Cart.php


        <?php

    namespace CheeryMagic\CustomCheckout\Plugin\Magento\Checkout\Controller;

    class Cart
    {

        /**
         * @param \Magento\Checkout\Controller\Cart $subject
         * @param \Magento\Framework\App\RequestInterface $request
         */

        public function beforeDispatch(
            \Magento\Checkout\Controller\Cart $subject,
            \Magento\Framework\App\RequestInterface $request
        ) {

            $uri = \CheeryMagic\CustomCheckout\Plugin\Helper::getArrayPathInfo($request->getPathInfo());
            $parseUri = parse_url($request->getRequestUri());
            $query = isset($parseUri['query']) ?  '?' . $parseUri['query'] : '';

            if(count($uri) == 2 && $uri[0] == 'checkout' && $uri[1] == 'cart')
            {
                $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
                $responseFactory = $objectManager->get('\Magento\Framework\App\ResponseFactory');
                $url = $objectManager->get('Magento\Framework\UrlInterface');
                $responseFactory->create()->setRedirect(
                    $url->getUrl('customcheckout/basket' . $query
                    ))->sendResponse();
                exit();

           }

        }
    }

/app/code/CheeryMagic/CustomCheckout/Plugin/Magento/Checkout/Controller/Action.php


        <?php

    namespace CheeryMagic\CustomCheckout\Plugin\Magento\Checkout\Controller;

    class Action
    {
        /**
         * @param \Magento\Checkout\Controller\Action $subject
         * @param \Magento\Framework\App\RequestInterface $request
         */

        public function beforeDispatch(
            \Magento\Checkout\Controller\Action $subject,
            \Magento\Framework\App\RequestInterface $request
        )
        {
            $uri = \CheeryMagic\CustomCheckout\Plugin\Helper::getArrayPathInfo($request->getPathInfo());
            $parseUri = parse_url($request->getRequestUri());
            $query = isset($parseUri['query']) ? '?' . $parseUri['query'] : '';

            if (count($uri) == 1 && $uri[0] == 'checkout') {
                $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
                $responseFactory = $objectManager->get('\Magento\Framework\App\ResponseFactory');
                $url = $objectManager->get('Magento\Framework\UrlInterface');
                $responseFactory->create()->setRedirect(
                    $url->getUrl('customcheckout' . $query
                    ))->sendResponse();
                exit();
            }

        }
    }

Run in consloe php bin/magento setup:upgrade.

Now you need override links in your Magento 2 Theme from /checkout to /customcheckout and /checkout/cart to /customcheckout/basket.

Custom Url Cart in Magento 2

Custom Url Checkout in Magento 2

This module isn’t a universal solution, and is given as a way to solve the problem or how to work with the routers and plugins in Magento 2. It is also possible that the module may conflict with your installed modules. This module can be downloaded .

Protected by Copyscape