We have to fetch and set a session variable when the user redirects to our magento2 website using session-id from 3rd party site.

It looks like <http://magento2.com/?SID=322e00bfqm3g6rfpjumah3939n>

For this, we have written a custom router & match method but it's not triggering properly at the first time.

Could you please review the below logic and files and advise please.

**Note**: The implementation is in the same module only.
 The concept of this implementation is if the user redirecting to the M2 website from 3rd party we need to save fetch the SID (session-id) & and the user will browse the products on the M2 website. who are coming from 3rd party we don't want to display proceed to checkout button.

I have written a custom router and setting array value in the core session and trying to fetch that value in the plugin which is in the same module.

> 
> 1. app/code/Vendor/ModuleName/etc/frontend/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\Framework\App\RouterList">
        <arguments>
            <argument name="routerList" xsi:type="array">
                <item name="redirect_if_sid" xsi:type="array">
                    <item name="class" xsi:type="string">Vendor\ModuleName\Controller\Router\RewriteSID</item>
                    <item name="disable" xsi:type="boolean">false</item>
                    <item name="sortOrder" xsi:type="string">20</item>
                </item>
            </argument>
            <argument name="cacheable" xsi:type="boolean">false</argument>
        </arguments>
    </type>
    <! -- For Proceed to checkout button -->
    <type name="Magento\Checkout\Helper\Data">
        <plugin name="CartPageProceeedtoToCheckoutButton" type="Vendor\ModuleName\Plugin\AfterProceedToCheckoutButton" disabled="false"/>
    </type>
</config>

```

> 
> 2. Vendor/ModuleName/Controller/Router/RewriteSID.php
> 
> 
> 

```
<?php
namespace Vendor\ModuleName\Controller\Router;
use Psr\Log\LoggerInterface;
use Magento\Framework\App\ActionFactory;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Data\Form\FormKey;
use Magento\Framework\Session\SessionManagerInterface;
class RewriteSID implements \Magento\Framework\App\RouterInterface
{   

    private $logger;
    private $actionFactory;
    private $response;
    private $formKey; 
    private $coreSession;    

    public function __construct(
        LoggerInterface $logger,
        ActionFactory $actionFactory,
        ResponseInterface $response,
        FormKey $formKey,
        SessionManagerInterface $coreSession

    ) {
        $this->logger = $logger;
        $this->actionFactory = $actionFactory;
        $this->response = $response;
        $this->formKey = $formKey;
        $this->coreSession = $coreSession;

    }

    public function match(\Magento\Framework\App\RequestInterface $request)
    {
        $identifier = $request->getOriginalPathInfo();
        $condition = new \Magento\Framework\DataObject(['identifier' => $identifier, 'continue' => true]);
        $identifier = $condition->getIdentifier();
        if ($condition->getRedirectUrl()) {
            $this->response->setRedirect($condition->getRedirectUrl());
            $request->setDispatched(true);
            return $this->actionFactory->create('Magento\Framework\App\Action\Redirect');
        }
        if (!$condition->getContinue()) {
            return null;
        }
        if ($request->getParam('SID')) {

            $sessionData = array("Volvo", "BMW", "Toyota");
           try {  
                    $this->coreSession->setVIEUserData($sessionData);
            }catch(\Exception $e) {
                    $this->logger->info("Something went wrong");
                    $this->logger->info($e->getMessage());
            }

        }//get params ends here
        else
        {
            return null;
        }  
    }

}

```

> 
> 3. app/code/Vendor/ModuleName/Plugin/AfterProceedToCheckoutButton.php
> 
> 
> 

```
<?php
namespace Vendor\ModuleName\Plugin;
use Magento\Framework\Session\SessionManager;
class AfterProceedToCheckoutButton
{

    private $coreSession;

    public function __construct(
        SessionManager $sessionManager
    ) {
        $this->coreSession = $sessionManager;    
    }

    /**
     * @param $subject
     * @param $result
     * @return mixed
    */
    public function afterCanOnepageCheckout(\Magento\Checkout\Helper\Data $subject, $result)
    {
        //Here $this->coreSession->getVIEUserData() unable to fetch here

        if($result) {
            if($this->coreSession->getVIEUserData()) {
                return false;
            }
            return $result;
        }

    }
}

```