I am trying to add a product to the cart through a controller, one the first attempt the basket is loaded, and says there is nothing in it. Once I reload the controller for a second time the item is added and the basket it shown - with both the first and second item. The code being run each time is exactly the same.

        /** @var \Magento\Catalog\Model\Product $eventProduct */
        $eventProduct = $this->productsHelper->getProductById($eventProductId);

        // Set relevant options
        $params = [
            'product' => $eventProductId,
            'related_product' => null,
            'options' => [
                25 => $referenceNumber,
                26 => "{$event->getTitle()} on {$event->getEventDate()}"
            'qty' => $qty

        // Add to cart
        $cart = $this->cartHelper->getCart();
        $cart->addProduct($eventProduct, $params);

        return $this->_redirect("checkout/cart/index");

1 Answer 1


After some trouble shooting I found that the code above is correct, however it was failing to appear like I was adding anything to the cart because above this script a user was being logged in. The result in the cache being mismatched I can only assume.

I resolved this by separating the add to cart code in to a new controller where the would then have been loaded as the logged in user.

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.