magento 1.8 使用 php 将产品添加到购物车

magento 1.8 add product to cart using php(magento 1.8 使用 php 将产品添加到购物车)
本文介绍了magento 1.8 使用 php 将产品添加到购物车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

在 Magento 1.7 之前,我能够使用以下代码以编程方式将产品添加到购物车:

Up until Magento 1.7 I was able to use the following code to add a product to cart programatically:

require_once '../app/Mage.php'; 
Mage::getSingleton('core/session', array('name' => 'frontend')); 
umask(0);  
Mage::app();  
$session = Mage::getSingleton('customer/session');  
$product = Mage::getModel('catalog/product')->load(99);  // Random product ID

// get cart and add product
$cart = Mage::getSingleton('checkout/cart'); 
$cart->init();
$cart->addProduct($product, 1);

// update session
$session->setCartWasUpdated(true);

// save the cart
$cart->save();  

然而,这在 Magento 1.8 中不再起作用.我一整天都在尝试/搜索为什么会这样.可悲的是,我还没有找到有关此问题的任何线索.

However, this does not work anymore in Magento 1.8. I've been trying/searching all day why this is the case. Sadly I haven't found any clue regarding this issue.

可能与1.8的变化有关,这也导致使用URL方法时需要form-key;不过,这是一个疯狂的猜测.

It may have to do with the changes in 1.8 which also causes the requirement for a form-key when using the URL method; this is a wild guess though.

在使用 Magento 1.8 时,有人知道如何做到这一点吗?

Anyone any idea or a working example on how to do this when working with Magento 1.8?

推荐答案

重新审视它并立即让它运行良好.仅供任何人参考,这是我正在使用的代码(Magento 1.8):

Had a fresh look at it and got it working nicely right away. Just for anyone's reference, here's the code I'm using (Magento 1.8):

// Mage init
require_once '../../app/Mage.php'; 
umask(0);  
Mage::init('default');
Mage::getSingleton('core/session', array('name' => 'frontend'));  

// Get customer session
$session = Mage::getSingleton('customer/session'); 

// Get cart instance
$cart = Mage::getSingleton('checkout/cart'); 
$cart->init();

// Add a product (simple); id:12,  qty: 3 
$cart->addProduct(12, 3);

// Add a product with custom options
$productInstance = Mage::getModel('catalog/product')->load($productId);
$param = array(
    'product' => $productInstance->getId(),
    'qty' => 1,
    'options' => array(
        234 => 'A value'  // Custom option with id: 234
    )
);
$request = new Varien_Object();
$request->setData($param);
$cart->addProduct($productInstance, $request);

// Set shipping method
$quote = $cart->getQuote();
$shippingAddress = $quote->getShippingAddress();
$shippingAddress->setShippingMethod('flatrate_flatrate')->save();               

// update session
$session->setCartWasUpdated(true);

// save the cart
$cart->save(); 

这篇关于magento 1.8 使用 php 将产品添加到购物车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

In PHP how can you clear a WSDL cache?(在 PHP 中如何清除 WSDL 缓存?)
failed to open stream: HTTP wrapper does not support writeable connections(无法打开流:HTTP 包装器不支持可写连接)
Stop caching for PHP 5.5.3 in MAMP(在 MAMP 中停止缓存 PHP 5.5.3)
Caching HTTP responses when they are dynamically created by PHP(缓存由 PHP 动态创建的 HTTP 响应)
Memcached vs APC which one should I choose?(Memcached 与 APC 我应该选择哪一个?)
What is causing quot;Unable to allocate memory for poolquot; in PHP?(是什么导致“无法为池分配内存?在 PHP 中?)