Skip to main content

Add Products to cart Programmatically in Magento

Sometimes we need to add products into cart programmatically in controller. Below is the code which you can use to add product to cart. 
1) For Simple Products
If you are working in magento controller and wants to add a simple product programmatically then here is the code: Suppose our product id value stored in $productId.
       
        // Below code will create instance of cart
        $cart = Mage::getModel('checkout/cart');
        // This will initialize cart
        $cart->init();
        // Get the product collection by passing product id
        $productCollection = Mage::getModel('catalog/product')->load($productId);
This product collection will be required as we have to pass it at the time of adding product to cart.
For adding the product we have to call addProduct() function. In this function we have to pass two parameters first is productcollection and second is an array. This array contains product id and quantity which we are going to add to cart.
       
         // Add product to cart 
         $cart->addProduct($productCollection, array( 'product_id' => $productId, 'qty' => 1);
As now we have added our simple product to cart. Now only one thing remainig and that is save the cart.
       
         // Save cart
        $cart->save(); 
Like this we can add a simple product to cart in Magento.

2) For Configurable Product

Suppose we want to add a configurable product which have custom options. In that case we will product id, option id and optoin value. Lets took an example for suppose our product is T-Shirt and its coming in color variant. Each color has its different option id and option value.
Same thing goes with configurable product also like create instance of cart and then initialize it and create product collection.
       
        // Get cart and product collection
        $cart = Mage::getModel('checkout/cart');
        $cart->init();
        $productCollection = Mage::getModel('catalog/product')->load($productId);
Now the part came where we need to add product to cart which is different from simple type product. Here in array we have to pass options array also. This options array contains the selected option id and option value for product.
       
        // Add configurable product to cart 
        $cart->addProduct($productCollection , 
                                    array( 'product_id' => $productId,
                                             'qty' => 1,
                                             'options' => array( $optionId => $optionValue));
Like this we can add configurable product to cart. Atlast we need to save the cart.
       
        // Save cart 
        $cart->save(); 
That's it done with adding configurable product. This is the code for adding product.


Author: Mohit Verma

Comments

  1. usefull link:
    http://codegento.com/2011/02/grids-and-forms-in-the-admin-panel/

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. hello,

    Can you let us know how can i add product with custom option in magneto by soap api.

    ReplyDelete
    Replies
    1. Hi, Appologies for late reply I have written blog about adding products with custom option but by soap api. Need to do some research for it.

      Delete
  4. Thanks Andie !!! surely i will visit your suggested link

    ReplyDelete
  5. The Pros and Cons of Group Health Insurance By Timothy Hebert | Customs When Completing Individual Health Insurance Application By Dennis Alexander.50 Free Backlink
    2000 Backlink at cheapest
    5000 Backlink at cheapest
    Boost DA upto 15+ at cheapest
    Boost DA upto 25+ at cheapest . Suggest Article Comments Print ArticleShare this article on FacebookShare this article on TwitterShare

    ReplyDelete
  6. Responsibility road mission trouble sound realize mind. Company woman treat.information

    ReplyDelete

Post a Comment

Popular posts from this blog

Magento Multiple Store Setup

Many times we need to create multiple stores in magento website. In magento we create different stores to make it more local. We can access different stores using below ways: 1) Domain (e.g. www.store1.com and www.store2.com) 2) Subdomain (e.g. store1.mystore.com and store2.mystore.com) 3) Folder (e.g. mystore.com/store1/ and mystore.com/store2/) Its all upto to you which way you prefer. For all these we need to follow below steps: Step 1: Add new Magento store: It might be possible that both store have different  catalogue so to manage this. We need to make two different Base Categories. To create a new base category follow below steps: Log in to your Magento admin panel. Go to Catalog -> Manage Categories . If you want both your websites to share same “ Default Category ”, select it by clicking on it on the left. Or click Add Root Category to create a new root category different from the existing one. Once the category is selected, under the General

Quick Order Placement Form

If we want to place fast and quick order. We want to list all the product along with their categories  and price. For achieving this goal we have to follow these steps: 1) Create Template File : Create a template  file at /app/design/frontend/default/blanco/template/catalog/product/list_fast_order.phtml which will the list of all products along with their categories and Price listing. 2) Edit Layout : Before this we have to add the phtml file in catalog.xml file. 3) Get Result :  In list_fast_order.phtml file we have to show the product category wise /*assume we want to get all products of category id 5 */ $catid_1 = 5; /*we are loading all the products from category id 5 */ $_category = Mage::getModel('catalog/category')->load($catid_1); $subs = $_category->getAllChildren(true); /* Use for loop for making result array */ $result = array(); foreach($subs as $cat_id) { $category = new Mage_Catalog_Model_Category();