Friday 30 March 2012

Programmatically Add Product In Cart in Magento

Adding a Product to the Cart via Querystring


For Simple Product :

$this->getUrl('checkout/cart').'add?product='.$pro_id.'&qty='.$qty.';
where : $pro_id= 'Product id' ; $qty = 'Product Quentity'

For Simple Product with Custome Option:

$this->getUrl('checkout/cart').'add?product = '.$pro_id.'&qty = '.$qty.'&options['.$pro_value.'] = '.$pro_opt_val.'
where : $pro_value= 'Product option_id';$pro_opt_val= 'Product option_type_id';

For Bundle Product :

$this->getUrl('checkout/cart').'add/product/['.$pro_id.']/?bundle_option[['.option_id.']][]=['.selection_id.']

like if bundle product's id is 2790 than :
      $_product = Mage::getModel('catalog/product')->load(2790);
      if ($_product->getTypeId() == 'bundle') 
     {
        $w = Mage::getSingleton('core/resource')->getConnection('core_write');
        $result = $w->query("select `option_id`, `selection_id` from `catalog_product_bundle_selection` where        `parent_product_id`=$id");
        $route = "checkout/cart/add";
        $params = array('product' => $id);
        while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
            $params['bundle_option['. $row[option_id] .']'][] = $row['selection_id'];
       };
        header("Location: ". Mage::getUrl($route, $params));
   }

For Adding a Configurable Product to the Cart via Querystring:

http://www.your_domain.com/checkout/cart/add?product = 68&qty = 1&super_attribute[528] = 55& super_attribute[525] = 56


Here  68  is the product entity id displayed in magento admin panel.  528 and 525  is the attribute associated with this product and  55 and 56  is the selected value of that attribute. 

Another way to add product in cart :

For Simple Product :

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



For  Configurable  Product  :



$params = array(
    'product' => 23,
    'super_attribute' => array(
         528 =>55,
    ),
    'qty' => 2,
);

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

13 comments:

  1. nice post..
    can u tell me where addProduct function is resides

    Thanx..

    ReplyDelete
    Replies
    1. addProduct function define in app/code/core/Mage/Checkout/Model/Cart.php which use to add product in cart...!!!!!

      Delete
    2. thanx for the reply..

      $params = array(
      'product' => 23,
      'qty' => 2,
      );
      in the above code can we set product price like

      $params = array(
      'product' => 23,
      'qty' => 2,
      'price' => 50,
      );

      so that we can directly move to checkout page..

      Thanx.

      Delete
  2. Hiii Gz chauhan

    In previous comment i asked you to app set product parameters like
    ------
    $params = array(
    'product' => 23,
    'qty' => 2,
    );
    ------
    is this valid for simple product with custom options?
    or how can we set price of "simple product with custom options"?
    i use a code

    $params = array(
    'qty' => 1,
    'options' => array(
    1 => 1
    )
    );

    this code will add the product to cart with the price which is stored in database with option_type_id = 1.

    But i have to set price manually

    like

    $params = array(
    'qty' => 1,
    'options' => array(
    'price' => 50
    )
    );

    am i right..
    pls help..

    thanx again.

    ReplyDelete
    Replies
    1. No you can not give price like above code that you write in option parameter you can give only option value not price, price taken according to that option which set from backend

      Delete
    2. thanx Gz chauhan...
      i got it..

      Delete
  3. Hi Gz chauhan

    Have you tested the bundle product? I get a Please specify product option(s) and it redirects to the product page. I think it is missing the ? following the product id.

    - Dane

    ReplyDelete
  4. can you please tell the how can i add bundle and group product as related product into cart

    ReplyDelete
  5. Can you tell me regarding how can I add simple product into cart using specific store id? I want to add product in specific store view.

    ReplyDelete
  6. http://www.your_domain.com/checkout/cart/add?product = 68&qty = 1&super_attribute[528] = 55& super_attribute[525] = 56

    not working for configurable products, it shows empty cart even after hitting this url with correct values.

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

    ReplyDelete
  8. In which file I add this code?

    ReplyDelete
  9. Yes i am totally agreed with this article and i just want say that this article is very nice and very informative article.I will make sure to be reading your blog more. You made a good point but I can't help but wonder, what about the other side? !!!!!!Thanks Ey yaa

    ReplyDelete