doumaque6551 2016-08-16 07:21
浏览 202
已采纳

以编程方式将产品添加到购物车Magento2

HI i am building a module in which i am adding a product to cart through controller .But is throwing the below error

Recoverable Error: Argument 2 passed to Eecom\Atc\Controller\Atc\Index::__construct() must be an instance of Magento\Framework\View\Result\PageFactory, instance of Magento\Framework\App\Cache\TypeList given, called in C:\wamp\www\productzoom\var\generation\Eecom\Atc\Controller\Atc\Index\Interceptor.php on line 14 and defined in C:\wamp\www\productzoom\app\code\Eecom\Atc\Controller\Atc\Index.php on line 21

Below is my code

namespace Eecom\Atc\Controller\Atc;

 class Index extends \Magento\Framework\App\Action\Action {

         /**
          * @var \Magento\Checkout\Model\Cart
          */
         protected $cart;
         /**
          * @var \Magento\Catalog\Model\Product
          */
         protected $product;

         public function __construct(
             \Magento\Framework\App\Action\Context $context,
             \Magento\Framework\View\Result\PageFactory $resultPageFactory,
             \Magento\Catalog\Model\Product $product,
             \Magento\Checkout\Model\Cart $cart
         ) {
             $this->resultPageFactory = $resultPageFactory;
             $this->_customerSession = $customerSession;
             $this->cart = $cart;
             $this->product = $product;
             parent::__construct($context);
         }
         public function execute()
         {
             try {
                 $params = array();
                 $params['qty'] = '1';//product quantity
                 /*get product id*/
                 $pId = '1';//productId
                 $_product = $this->product->load($pId);
                 if ($_product) {
                     $this->cart->addProduct($_product, $params);
                     $this->cart->save();
                 }

                 $this->messageManager->addSuccess(__('Add to cart successfully.'));
             } catch (\Magento\Framework\Exception\LocalizedException $e) {
                 $this->messageManager->addException(
                     $e,
                     __('%1', $e->getMessage())
                 );
             } catch (\Exception $e) {
                 $this->messageManager->addException($e, __('error.'));
             }
             /*cart page*/
             //$this->getResponse()->setRedirect('/checkout/cart/index');


         }
    }

Please suggest where i am doing mistake

  • 写回答

2条回答 默认 最新

  • douci1196 2016-08-16 09:18
    关注

    You probably need to delete your var/generation folder, since you might have changed your constructor signature.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何用Thoony写ESP32温湿度检测无源蜂鸣器报警代码?
  • ¥20 部件内部的CT图像数据集
  • ¥15 Visual studio调用动态库
  • ¥15 双目摄像头标定后的校准文件
  • ¥15 powerbi矩阵增加度量值后出现对应关系错乱
  • ¥30 频率分析法分析绘制奈奎斯特图、波特图
  • ¥15 弹出来一万个系统找不到指定的文件框框,怎么解决
  • ¥15 ADS生成的微带线为什么是蓝色空心的
  • ¥15 求一下解题思路,完全不懂
  • ¥15 tensorflow