简体   繁体   English

如何在Magento中以编程方式将产品添加到购物车

[英]How to add product to cart programmatically in Magento

I am unable to add product to cart programmatically in Magento , I have tried every possible technique to add product with quantity to cart ( in Magento), following are the things I have tried and forums/sites taken help from. 我无法在Magento中以编程方式将产品添加到购物车中,我尝试了所有可能的技术以将一定数量的产品添加到购物车(在Magento中),以下是我尝试过的事情,并从论坛/站点获得了帮助。 But none of them is working. 但是他们都不在工作。 I am using Magento Community edition 1.9.1.1. 我正在使用Magento社区版本1.9.1.1。 If anyone has any suggestion/answer, please share. 如果有人有任何建议/答案,请分享。

  • Tried to do it via URL - eg [Magento_Store_URL]/checkout/cart/add?product=[id]&qty=[qty] or even like this - [Magento_Store_URL]/checkout/cart/add/product/[id]/qty/[qty] . 尝试通过URL进行操作-例如[Magento_Store_URL]/checkout/cart/add?product=[id]&qty=[qty]或什至是这样- [Magento_Store_URL]/checkout/cart/add/product/[id]/qty/[qty] I have also tried with form_key , generated via Mage::getSingleton('core/session')->getFormKey(); 我还试图与form_key ,经由产生Mage::getSingleton('core/session')->getFormKey(); . All these stuff is not working at all. 所有这些东西根本不起作用。 These things are mentioned here - Magento website 这些东西在这里提到-Magento网站
  • Next, I have tried via programmatically, like this. 接下来,我已经像这样通过编程方式尝试了。

 <?php require_once 'app/Mage.php'; Mage::app(); $product=new Mage_Catalog_Model_Product(); $product->load(1); // 1 is product id, this is simple product ( type) $quote = Mage::getSingleton('checkout/session')->getQuote(); $quote->addProduct($product, 1 ); // quantity is 1 $cart = Mage::getSingleton('checkout/cart'); $cart->init(); // tried commenting this too! $cart->addProduct($product, 1 ); // quantity is 1 $cart->save(); Mage::getSingleton('checkout/session')->setCartWasUpdated(true); $quote->collectTotals()->save(); header('Location: '. 'http://localhost/magento/index.php/checkout/cart'); 

  • Also tried following. 还尝试了以下。

 <?php require_once 'app/Mage.php'; Mage::app(); $params=array( 'product'=>1, 'qty' => 3 ); $cart = Mage::getSingleton('checkout/cart'); $product = new Mage_Catalog_Model_Product(); $product->load($params["product"]); $cart->addProduct($product, $params); $cart->save(); Mage::getSingleton('checkout/session')->setCartWasUpdated(true); header('Location: '. 'http://localhost/magento/index.php/checkout/cart'); 

I have taken help from following websites. 我已从以下网站获得帮助。

Is there any wrong in my code, whatever I have tried or is there any setting issue with my Magento Installation? 我的代码有什么错误,我尝试过什么,还是Magento安装有任何设置问题?

Update 1 I have tried below from the answer proposed to this question , still that is not working. 更新1我已经从下面对这个问题提出的答案中尝试了,仍然没有用

 $formKey = Mage::getSingleton('core/session')->getFormKey(); $params = array( 'product' => 3, 'qty' => 2, 'form_key' => $formKey ); $product = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())->load($params['product']); $cart = Mage::helper('checkout/cart')->getCart(); $cart->addProduct($product, $params); $cart->save(); 

Update 2 更新2

This is working, if I create a controller and place all code there, not working - in a standalone page that refers /app/Mage.php . 如果我创建了一个控制器并将所有代码放置在此处,则无法正常工作-在引用/app/Mage.php的独立页面中。

Since 1.8 you won't be able to add a product to the cart from a GET request alone, as you need to provide the form_key. 从1.8开始,您将无法仅通过GET请求将产品添加到购物车,因为您需要提供form_key。

You should be able to add a product to the cart using the following: 您应该可以使用以下方法将产品添加到购物车:

form_key is the main thing to get right here. form_key是这里的主要内容。

$params //should include at least a valid form_key, qty

$product = Mage::getModel('catalog/product')
                    ->setStoreId(
                        Mage::app()
                        ->getStore()
                        ->getId()
                    )
                    ->load($product_id);

$cart = Mage::helper('checkout/cart')->getCart();
$cart->addProduct($product, $params);
$cart->save();

Take a look at the addProduct function in app\\code\\core\\Mage\\Checkout\\Model\\Cart.php if you need to debug (and then in the app\\code\\core\\Mage\\Sales\\Model\\Quote.php too) 如果需要调试,请查看app \\ code \\ core \\ Mage \\ Checkout \\ Model \\ Cart.php中的addProduct函数(然后也可以在app \\ code \\ core \\ Mage \\ Sales \\ Model \\ Quote.php中)

Here is the easy way to add product to cart/quote: 这是将产品添加到购物车/报价的简单方法:

$customer = Mage::getModel('customer/customer')->load($customerId);
$product = Mage::getModel('catalog/product')->load($productId);
// load quote by customer and store...
$quote = Mage::getModel('sales/quote')->setStore($storeId)->loadByCustomer($customerId);
$quote->addProduct($product, 1);
$quote->setIsActive(1);
$quote->collectTotals()->save();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM