[英]magento 1.8 add product to cart using php
Up until Magento 1.7 I was able to use the following code to add a product to cart programatically: 直到Magento 1.7我能够使用以下代码以编程方式将产品添加到购物车:
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();
However, this does not work anymore in Magento 1.8. 但是,这在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. 可悲的是,我没有找到任何关于这个问题的线索。
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; 它可能与1.8中的更改有关,这也导致在使用URL方法时需要表单键; this is a wild guess though. 这是一个疯狂的猜测。
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): 仅供任何人参考,这是我正在使用的代码(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();
Add this code in controller and Send product id from form 在控制器中添加此代码并从表单发送产品ID
public function buyAction()
{
$sksku = $_REQUEST['skusky'];
$_sku = "sample-".$sksku;
$id = Mage::getModel('catalog/product')->getIdBySku($_sku);
if ($id){
$_catalog = Mage::getModel('catalog/product');
$_productId = $_catalog->getIdBySku($_sku);
$_product = Mage::getModel('catalog/product')->load($_productId);
$id = $_product->getEntityId();
$form_key = Mage::getSingleton('core/session')->getFormKey();
$params = array('form_key'=>$form_key,'qty'=>1);
$product = Mage::getModel('catalog/product')
->setStoreId(
Mage::app()
->getStore()
->getId()
)
->load($id);
$cart = Mage::helper('checkout/cart')->getCart();
$cart->addProduct($product, $params);
$cart->save();
$this->_redirect('checkout/cart');
Mage::getSingleton('core/session')->addSuccess('Your product has been added to cart.');
}
else{
$this->_redirectReferer();
Mage::getSingleton('core/session')->addError('Sorry!!! No sample color option available for this product.');
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.