I'm trying to add a custom cookie that will save the current cart item total so an external CMS can access and display it. However, when the cookie is created successfully, any subsequent page loads just default to the store home page.

namespace MyVendor\Cartsession\Observer;

use \Magento\Framework\Event\Observer; use \Magento\Framework\Event\ObserverInterface; use Magento\Framework\Stdlib\CookieManagerInterface; use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory; use Magento\Framework\Session\SessionManagerInterface; use \Magento\Checkout\Model\Session as CheckoutSession;

class UpdateCartCount implements ObserverInterface { /** @var CheckoutSession */ protected $checkoutSession;

 * @param CheckoutSession $checkoutSession
public function __construct(
    CookieManagerInterface $cookieManager,
    CookieMetadataFactory $cookieMetadataFactory,
    SessionManagerInterface $sessionManager,
    CheckoutSession $checkoutSession
    ) {
    $this->checkoutSession = $checkoutSession;
    $this->cookieManager = $cookieManager;
    $this->cookieMetadataFactory = $cookieMetadataFactory;
    $this->sessionManager = $sessionManager;

public function execute(Observer $observer) {

    /** @var \Magento\Quote\Model\Quote  */
    $quote = $this->checkoutSession->getQuote();

    $cartData = $quote->getAllVisibleItems();
    $totalQuantity = count($cartData);

    $totals = $quote->getTotals();
    $duration = 3000;

    $metadata = $this->cookieMetadataFactory




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.