Question

How to add a product to cart is what I have already working, but it seems to be working for the default store only. So when I add a product to cart with the script on the default website/store, the products get added to the cart, but if I do it on another store, the cart remains empty (without any error).

Here's the add to cart script I have so far:

require_once($_SERVER['DOCUMENT_ROOT'] . '/app/Mage.php');
umask(0);
Mage::app()->setCurrentStore(Mage::app()->getStore()->getStoreId());

$session = Mage::getSingleton('core/session', array('name'=>'frontend'));
$cart = Mage::helper('checkout/cart')->getCart();

if( $_REQUEST['params_1'] != '' ) {
    try {
        $params_1 = unserialize($_REQUEST['params_1']);

        $productcart = new Mage_Catalog_Model_Product();
        $productcart = $productcart->load($params_1['product']);
        $cart->addProduct($productcart, $params_1);
        $session->setLastAddedProductId($productcart->getId());
    } catch(Exception $e) {
        echo Mage::helper('catalog')->__($e->getMessage());
    }
}

$cart->save();

Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

$_REQUEST['params_1'] holds the bundle product configuration as an array, which seems correct as it's working on the default store.

Do I need call $cart = Mage::helper('checkout/cart')->getCart(); different in order to reflect another store?

Was it helpful?

Solution 2

Okay, I do not know exactly what resolved it, but I solved the problem by taking these two steps:

  1. I was using jQuery.ajax() to make an AJAX call to my Magento ajaxController that handles my add to cart script. That was a single .php file, but I added the code to my ajaxController module.

  2. The url parameter for the AJAX call was relative, as in url: "/myhelper/ajax/add_to_cart_final_ajax/", now I wrap the URIs with the getUrl() helper function like so: url: "<?php echo $this->getUrl('myhelper/ajax/add_to_cart_final_ajax') ?>"

Thanks Fabian Blechschmidt for your help and patience anyway!

OTHER TIPS

The cart is a represantetion of a quote. A quote is attached to a store, so just changing the store ID might not be enough if the cart/quote is already attached to the wrong store.

Check the storeId of the cart/quote and change it, this might help.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top