0

Magento 2 website is mydoamin.com. (Refer a friend) External Website link redirect to my website. This will provide a reward based on refer a friend anyone buy product in Magento website. After external website reachs Magento home page url mydoamin.com/?aid=arabf7d00bddc712c3e51f5b44e6813bbeb1be803a0ff0d69b31c9e014c2a9da9e

This "aid" is name always static name and "arabf7d00bddc712c3e51f5b44e6813bbeb1be803a0ff0d69b31c9e014c2a9da9e" is dynamic change depends on links

I want to store this link name and value after Checkout success page phtml page get to call name and value check exist or not

1 Answer 1

0

Hello you should use the event:

<event name="controller_front_send_response_before">
    <observer name="Observer" instance="Vendor\Module\Observer\Observer" shared="false" />
</event>

The observer:

<?php

namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Request\Http;
use Vendor\Module\Cookie\Aid;

class Observer implements ObserverInterface
{

    protected $request;
    protected $cookie;

    public function __construct(
        Http $request,
        Aid $cookie,
    )
    {
        $this->request = $request;
        $this->cookie = $cookie;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $this->request->getParams(); // all params
        $param = $this->request->getParam('aid');

        $cookieExists = $this->cookie->get();
        if($param != "" && !$cookieExists){
            $this->cookie->set($param, 86400);
        }
    }
}

i've created a separated class for the Cookie in Vendor\Module\Cookie\Aid

<?php

namespace Vendor\Module\Cookie;

use Magento\Framework\Stdlib\CookieManagerInterface;
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
use Magento\Framework\Session\SessionManagerInterface;

class Aid
{
    /**
     * Name of cookie that holds private content version
     */
    const COOKIE_NAME = 'aid';

    /**
     * CookieManager
     *
     * @var CookieManagerInterface
     */
    private $cookieManager;

    /**
     * @var CookieMetadataFactory
     */
    private $cookieMetadataFactory;

    /**
     * @var SessionManagerInterface
     */
    private $sessionManager;

    /**
     * @param CookieManagerInterface $cookieManager
     * @param CookieMetadataFactory $cookieMetadataFactory
     * @param SessionManagerInterface $sessionManager
     */
    public function __construct(
        CookieManagerInterface $cookieManager,
        CookieMetadataFactory $cookieMetadataFactory,
        SessionManagerInterface $sessionManager,

    ) {
        $this->cookieManager = $cookieManager;
        $this->cookieMetadataFactory = $cookieMetadataFactory;
        $this->sessionManager = $sessionManager;
    }

    /**
     * Get form key cookie
     *
     * @return string
     */
    public function get()
    {
        return $this->cookieManager->getCookie(self::COOKIE_NAME);
    }

    /**
     * @param string $value
     * @param int $duration
     * @return void
     */
    public function set($value, $duration = 86400)
    {
        $metadata = $this->cookieMetadataFactory
            ->createPublicCookieMetadata()
            ->setDuration($duration)
            ->setPath($this->sessionManager->getCookiePath())
            ->setDomain($this->sessionManager->getCookieDomain());

        $this->cookieManager->setPublicCookie(
            self::COOKIE_NAME,
            $value,
            $metadata
        );
    }

    /**
     * @return void
     */
    public function delete()
    {
        $metadata = $this->cookieMetadataFactory
            ->createPublicCookieMetadata()
            ->setDuration(36000)
            ->setPath($this->sessionManager->getCookiePath())
            ->setDomain($this->sessionManager->getCookieDomain());

        $this->cookieManager->deleteCookie(
            self::COOKIE_NAME,
            $metadata
        );
    }
}

With all of this you'll be able to use the cookie in your success page using the event "checkout_submit_all_after" if you need to send a curl or check if there's your cookie, or just updating the layout: checkout_onepage_success.xml adding your PHTML to the page

5
  • Thanks for you useful answer, I got this error, 2 exception(s): Exception #0 (ReflectionException): Class Mytask\ReferCookie\Observer\Aid does not exist Commented Jun 1, 2021 at 12:43
  • check that the di.xml observer event declaration of instance: Vendor\Module\Observer\Observer (if you called it Vendor\Module\Observer\Aid) be sure that it corresponds with the observer class Commented Jun 1, 2021 at 13:27
  • Can you please add example of di.xml in your answer? Commented Jun 2, 2021 at 10:06
  • i didn't need a di in my module, i just built a \Vendor\Module\etc\frontend\events.xml where i placed this code: <?xml version="1.0"?> <config xmlns:xsi="w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> <event name="controller_front_send_response_before"> <observer name="Observer" instance="Vendor\Module\Observer\Observer" shared="false" /> </event> </config> Commented Jun 3, 2021 at 10:10
  • Have you ended up solving your issue? Commented Jun 10, 2021 at 11:32

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.