0

Here are two php files you can run in the browser that should add multiple products to the cart by product id. The first one (mine) tries to be efficient and programmatic and doesn't work. :P The second one (a coworker created for me) is a manually generated array, and it creates models inside of a for loop. Naturally it works. here's the code below:

1.

<?php
/**
* code for adding products to cart for testing on dev site
* author: Matt Mathis
*/
include 'app/Mage.php';
Mage::app();
// Needed to start the session
Mage::getSingleton('core/session', array('name' => 'frontend'));
try {
    $product_ids = range(1900, 2050); //array full of arange of product id's on DTPM...
    $qty = '10'; // Replace qty with whatever you want to add to the cart
    $mage_product = Mage::getModel('catalog/product');
    $cart = Mage::getSingleton('checkout/cart');
    $cart->init();
    for($i = 0; $i < count($product_ids); $i++) {
        $product = $mage_product->load($product_ids[$i]);
        if($product->getId()) {
            $cart->addProduct($product, array('qty' => $qty)); //, 'product_id' => $product->getId()));
            Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
        }
    }
    $cart->save();
    Mage::getSingleton('core/session')->addSuccess('Products added successfully');
} catch (Exception $e) {
    echo $e->getMessage();
}
?>

2.

<?php
namespace Addtocart;

use Mage;

require __DIR__ . '/app/Mage.php';
$data = array(
    array('id' => 1900, 'qty' => 1),
    array('id' => 1901, 'qty' => 1),
    array('id' => 1902, 'qty' => 1),
    array('id' => 1903, 'qty' => 1),
    array('id' => 1904, 'qty' => 1),
    array('id' => 1905, 'qty' => 1),
    array('id' => 1906, 'qty' => 1),
    array('id' => 1907, 'qty' => 1),
    array('id' => 1908, 'qty' => 1),
    array('id' => 1909, 'qty' => 1),
    array('id' => 1910, 'qty' => 1),
    array('id' => 1911, 'qty' => 1),
    array('id' => 1912, 'qty' => 1),
    array('id' => 1913, 'qty' => 1),
    array('id' => 1914, 'qty' => 1),
    array('id' => 1915, 'qty' => 1),
    array('id' => 1916, 'qty' => 1),
    array('id' => 1917, 'qty' => 1),
    array('id' => 1918, 'qty' => 1),
    array('id' => 1919, 'qty' => 1),
    array('id' => 1920, 'qty' => 1),
    array('id' => 1921, 'qty' => 1),
    array('id' => 1922, 'qty' => 1),
    array('id' => 1923, 'qty' => 1),
    array('id' => 1924, 'qty' => 1),
    array('id' => 1925, 'qty' => 1),
    array('id' => 1926, 'qty' => 1),
    array('id' => 1927, 'qty' => 1),
    array('id' => 1928, 'qty' => 1),
    array('id' => 1929, 'qty' => 1),
    array('id' => 1930, 'qty' => 1),
    array('id' => 1931, 'qty' => 1),
    array('id' => 1932, 'qty' => 1),
    array('id' => 1933, 'qty' => 1),
    array('id' => 1934, 'qty' => 1),
    array('id' => 1935, 'qty' => 1),
    array('id' => 1936, 'qty' => 1),
    array('id' => 1937, 'qty' => 1),
    array('id' => 1938, 'qty' => 1),
    array('id' => 1939, 'qty' => 1),
    array('id' => 1940, 'qty' => 1),
    array('id' => 1941, 'qty' => 1),
    array('id' => 1942, 'qty' => 1),
    array('id' => 1943, 'qty' => 1),
    array('id' => 1944, 'qty' => 1),
    array('id' => 1945, 'qty' => 1),
    array('id' => 1946, 'qty' => 1),
    array('id' => 1947, 'qty' => 1),
    array('id' => 1948, 'qty' => 1),
    array('id' => 1949, 'qty' => 1),
    array('id' => 1950, 'qty' => 1),
);
// foreach (range(1900, 2050) as $id) {

//    $data = array(
//        array('id' => $id, 'qty' => 1),

//    );
// }
Mage::app();
Mage::getSingleton('core/session', array('name' => 'frontend'));
$cart = Mage::getSingleton('checkout/cart');
$cart->init();
$errors = array();
foreach ($data as $product) {
    try {
        $product_model = Mage::getModel('catalog/product');
        $productObj = $product_model->load($product['id']);
        if ($productObj->getId()) {
            $params = array(
                'product' => $productObj->getId(),
                'qty'     => $product['qty']
            );
            $cart->addProduct($productObj, $params);
        }
    } catch (\Exception $e) {
        $errors[$product['id']] = $e->getMessage();
        continue;
    }
}
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
if ($errors) {
    var_dump($errors);
    exit;
}
$url = Mage::getUrl('checkout/cart', true);
header('Location: ' . $url);
?>

Any thoughts on why this is the case, I've searched around and can't find anything that says 1 shouldn't work.

  • 1
    In your version (1) try to call the reset() method before the load() on $mage_product; this will ensure the entity is brought back to a clear state before new data from another entity is loaded; I didn't try it so I can't be sure it will solve your issue. – Alessandro Ronchi Jan 15 '16 at 23:30
1

You just move the following line of code inside the loop:

$mage_product = Mage::getModel('catalog/product');

so it looks like:

...
for($i = 0; $i < count($product_ids); $i++) {
    $mage_product = Mage::getModel('catalog/product');
    $product = $mage_product->load($product_ids[$i]);
    ...

This way product model will be re-initialized each iteration.

| improve this answer | |
  • 2
    Actually with $product->reset() you'll also re-initialized the object – Sander Mangel Jan 16 '16 at 8:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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