1

In my custom module events.xml

<event name="sales_quote_save_after">
        <observer name="cedapi_set_checkout_quote_id" instance="Vender\Module\Observer\SalesQuoteSaveAfter" />
    </event>

In my observer SalesQuoteSaveAfter.php

<?php
namespace Vender\Module\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Stdlib\Cookie\PublicCookieMetadata;

class SalesQuoteSaveAfter implements ObserverInterface
{
    public function __construct(
    \Magento\Checkout\Model\Session $checkoutSession,
    \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager,
    \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory,
    \Psr\Log\LoggerInterface $logger,
    \Magento\Framework\Session\SessionManagerInterface $sessionManager
    )
    {
        $this->checkoutSession = $checkoutSession;
        $this->cookieManager = $cookieManager;
        $this->cookieMetadataFactory = $cookieMetadataFactory;
        $this->logger = $logger;    
        $this->sessionManager = $sessionManager;
    }

    /**
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $quote = $observer->getEvent()->getQuote();
        /* @var $quote \Magento\Quote\Model\Quote */
        $publicCookieMetadata = $this->cookieMetadataFactory
                                     ->createPublicCookieMetadata()
                                     ->setPath('/');
        $duration = 86400;
        $metadata = $this->cookieMetadataFactory
          ->createCookieMetadata()
          ->setDuration($duration)
          ->setPath($this->sessionManager->getCookiePath())
          ->setDomain($this->sessionManager->getCookieDomain());

        $this->cookieManager->setPublicCookie('quoteId', $quote->getId(),$metadata);
        $this->_logger->info($this->cookieManager->getCookie('quoteId'));
    }
}

Not working.Any one help me

0

Here is how I delete a cookie.

public function execute(
    \Magento\Framework\Event\Observer $observer
) {
    $cookieMetadata = $this->_cookieMetadataFactory->createCookieMetadata()->setPath('/');
    $this->_cookieManager->deleteCookie('your_cookie', $cookieMetadata);
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.