15

I'm making a bulk add to cart system. Please note : i want it to works for simple products with with custom options -> Where custom option are like color(red , green , blue) or Size(Xl, M , S)

Suppose a person wants to order below items:

  1. product A, red color, qty 12
  2. Product A, green color, qty 18
  3. Product B, XL, qty 3
  4. Product C, qty 10

So I want to add these 4 items by code/programmatically at once. How can I do this? Is it possible via query string, or any controller or built in function for that? It doesn't have to be a single query or one function call only per see...

  • yeah exactly how can i do this – user1799722 Apr 17 '15 at 8:08
  • what type of product are you using? – Amit Bera Apr 20 '15 at 11:30
  • @AmitBera i am using simple products – user1799722 Apr 22 '15 at 5:56
1

So to add products to cart programmatically is fairly simple you just need to product object and the cart session.

$quote = Mage::getSingleton('checkout/session')->getQuote();
$quote->addProduct($product, $qty);

$quote->collectTotals()->save();

This because a bit more difficult when adding configurable or products with options but all you need to do is load the product object with the right options.

Now what you need to do is pass an array instead of $qty and this array should be formatted in a different way depending on what type of product you are adding.

See the following for more information:

  • thanks i just want to add simple objects with attributes pls reread my question – user1799722 Apr 20 '15 at 6:46
  • 1
    @mour so you can add products via url to the cart but I dont think it will work with multiple products so I suggest building your own controller as in my answer to add multiple products. – David Manners Apr 21 '15 at 7:40
1

Heres a method i used a while back:

// Products array
$productArray = array(
    // Simple product
    array(
        'product_id' => 1,
        'qty' => 1
    ),
    // Configurable product
    array(
        'product_id' => 4,
        'qty' => 1,
        'options' => array(
            'color' => 'Red'
        )
    )
);

// Prepare cart products
$cartProducts = array();
foreach ($productArray as $params) {
    if (isset($params['product_id'])) {
        // Load product
        $product = Mage::getModel('catalog/product')->load($params['product_id']);

        if ($product->getId()) {
            // If product is configurable and the param options were specified
            if ($product->getTypeId() == "configurable" && isset($params['options'])) {
                // Get configurable options
                $productAttributeOptions = $product->getTypeInstance(true)
                    ->getConfigurableAttributesAsArray($product);

                foreach ($productAttributeOptions as $productAttribute) {
                    $attributeCode = $productAttribute['attribute_code'];

                    if (isset($params['options'][$attributeCode])) {
                        $optionValue = $params['options'][$attributeCode];

                        foreach ($productAttribute['values'] as $attribute) {
                            if ($optionValue == $attribute['store_label']) {
                                $params['super_attribute'] = array(
                                    $productAttribute['attribute_id'] => $attribute['value_index']
                                );
                            }
                        }
                    }
                }
            }

            unset($params['options']);
            $cartProducts[] = array(
                'product'   => $product,
                'params'    => $params
            );

        }
    }
}

// Add to cart
$cart = Mage::getModel("checkout/cart");
if (!empty($cartProducts)) {
    try{
        foreach ($cartProducts as $cartProduct) {
            $cart->addProduct($cartProduct['product'], $cartProduct['params']);
        }

        Mage::getSingleton('customer/session')->setCartWasUpdated(true);
        $cart->save();
    } catch(Exception $e) {
        Mage::log($e->getMessage());
    }
}

Its pretty straight forward and tested right now to be working.

I've include 2 products in $productArray, one simple and the other configurable. Obviously you could add more and if the configurable had 2 options like size and color, you could just add the additional in the options array.

  • hii thanks i want to works for simple products with with custom options – user1799722 Apr 22 '15 at 5:58
  • So comment out the line "unset($params['options']);" then make sure the product has the specified product options – Shaughn Apr 22 '15 at 8:51
1

Besides that using simple products with custom options is imho pretty much not the way to use "size" and "color" in magento, I managed to add products with custom options to the cart like this:

/*
 * Assuming this is inside a method in a custom controller
 * that receives a $_POST
 */
$post = $this->getRequest()->getPost();

// load the product first
$product = Mage::getModel('catalog/product')->load($post['product_id']);
$options = $product->getOptions();

// this is the format for the $params-Array
$params = array(
    'product' => $product->getId(),
    'qty' => $post['qty'],
    'related_product' => null,
    'options' => array()
);
// loop through the options we get from $_POST
// and check if they are a product option, then add to $params
foreach( $post as $key => $value ) {
    if(isset($options[$key]) {
        $params['options'][$key] = $value; 
    }
}

// add the product and its options to the cart
$cart->addProduct($product, $params);

Is this what you meant? If you want to add multiple products, just repeat this process for each product you want to add. The key factor is always to have the product_id, qty and options given via $_POST.

1

You can add mulitple simple products with custom options by overwriting the Cart Controller like this:

I have placed the CartController.php file here: https://github.com/svlega/Multiple-Products-AddtoCart

        //Programatically Adding multiple products to cart
        $productArray = array(
            array(
                'product_id' => 7,
                'qty' => 2,
                'custom_options' => array(
                    'size' => 'XL'
                )
            ),
            array(
                'product_id' => 1,
                'qty' => 1,
                'custom_options' => array(
                    'color' => 'Red'
                )
            )   

        );

        // Prepare cart products
        foreach ($productArray as $params) {
            if (isset($params['product_id'])) {
                // Load product
                $product = Mage::getModel('catalog/product')->load($params['product_id']);

                if ($product->getId()) {
                    // If product is configurable and the param options were specified
                    if (isset($params['custom_options'])) {
                        // Get options                
                        $options = $product->getOptions();
                            foreach ($options as $option) {
                                /* @var $option Mage_Catalog_Model_Product_Option */                        
                                if ($option->getGroupByType() == Mage_Catalog_Model_Product_Option::OPTION_GROUP_SELECT) {                          

                                    $product_options[$option->getTitle()] = $option->getId();
                                    if(array_key_exists($option->getTitle(),$params['custom_options'])){
                                    $option_id =  $option->getId();                 
                                        echo '<br>Did'.$id = $option->getId().'Dlabe'.$option->getTitle();
                                    foreach ($option->getValues() as $value) {                          
                                        /* @var $value Mage_Catalog_Model_Product_Option_Value */                    
                                       if($value->getTitle()== $params['custom_options'][$option->getTitle()]){     
                                echo 'id'.$id = $value->getId().'labe'.$value->getTitle();
                                       $params['options'][$option->getId()]=$value->getId();
                                       }                                
                                    }
                                    }                          
                            }
                            }
                    }

                    try{
                    $cart = Mage::getModel('checkout/cart');
                    $cart->addProduct($product, $params);
                    $cart->save();
                    }catch(Exception $e) {
                    Mage::log($e->getMessage());
                    }

                }
            }
        }

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.