I'm having this problem and getting pretty annoyed by it.

here's the situation.

I was running a website on the codeigniter framework, and our customer wanted to add a shop to sell a couple of products. I decided to try to use magento, since its widely used and the documentation seemed ok.

My test setup on my local machine looks like this.

"localhost/project/" is the website root, where the codeigniter installation files are.

I added a new folder "store" to the root and installed magendo into it. The magento installation runs as baseurl

Now i tried to add products from a codeigniter library to a magento cart via the following code:

require_once 'store/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,
$request = new Varien_Object();
$cart->addProduct($productInstance, $param);

// update session

// save the cart

Seemed pretty straigforward. no errors or anything. I can verify that the adding works by


It shows the right amount of items.

But whenever I try to switch to the cart in the store itself using


It is empty.

The interesting part is, I can get all the info I need from magento, but I cant seem to add anything to a cart.

Im guessing it has something to do with sessions getting mixed up.

EDIT: Mage::helper('checkout/url')->getCheckoutUrl() delivers something like

where as 5068c0335528f0713360cc3d6b302f8481ebb732 is the codeigniter session id. so im guessing the code never creates a magento session at all and links to a cart that with the CI session and not a magento session?

EDIT2: i think im getting closer to the root of the problem. after printing the data for the magento session with echo print_r($session->getData());

im getting the following

Array ( [_session_validator_data] => Array ( [remote_addr] => ::1 [http_via] => [http_x_forwarded_for] => [http_user_agent] => Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36 ) [cart_was_updated] => 1 [id] => ) 1

the curious part for me is that the 'id' is empty. so i think the session is just not created correctly.

  • Did you try accessing the cart from codeigniter too ? My guess is it would be something like the session you create is not shared with Magento, and it is creating another one when you visit the URL.
    – hellimac
    Mar 11, 2016 at 10:23
  • @hellimac i followed up on your idea and you seem to be right. if i inspect the codeigniter session id "ci_session" and the "frontend" id they differentiate. but i CAN get the cart items from my codeigniter side. no idea what could cause this though.
    – lagiacrux
    Mar 11, 2016 at 10:39
  • @lagiacrux Did you achieve what you want?
    – Gem
    Jun 30, 2018 at 4:29
  • @Rathinam no, if i remember correctly (it was 7 years ago), i stopped using magento and coded the shop application myself in php.
    – lagiacrux
    Jul 2, 2018 at 13:31

3 Answers 3


If you want to make use of magento sessions while "outside magento" then you should use:


instead of:


I had the same issue and this is how I solved it.

  • thx for the reply. unfortunatly it didnt help..
    – lagiacrux
    Mar 11, 2016 at 10:39

You need a formkey to add products to your cart. Try to get the formkey like this Mage::getSingleton('core/session')->getFormkey. This is needed in Magento 1.8 and higher.


I would suggest to override cart controller addAction function and comment form key check then just have link from code igniter like http://host/ checkout/cart/add/product/id-of-the-product . up on click on the link will add the product to cart.

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.