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
$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 :
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 :
'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);
'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);
nice post..
ReplyDeletecan u tell me where addProduct function is resides
Thanx..
addProduct function define in app/code/core/Mage/Checkout/Model/Cart.php which use to add product in cart...!!!!!
Deletethanx for the reply..
Delete$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.
Hiii Gz chauhan
ReplyDeleteIn 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.
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
Deletethanx Gz chauhan...
Deletei got it..
Hi Gz chauhan
ReplyDeleteHave 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
can you please tell the how can i add bundle and group product as related product into cart
ReplyDeleteCan 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.
ReplyDeletehttp://www.your_domain.com/checkout/cart/add?product = 68&qty = 1&super_attribute[528] = 55& super_attribute[525] = 56
ReplyDeletenot working for configurable products, it shows empty cart even after hitting this url with correct values.
This comment has been removed by the author.
ReplyDeleteIn which file I add this code?
ReplyDeleteYes 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