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条)

报告相同问题?

悬赏问题

  • ¥30 微信小程序蓝牙数据透传
  • ¥15 加氢站氢负荷数据集来源
  • ¥15 umi接入sentry遇到问题
  • ¥15 HBuilderX打包H5网页,扫码模块无法使用
  • ¥15 Javascript跳转页面后,无法执行后面代码,如何解决?
  • ¥15 echarts绘制图表
  • ¥15 请教两个关于高德地图定位不准的技术问题
  • ¥15 根据企业名称 对照两个文件 样本筛选/匹配
  • ¥15 Linux环境下CA证书更新问题
  • ¥15 sqlserver语句提取结果以外数据