How To

Magento 2: How to Redirect on Checkout Page After Add to Cart

Hello Magento Friends ?,

In the present article, I will be showing How to Redirect on Checkout Page After Add to Cart in Magento 2. Before beginning, take a look at our last blog was How to Move Apply Coupon to Order Summary in Magento 2.

Preface:

The Magento store owners always face a key issue on how to quicken the buying process for customers in order to decrease bounce rates and cart abandonment. When the customer adds products to the cart it is redirected to the shopping cart page where the list of products is displayed. However, admins can skip this step by directly redirecting them to the checkout page which also contains the cart details.

Redirecting to the checkout page directly, benefits in saving time for customers. As the loading time of the shopping cart page is skipped, it will result in minimized shopping cart abandonments.

Let us look at the steps to Redirect on Checkout Page After Add to Cart in Magento 2 ?

Steps to Redirect on Checkout Page After Add to Cart in Magento 2:

Step 1: First, we need to create a “di.xml” file inside our extension at the following path:

app\code\Vendor\Extension\etc\di.xml

Now add the below code:

<?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\Model\Cart">
        <plugin name="AddProductToCart" type="Vendor\Extension\Plugin\Cartplugin" sortOrder="10" disabled="false"/>
    </type>
</config>

Step 2: After that, we need to create  “Cartplugin.php” file inside the below folder path of extension:

app\code\Vendor\Extension\Plugin\Cartplugin.php

And add the below code: 

<?php
namespace Vendor\Extension\Plugin;

class Cartplugin 
{
     protected $_url;
     protected $request;
     protected $helperdata;
     protected $storeManager;
 
     public function __construct(\Magento\Framework\UrlInterface $url,
     \Magento\Framework\App\Request\Http $request,
     \Magento\Store\Model\StoreManagerInterface $storeManager)
     {
          $this->_url = $url;
          $this->request = $request;
          $this->storeManager = $storeManager;
     }
  
     public function beforeAddProduct($subject, $productInfo, $requestInfo = null)
     {
          $cartrtnurl=$this->storeManager->getStore()->getBaseUrl()."checkout/";
          if($cartrtnurl != '' && isset($cartrtnurl))
          {
               $accUrl = $this->_url->getUrl($cartrtnurl);
               $this->request->setParam('return_url', $accUrl);
          }
          return [$productInfo, $requestInfo];
     }
}

That’s it.

Conclusion:

Hence, now you are able to redirect your customers on the checkout page after add to cart. Eliminate this task and redirect your store customers to a specific URL with Custom Redirect Extension for Magneto 2.

If you have queries regarding the above code, feel free to ask me in the comment section and I will be right back to you. See you in the next article, till then keep coding and keep sharing.

Happy Coding ?

Click to rate this post!
[Total: 7 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

View Comments

Recent Posts

Generating Thumbnails with Spatie Media Library in Laravel 11: A Step-by-Step Guide

Generating image thumbnails is a common requirement in web applications, especially when handling media-heavy content.…

20 hours ago

Enhancing Web Application Security with Laravel’s Built-In Features

In today’s digital landscape, web application security is paramount. As a powerful PHP framework, Laravel…

2 days ago

Magento 2 Extensions Digest October 2024 (New Release & Updates)

October was an exciting month for MageComp! From significant updates across our Magento 2 extension…

2 days ago

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

1 week ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

2 weeks ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

2 weeks ago