0

I am trying to catch referrer from url : https://www.mycompany.com/?referrer_id=5 and save it to cookie so that if user sign up, I can save this cookie value (5 in this case) as his referrer.

My problem is even after setting cookie value (5 in this case) inside aroundDispatch() method of \CompanyName\ModuleName\Model\App\FrontController\RedirectPlugin.php, whenever I try to read that set cookie value inside execute() method of CompanyName\ModuleName\Observer\Customer\RegisterTrackingObserver.php it always returns NULL and always goes to condition

if ( !$trackingCookie )
{

//if not referred then save affiliate ID to 0

    return;

}

Here is code:

file CompanyName\ModuleName\Cookie\TrackingCookie.php

<?php

namespace CompanyName\ModuleName\Cookie;

use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
use Magento\Framework\Stdlib\CookieManagerInterface;
use CompanyName\ModuleName\Helper\DataHelper;

class TrackingCookie
{

    protected $_cookieManager;

    protected $_cookieMetadataFactory;

    public function __construct(
        CookieManagerInterface $cookieManager,
        CookieMetadataFactory  $cookieMetadataFactory
    ) {
        $this->_cookieManager         = $cookieManager;
        $this->_cookieMetadataFactory = $cookieMetadataFactory;
    }

    public function get($cookieName)
    {
        return $this->_cookieManager->getCookie($cookieName);
    }

    public function set($tracking, $cookieName)
    {
        $lifetime = 99;//99 weeks
        $lifetime = (int) $lifetime * 604800;

        $metadata = $this
            ->_cookieMetadataFactory
            ->createPublicCookieMetadata()
            ->setDuration($lifetime)
            ->setPath('/')
        ;

        $this->_cookieManager->setPublicCookie($cookieName, $tracking, $metadata);
    }

    public function delete($cookieName)
    {
        $this->_cookieManager->deleteCookie(
            $cookieName,
            $this->_cookieMetadataFactory->createCookieMetadata()
        );
    }
}

file CompanyName\ModuleName\Helper\DataHelper.php

<?php

namespace CompanyName\ModuleName\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use CompanyName\ModuleName\Cookie\TrackingCookie;


class DataHelper extends AbstractHelper

{
 const AFFLID_COOKIE_NAME         = 'mcps';

 const CUSTID_COOKIE_NAME         = 'custid';

    protected $_trackingCookie;

    public function __construct(
        Context                     $context,
        TrackingCookie              $trackingCookie,
    ) {
        parent::__construct($context);

        $this->_trackingCookie     = $trackingCookie;
    }

    public function getTrackingCookie($cookieName)
    {
        return $this->_trackingCookie->get($cookieName);
    }

    public function setTrackingCookie($tracking, $cookieName)
    {
        $this->_trackingCookie->set($tracking, $cookieName);
    }

    public function deleteTrackingCookie($cookieName)
    {
        $this->_trackingCookie->delete($cookieName);
    }

}

file CompanyName\ModuleName\Observer\Customer\RegisterTrackingObserver.php

<?php

namespace CompanyName\ModuleName\Observer\Customer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use CompanyName\ModuleName\Helper\DataHelper;

class RegisterTrackingObserver implements ObserverInterface
{
    protected $_dataHelper;


    public function __construct(DataHelper $dataHelper)
    {
        $this->_dataHelper = $dataHelper;
    }

    public function execute(Observer $observer)
    {
        $customer = $observer->getCustomer();
        $customerId = $customer->getId();

        //save registered customer's ID to cookie
        $this->_dataHelper->setTrackingCookie($customerId, DataHelper::CUSTID_COOKIE_NAME);

        $trackingCookie = $this->_dataHelper->getTrackingCookie(DataHelper::AFFLID_COOKIE_NAME);

        if ( !$trackingCookie )
        {
            //if not referred then save affiliate ID to 0
            return;
        }

        //save affiliate ID to $trackingCookie
        //here send email to inform referrer has registered
    }
}

file CompanyName\ModuleName\Model\App\FrontController\RedirectPlugin.php

<?php

namespace CompanyName\ModuleName\Model\App\FrontController;

use Magento\Framework\App\FrontControllerInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\UrlInterface;
use CompanyName\ModuleName\Helper\DataHelper;

class RedirectPlugin
{
    protected $_dataHelper;
    protected $_resultFactory;
    protected $_url;

    public function __construct(DataHelper $dataHelper, 
            ResultFactory   $resultFactory, 
            UrlInterface $url)
    {
        $this->_dataHelper = $dataHelper;
        $this->_resultFactory   = $resultFactory;
        $this->_url             = $url;
    }

    public function aroundDispatch(FrontControllerInterface $subject, callable $proceed, RequestInterface $request)
    {   
        //catch url: https://www.mycompany.com/?referrer_id=5
        $aReferrerId = $request->getParam('referrer_id', false);
        if($aReferrerId) {
            //got referrer id = 5
            //now save referrer id to cookie
            $this->_dataHelper->setTracking($aReferrerId, DataHelper::AFFLID_COOKIE_NAME); 
        }
        return $proceed($request);
    }
}

Please tell me where I am doing wrong. Is there any settings I have to do inside admin panel? OR there is some issue with code (Please forget about the syntax, it might not be correct)

  • Have you tried setting it within session instead much easier to access – Dava Gordon May 10 at 8:46
  • are you talking about customer session? when i am catching referrer_id parameter from url that time we can't find any customer session because user will not be signed in that time. – Prince FB May 13 at 6:58
  • are you talking about customer session? when i am catching referrer_id parameter from url that time we can't find any customer session because user will not be signed in that time. – Prince FB May 13 at 6:58
  • No if you store in local session it will in the localcache then you should be able to access that at the time of checkout and set it then – Dava Gordon May 13 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.