2016-08-16 49 views
0

HI我建立一個模塊中,我公司通過控制器將產品添加到購物車。但拋出下面的錯誤添加到購物車產品編程Magento2

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

下面是我的代碼

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'); 


     } 
    } 

請建議我在哪裏做錯

回答

3

您可能需要刪除您的var/generation文件夾,因爲您可能已更改您的構造函數簽名。

+0

由於其工作的罰款。您能否告訴我爲什麼在將產品添加到購物車後不更新標題中的minicart。我已經在我的模塊的前端文件夾中添加了sections.xml文件作爲建議基爾http://magento.stackexchange.com/questions/100615/magento-2-how-can-refresh-minicart-cache-after-clear-cart- session-and-place-orde但仍然不更新 –

+0

你可以發佈你的'sections.xml'文件嗎? –

+0

嗨,我剛剛使用阿賈克斯,它工作正常,但它並沒有更新項目的HTML,它正在更新標題和總計購物車,但不是項目的一部分,說我從購物車頁面添加一個新的產品到購物車,但它沒有顯示在項目部分中,只更新購物車頁面上的標題購物車和總計<?xml version =「1.0」?> <動作名稱= 「ATC/ATC /索引」> <段名= 「購物車」/>

0

請試試這個

<?php 
    namespace Webkul\Hello\Controller\Index; 

    use Magento\Framework\App\Action\Context; 
    use Magento\Framework\View\Result\PageFactory; 

    class AddProducttocart extends \Magento\Customer\Controller\AbstractAccount 
    { 
     /** 
     * @var PageFactory 
     */ 
     protected $resultPageFactory; 
     /** 
     * @var \Magento\Framework\Data\Form\FormKey 
     */ 
     protected $formKey; 
     /** 
     * @param Context $context 
     * @param PageFactory $resultPageFactory 
     */ 
     public function __construct(
      Context $context, 
      \Magento\Framework\Data\Form\FormKey $formKey, 
      PageFactory $resultPageFactory 
     ) { 
      parent::__construct($context); 
      $this->formKey = $formKey; 
      $this->resultPageFactory = $resultPageFactory; 
     } 

     /** 
     * 
     * @return \Magento\Framework\View\Result\Page 
     */ 
     public function execute() 
     { 
      $resultPage = $this->resultPageFactory->create(); 
       $params = array(
        'form_key' => $this->formKey->getFormKey(), 
        'product' =>12,//product Id 
        'qty' =>1,//quantity of product 
        'price' =>100 //product price 
       ); 
       $this->_redirect("checkout/cart/add/form_key/", $params); 
      /** @var \Magento\Framework\View\Result\Page $resultPage */ 
      return $resultPage; 
     } 
    } 
相關問題