Saturday, April 9, 2011

Programmatically add configurable products to Magento Shopping Cart

Hello Guys, 


Here we have another Magento blog. Recently I was working on building a quick shop module for magento. Its like customer can quickly search products based on some attributes. All the products with matching criteria will be displayed with their options. Customer can select product options and enter quantity and those products get added to shopping cart manually. Here all the products will be configurable products. So I created a custom module for it. After relevant products are displayed all the data like product id, quantity, option id and option values will be sent to cart controller page. This is the page where we need to write code to add product in the cart.


There are two way to do this. Following are the two options.


Option 1 : Via query string


Following is the URL format to add product to shopping cart.


$route  = "http://www.your_domain.com/checkout/cart/add?product=272&qty=2&super_attribute[22]=30";


header("Location : ".$route);


Here 272 is the product entity id displayed in magento admin panel. 22 is the attribute associated with this product and 30 is the selected value of that attribute.  



This will add product to the cart and redirect to cart page. Using this approach you can add a a single product to cart. What if we have more than one product. Like in my case customer can select more than one products and options. Following is the another option to add product manually.

Option 2 :


Following is the code to add product to shopping cart.



$params = array(
    'product' => 272,
    'super_attribute' => array(
        22 =>30 ,
    ),
    'qty' => 2,
);


$cart = Mage::getSingleton('checkout/cart'); 
$product = new Mage_Catalog_Model_Product();
$product->load(272); 
$cart->addProduct($product, $params);
$cart->save(); 
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);


Using above code you can add any number of products to the cart.
Above code will work for only configurable products having only one option. If configurable product has more option then all the option values should be specified.



7 comments:

  1. Hi Hiran,

    I am trying to create one script to create an order containing configurable product which has two attributes configurable i.e. color and size.

    Following is my long code that ideally creates order but in admin order detail,i cannot see product configurable options that were set while creating order. Please look at my code and let me know what i am missing,

    $product = Mage::getModel('catalog/product')->load($productId);

    //$product->setParentItemId(4);



    $attributeSize = $product->getResource()->getAttribute('size');
    $attributeColor = $product->getResource()->getAttribute('color');


    /*$buyInfo = array('qty' => 5 ,
    $attributeSize->getId() => 'S',
    $attributeColor->getId() => 'Blue',
    );
    $params = array(
    'product' => $productId,
    'super_attribute' => array(
    $attributeSize->getId() => 'Small',
    $attributeColor->getId() => 'Blue',

    ),
    'qty' => 2,
    ); */
    /*********************************************/
    $colorAttributeText=$product->getAttributeText('color');
    $sizeAttributeText=$product->getAttributeText('size');

    $colorValue = $product->color;
    $sizeValue = $product->size;

    $attribute_data=array('info_buyRequest'=>array(
    'product'=>$productId,
    'super_attribute'=>array(
    $attributeColor->getId() => $colorValue,
    $attributeSize->getId() => $sizeValue,
    )
    ),
    'attributes_info'=>array(
    '0'=>array(
    'label'=>'Color',
    'value'=>$colorAttributeText
    ) ,
    '1'=> array(
    'label'=>'size',
    'value'=>$sizeAttributeText
    ),
    ),
    'simple_name'=> $product->getName(),
    'simple_sku'=> $product->getSku(),
    'product_calculations' =>1,
    'shipment_type'=>0
    );

    /*********************************************/

    $quote->addProduct($product, new Varien_Object($attribute_data));

    ReplyDelete
  2. Referencing to above post,
    And when I looked at database there is creation of only one simple product sales_flat_order_item, where as there should be also configurable product.

    Note: I am running this script not root directory like http://localhost/magento/createOrder.php

    ReplyDelete
  3. what if i want to edit configurable products programatically.

    ReplyDelete
  4. I tried this solution with a product with a yes no option, in product view the price adjusts dynamically and is correctly added to the cart at the correct price, however with this solution when yes is selected a certificate is issued which costs a fixed additional price, how do i get the correct price to the cart? adding products with yes produces the default cost (and not default + certificate cost)

    ReplyDelete
    Replies
    1. my fault, the product i tested didn't have the cost added in the backend, works like a dream...

      Delete
  5. Hi hiren,

    I got some issue with simple configurable Products extension. Due to SCP extension selected custom options doesn't show up on cart page. I m so frustated. SO if possible give me any solution for that.

    The SCP extension i m using on magento 1.7.0.2..so please help me



    ReplyDelete
  6. how to update the super atributes of a cart item ?
    like the way we update qty?

    ReplyDelete