0

I'm trying to add to cart programmatically from store 2, but it's adding products to store 1. How can I force it to add to the correct store?

This is the code I've got in my CMS page.

include_once "app/Mage.php";
umask(0);
Mage::app();
Mage::getSingleton('core/session');

$product_id = '1658';
$qty = '1';
$product = Mage::getModel('catalog/product')->load($product_id);
$cart = Mage::getSingleton('checkout/session')->getQuote();
$cart->addProduct($product, $qty);
$cart->save();
Mage::getSingleton('core/session')->addSuccess('Art Hanging System added to cart successfully. Enjoy!');

I've tried using Mage::app()->setCurrentStore(2), but this ends up adding the success message to the 1st store still and not adding the product to either site's cart.

I've tried every way I can find or think of, but as soon as I try to specify the 2nd store, then nothing happens.

How can I specify which site's cart I'm trying to work with?

2
  • In your script, Mage::getSingleton('checkout/session')->getQuote() creates a new quote with the current store scope. Try setting the store_id on the quote directly by doing $cart->setStoreId(2) before saving it.
    – laketuna
    Commented Aug 17, 2015 at 14:35
  • Please accept your answer if that's the solution to mark this question as solved :)
    – Moose
    Commented Aug 19, 2015 at 8:17

2 Answers 2

1

Try setting the store scope when declaring your $cart variable, as below:

$cart = Mage::getSingleton('checkout/session')->setStoreId(2)->getQuote();

This forces Magento to load the quote object from store with ID 2 (or whatever you put between the brackets).

1

It appears this must have been an issue with how I was initiating my custom PHP page.

I found a way to make this work by creating a CMS page with a call to a template:

<p>{{block type="core/template" template="hanging/click-rail-configurator.phtml"}}</p>

Where my template file, 'hanging/click-rail-configurator.phtml' is pointing to the file in my template path, which for me is /app/design/frontend/rwd/default/template/hanging/click-rail-configurator.phtml.

Then, in my template file, I am using the following code:

<?php 
try {
    $product_ids  = $_REQUEST['productsArray'];
    $qty = array('1','2','3');
    $n = 0;

    $cart = Mage::getSingleton('checkout/session')->getQuote();
    foreach($product_ids as $product_id) {
        $product = Mage::getModel('catalog/product')->load($product_id);
        $cart->addProduct($product, $qty[$n]);
        $n++;
    }
    $cart->save();
    Mage::getSingleton('customer/session')->setCartWasUpdated(true);
    Mage::getSingleton('core/session')->addSuccess('Art Hanging System added to cart successfully. Enjoy!');
} catch (Exception $e) {
    Mage::log($e);
}
?>

$products_ids is an array being passed in by jQuery's post function and everything is working as expected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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