i have a problem with Magento's addProduct() function. I have the following code:
<?php
// Mage init
include_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 with custom options
$productId = 11348;
$productInstance = Mage::getModel('catalog/product')->load($productId);
$param = array(
'product' => $productInstance->getId(),
'qty' => 1,
'options' => array(
528 => '1756', // Custom option with id: 528
527 => '1753', // Custom option with id: 527
526 => '1751' // Custom option with id: 526
)
);
$request = new Varien_Object();
$request->setData($param);
$cart->addProduct($productInstance, $request);
// update session
$session->setCartWasUpdated(true);
// save the cart
$cart->save();
?>
It worked yesterday so include and $param are rigth but now it doesn't work. You also can add this product to cart inside the shop so the product exist and it's in stock. This code doesn't seems to have any error but it doesn't add product to cart.
Thanks for help.
<?php
require_once('app/Mage.php');
umask(0);
Mage::app('admin');
$product_model = Mage::getModel('catalog/product');
$my_product_sku = 'test';
$my_product_id = $product_model->getIdBySku($my_product_sku);
$my_product = $product_model->load($my_product_id);
$qty_value = 13;
$cart = Mage::getModel('checkout/cart');
$cart->init();
$cart->addProduct($my_product, array('qty' => $qty_value));
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
?>
try with adding form key
and uenc
field
$param = array(
'product' => $productInstance->getId(),
'form_key'=>$form_key_put_here,
'uenc' =>Mage::app()->getRequest()->getParam('uenc', 1),
'qty' => 1,
'options' => array(
528 => '1756', // Custom option with id: 528
527 => '1753', // Custom option with id: 527
526 => '1751' // Custom option with id: 526
));
hope this will help.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.