i've an external php file called addtocart.php who is located in my magento root folder. with this file i want to add a product to the cart. the product should have the following attributes:

sku name options price(!!) qty

EDIT: this is a screenshot of my magento folder structure: https://www.dropbox.com/s/vmli0973iflfski/Screenshot%202014-02-12%2009.44.39.png

this is my code: (only with qty and sku)


$image = "uploads/52f7857f039b2.jpg";
// the ID of the product
$product_id  = 149;

$product     = Mage::getModel('catalog/product')->load($product_id);

$cart = Mage::getModel('checkout/cart');
$params = array(
    'product' => $product_id,
    'qty' => 1,
    'options' => array(
        149 => array(
                'quote_path' => $image,
                'secret_key' => substr(md5(file_get_contents(Mage::getBaseDir() . $image)), 0, 20)),

$cart->addProduct($product, $params);


it does not work! can anybody tell me how to do this? Thank you! :)

  • have you tried changing $cart = Mage::getModel('checkout/cart'); in $cart = Mage::getSingleton('checkout/cart'); ?
    – Kurdt94
    Feb 12, 2014 at 11:34
  • yes, it does not work... do you have any idea how add products to the cart from a external php file?
    – dr1987
    Feb 12, 2014 at 12:11
  • This will make no difference. In this context it will behave the same as getModel.
    – Marius
    Feb 12, 2014 at 12:11

3 Answers 3


The code looks pretty strait forward (and works locally with a sample configurable product). My guess is that you have a mal-formed $params array for those custom options.

It expects something along the lines of this...

$params = array(
    'product' => 1,
    'qty' => 1,
    'options' => array(
        26 => "value",
        42 => "another value here",
        71 => "yav"

What kind of custom option type is it that requires the value to be an array?


This might help:

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

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

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

// 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();
$cart->addProduct($productInstance, $param);            

// update session

// save the cart

Source: https://stackoverflow.com/questions/21867045/magento-1-8-add-product-to-cart-using-php


I think you need to look at the require call:


The screenshot indicates addtocart.php is in the produktkonfigurator folder, so I would expect it to be something like:


As the app folder is one level up from addtocart.php's current directory.

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.