I was thinking about a feature for repeat orders. By repeat orders I mean that I want the customer to eventually click a link that will add the products in the previous order and redirect to cart.

EDIT: As all payments are done externally with credit cards this should just fill the cart with the product and the details. Not to place the actual order

I came across this code in order to add products to cart. However when I run it, none is added.

include '../mydir/mydir/app/Mage.php';
// Need for start the session
Mage::getSingleton('core/session', array('name' => 'frontend'));
try {
    $product_id = '52'; // Replace id with your product id
    $qty = '4'; // Replace qty with your qty
    $product = Mage::getModel('catalog/product')->load($product_id);
    $cart = Mage::getModel('checkout/cart');
    $cart->addProduct($product, array('qty' => $qty));
    Mage::getSingleton('core/session')->addSuccess('Product added successfully');
    header('Location: ' . 'index.php/checkout/cart/');
} catch (Exception $e) {
    echo $e->getMessage();

It redirects at the end so I don't get any exception. I run this from a sub-domain.

  • Or could it be the somdomain that causes this. Magento public website is in subdomain1.mydomain.com and I runt this script from subdomain2.mydomain.com Could it be some sort of session issue? Reason I'm running this from subdomain is of the Magento rewrite for the root catalog for PHP files. – Andreas Jun 16 '15 at 7:03
  • Think it was related to the subdomain. I created a module and assigned this to a function and it works. – Andreas Jun 16 '15 at 11:34