4

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)

require_once('app/Mage.php');    
umask(0);
Mage::app('de');


$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');
$cart->init();
$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);
$cart->save();


Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

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

3
  • have you tried changing $cart = Mage::getModel('checkout/cart'); in $cart = Mage::getSingleton('checkout/cart'); ? Commented 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? Commented Feb 12, 2014 at 12:11
  • This will make no difference. In this context it will behave the same as getModel. Commented Feb 12, 2014 at 12:11

3 Answers 3

2

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?

2

This might help:

// 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 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, $param);            

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

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

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

1

I think you need to look at the require call:

require_once('app/Mage.php'); 

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

require_once('../app/Mage.php');

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.