2

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

  • 1
    302 is permanent redirect. not found would be 404. 302 is not an error. – Philipp Sander Mar 14 at 13:57
  • @PhilippSander, yes but it should be 200 is it? for what reason its showing 302 not found ? – jafar pinjar Mar 14 at 14:01
  • $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT); – Philipp Sander Mar 14 at 15:02
  • 302 does NOT mean "not found". quite the opposite: "found and moved Temporarily" – Philipp Sander Mar 14 at 15:03
  • 1
    @PhilippSander, yes it is there, but i forgot to post here, edited my question – jafar pinjar Mar 15 at 6:31
0

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;
 } 
}
  • hi @magefrms, that is already there – jafar pinjar Mar 15 at 5:59
  • can you try above modified code, because it works for me even in chrome – fmsthird Mar 15 at 6:05
  • yes, but there is no difference, tell me which line is difference here – jafar pinjar Mar 15 at 6:22
  • after this line namespace {Vendor}\{Module}\Controller\Product; you have to declare the use of use Magento\Framework\Controller\ResultFactory; which in your code you forgot to include. – fmsthird Mar 15 at 6:24
  • I told na, it is there, but forgot post in question – jafar pinjar Mar 15 at 6:28

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.