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 a开发者_StackOverflow社区nd 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.
Try this:
Mage::getSingleton('core/session', array('name'=>'frontend'));
Throw this in the index.php at the root of magento
header('P3P: CP="CAO PSA OUR"');
精彩评论