3

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 1.9.2.2 into it. The magento installation runs as baseurl 127.0.0.1

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

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

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

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

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

$cart->getItemsQty();

It shows the right amount of items.

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

Mage::helper('checkout/url')->getCheckoutUrl()

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

http://127.0.0.1/project/store/index.php/checkout/onepage/?SID=5068c0335528f0713360cc3d6b302f8481ebb732

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 '16 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 '16 at 10:39
  • @lagiacrux Did you achieve what you want? – Gem Jun 30 '18 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 '18 at 13:31
0

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

Mage::app()

instead of:

Mage::init()

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

  • thx for the reply. unfortunatly it didnt help.. – lagiacrux Mar 11 '16 at 10:39
0

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.

0

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, privacy policy and cookie policy

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