donglv9813 2015-06-12 13:45
浏览 49
已采纳

如何在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. But none of them is working. I am using Magento Community edition 1.9.1.1. If anyone has any suggestion/answer, please share.

  • Tried to do it via URL - e.g. [Magento_Store_URL]/checkout/cart/add?product=[id]&qty=[qty] or even like this - [Magento_Store_URL]/checkout/cart/add/product/[id]/qty/[qty]. I have also tried with form_key, generated via Mage::getSingleton('core/session')->getFormKey();. All these stuff is not working at all. These things are mentioned here - Magento website
  • 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?

Update 1 I have tried below from the answer proposed to this question, still that is not working.

$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

This is working, if I create a controller and place all code there, not working - in a standalone page that refers /app/Mage.php .

</div>
  • 写回答

2条回答 默认 最新

  • doudu4282 2015-06-13 17:38
    关注

    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.

    You should be able to add a product to the cart using the following:

    form_key is the main thing to get right here.

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号