Magento – Redirect issue after add to cart in magento2 custom controller

addtocartmagento2redirectredirect-url

I am calling my custom controller for adding the product through the form action.

Below is my controller file.

  <?php 

  namespace {Vendor}\{Module}\Controller\Product;  
  use Magento\Framework\Controller\ResultFactory;                           
 class AddProduct extends \Magento\Framework\App\Action\Action                 
{
 protected $formKey;
 protected $cart;
 protected $product;
 protected $checkoutSession;
 public function __construct(
  \Magento\Framework\App\Action\Context $context,
  \Magento\Framework\Data\Form\FormKey $formKey,
  \Magento\Checkout\Model\Cart $cart,
  \Magento\Catalog\Model\ProductFactory $product,
   \Magento\Checkout\Model\Session $checkoutSession,
   array $data = []) {
   $this->formKey = $formKey;
   $this->cart = $cart;
   $this->product = $product;
   $this->checkoutSession = $checkoutSession;
   parent::__construct($context);
  }

 public function execute()
{
 $productId = 1;
 $customPrice = 150;
 $params = array(
  'form_key' => $this->formKey->getFormKey(),
 'product_id' => $productId, //product Id
 'qty'   => 1 //quantity of product
 );

$_product = $this->product->create()->load($productId);
$this->cart->addProduct($_product, $params);

 $productItem = $this->getProductQuote($_product);
 $productItem->setCustomPrice($customPrice);
 $productItem->setOriginalCustomPrice($customPrice);
  //Enable super mode on the product.
  $productItem->getProduct()->setIsSuperMode(true);
  $this->cart->save();

   $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
    $resultRedirect->setUrl('http://localsite/checkout/cart');
    return $resultRedirect; 
}


public function getProductQuote($product)
{
 $quote = $this->checkoutSession->getQuote();
 $cartItems = $quote->getItemByProduct($product);
 return $cartItems;
 } 
}

After $this->cart->save(); , I am redirecting to checkout page

So Product is not adding to the cart, Issue exists in Chrome browser. Mozilla it's fine.

I am getting 302 not found status code in the request. May this be the reason

Can anyone look into this and update me, please. Thanks

Best Answer

I think your code is right, only you are missing to

use Magento\Framework\Controller\ResultFactory;

after your namespace declaration
Your code should work with it:

<?php 

 namespace {Vendor}\{Module}\Controller\Product; 

 use Magento\Framework\Controller\ResultFactory; 

 class AddProduct extends \Magento\Framework\App\Action\Action                 
 {
 protected $formKey;
 protected $cart;
 protected $product;
 protected $checkoutSession;
 public function __construct(
  \Magento\Framework\App\Action\Context $context,
  \Magento\Framework\Data\Form\FormKey $formKey,
  \Magento\Checkout\Model\Cart $cart,
  \Magento\Catalog\Model\ProductFactory $product,
   \Magento\Checkout\Model\Session $checkoutSession,
   array $data = []) {
   $this->formKey = $formKey;
   $this->cart = $cart;
   $this->product = $product;
   $this->checkoutSession = $checkoutSession;
   parent::__construct($context);
  }

  public function execute()
  {
  $productId = 1;
  $customPrice = 150;
  $params = array(
  'form_key' => $this->formKey->getFormKey(),
  'product_id' => $productId, //product Id
  'qty'   => 1 //quantity of product
  );

  $_product = $this->product->create()->load($productId);
  $this->cart->addProduct($_product, $params);

  $productItem = $this->getProductQuote($_product);
  $productItem->setCustomPrice($customPrice);
  $productItem->setOriginalCustomPrice($customPrice);
  //Enable super mode on the product.
  $productItem->getProduct()->setIsSuperMode(true);
  $this->cart->save();

   $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
   $resultRedirect->setUrl('checkout/cart');
   return $resultRedirect; 
 }


 public function getProductQuote($product)
 {
  $quote = $this->checkoutSession->getQuote();
  $cartItems = $quote->getItemByProduct($product);
  return $cartItems;
 } 
}