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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵