0

(Magento 1.9) I created a form to be able to customize a product and generate a preview sketch of what the end result would look like with a cost (this was done outside of Magento as an individual program). Now I integrated this into a Magento page in a custom frontend template.

Now I am trying to add this to the cart. Considering each customer might order several versions of the product, each with its unique details, I was experimenting with creating a new simple product for each version (it seems that if I try to add them as the same product, magento groups them together with the same price), before adding it to the cart. However, I am having trouble actually adding the product into the cart. This is my code so far for adding to cart:

<?php 
// Mage init
require_once Mage::getBaseDir() . '/app/Mage.php';
umask(0);
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    
$productInstance = Mage::getModel('catalog/product')->load(Mage::getModel('catalog/product')->getResource()->getIdBySku('testsku17'));

$param = array(
    'product' => $productInstance->getId(),
    'qty' => 1
);
$request = new Varien_Object();
$request->setData($param);
$cart->addProduct($productInstance, $request);  // This line tends to break my code regularly (meaning everything halts when I leave it in)

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

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

echo "added to cart <br>";
?>

Can anyone help me figure out what I'm doing wrong?

2 Answers 2

0

Try following way:

// Mage init
include_once dirname(__FILE__). '/app/Mage.php';
umask(0);
Mage::app();

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
$product = Mage::getModel('catalog/product')->load(Mage::getModel('catalog/product')->getIdBySku('acj005'));

$param = array(
    'qty' => 1
);

$request = new Varien_Object();
$request->setData($param);
$cart->addProduct($product, $request);  // This line tends to break my code regularly (meaning everything halts when I leave it in)

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

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

echo "added to cart 
";
1
  • This adds the product to cart, but every instance is a copy of the default. I need each instance to be separate with different settings (eg. price, size, etc) and be able to add a link or comments visible from the back-end.... Also, when I open the cart, it adds an instance of the product (ie. if I added a quantity of 3, when I open the cart I will have 4)
    – NRW
    Feb 27, 2017 at 20:29
0

If you want add product to cart with custom options then you can try the below code:

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();
$productid=1;
$qty=1;
$options = array("1"=>"test","2"=>"44");
// Add a product with custom options
$productInstance = Mage::getModel('catalog/product')->load($productid);
$param = array(
    'product' => $productInstance->getId(),
    'qty' => $qty,
    'options' =>$options 
);
$request = new Varien_Object();
$request->setData($param);
$cart->addProduct($productInstance, $request);

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

// save the cart
$cart->save(); 
1
  • It did not work. Is there something that needs to be previously configured for your code to work? What is it? I thought the only thing I needed was a product with ID=1. But I have that and when I ran your code, the page broke.... (meaning that the layout gets all screwy and everything I print/echo before the code now does not show)
    – NRW
    Feb 27, 2017 at 19:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.