Magento Trying to set the session id to programmatically add to cart


I have set up a module where I programmatically add a product to the cart. I swear I had it working but recently found that it wasn't. The background on this is, our client has a third party product that a customer can go to a page on their site, look at the products that this third party has and add an item to our cart. The third party service is put on our page with an iframe. I pass them the information they need and they send back a response which I create a product and then add that product to cart.

Everything works just find except for the fact if a user has something else in their cart and then adds one of the products from the third party it wipes them from the cart. I know this is a session thing and I was already setting the sessionId because that is one of the parameters that I set and pass to the third party and they send back. Here is an example of my code.

$checkoutSession = Mage::getSingleton('core/session');
$checkoutSession->setSessionId($sessionId); // which is my session ID I get back from the third party and is the customers session id
$product = Mage::getSingleton('catalog/product');
$check = $product->isSalable();
$cart = Mage::getModel('checkout/cart');
try {
    $cart->addProduct($product, array('qty' => $qty));
catch (Exception $ex) {
    //Handle the error

You would think this is all you need but it doesn't work. I thought it was working at one time but I guess not. If i debug and say getSessionId() it looks correct but if you look at the session object it still says in the visitor_data array that the sessionId is the old one.

3/15/2011 5:31:33 AM

Try this:

Mage::getSingleton('core/session', array('name'=>'frontend'));
8/27/2016 12:46:02 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow