dan.codes
dan.codes

Reputation: 3523

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 fine, 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');
$product->load($productId);
$check = $product->isSalable();
$cart = Mage::getModel('checkout/cart');
$cart->init();
try {
    $cart->addProduct($product, array('qty' => $qty));
    $cart->save();
}
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.

Upvotes: 3

Views: 7573

Answers (2)

seanvalencourt
seanvalencourt

Reputation: 1226

Try this:

Mage::getSingleton('core/session', array('name'=>'frontend'));

Upvotes: 1

numerical25
numerical25

Reputation: 10790

Throw this in the index.php at the root of magento

header('P3P: CP="CAO PSA OUR"');

Upvotes: 0

Related Questions