HI Ich baue ein Modul, in dem ich ein Produkt über einen Controller in den Einkaufswagen hinzufüge
Behebbarer Fehler: Argument 2 übergeben an EecomAtcControllerAtcIndex :: __ construct () muss eine Instanz von sein MagentoFrameworkViewResultPageFactory, Instanz von MagentoFrameworkAppCacheTypeList angegeben, aufgerufen C: wampwwwproductzoomvargenerationEecomAtcControllerAtcIndexInterceptor.php in Zeile 14 und definiert in C: wampwwwproductzoomappcodeEecomAtcControllerAtcIndex.php on Zeile 21
Unten ist mein Code
namespace EecomAtcControllerAtc;
class Index extends MagentoFrameworkAppActionAction {
/**
* @var MagentoCheckoutModelCart
*/
protected $cart;
/**
* @var MagentoCatalogModelProduct
*/
protected $product;
public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoFrameworkViewResultPageFactory $resultPageFactory,
MagentoCatalogModelProduct $product,
MagentoCheckoutModelCart $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 (MagentoFrameworkExceptionLocalizedException $e) {
$this->messageManager->addException(
$e,
__("%1", $e->getMessage())
);
} catch (Exception $e) {
$this->messageManager->addException($e, __("error."));
}
/*cart page*/
//$this->getResponse()->setRedirect("/checkout/cart/index");
}
}
Bitte schlagen Sie vor, wo ich Fehler mache
Antworten:
3 für die Antwort № 1Sie müssen wahrscheinlich Ihre löschen var/generation
Ordner, da Sie möglicherweise Ihre Konstruktorsignatur geändert haben.
0 für die Antwort № 2
Bitte versuchen Sie es
<?php
namespace WebkulHelloControllerIndex;
use MagentoFrameworkAppActionContext;
use MagentoFrameworkViewResultPageFactory;
class AddProducttocart extends MagentoCustomerControllerAbstractAccount
{
/**
* @var PageFactory
*/
protected $resultPageFactory;
/**
* @var MagentoFrameworkDataFormFormKey
*/
protected $formKey;
/**
* @param Context $context
* @param PageFactory $resultPageFactory
*/
public function __construct(
Context $context,
MagentoFrameworkDataFormFormKey $formKey,
PageFactory $resultPageFactory
) {
parent::__construct($context);
$this->formKey = $formKey;
$this->resultPageFactory = $resultPageFactory;
}
/**
*
* @return MagentoFrameworkViewResultPage
*/
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 MagentoFrameworkViewResultPage $resultPage */
return $resultPage;
}
}