Tuesday 4 November 2014

How to add a product to cart through parameters in url in magento

Hi sometime we need to add the product to cart by passing parameters to the url without actual adding to cart through default process. Fortunately we can do this in magento very easily.

Here is how we can do this


For simple product

www.yoursite.com/checkout/cart/add?product=[id]&qty=[qty]
 
id = your product id 
qty = number od items you ned to add to cart //  eg 1 , 2 or 3 
 
 


For simple product with custom optionss
 
http://magentoserver.com/checkout/cart/add?product=13&
qty=1&options[12]=57 
 

You can get the options id and value by viewing the source of the simple product page and it’s dropdowns.

By SKU

To add a product by SKU we must first instantiate an instance of the Mage::app() outside of the present application. You can do this as such:
  1. <?php
  2. include_once 'app/Mage.php';
  3. Mage::app();
  4. Mage::getSingleton('core/session', array('name' => 'frontend'));
By adding a $_GET variable we can get the sku and use an instance of the Product model to get the Magento ID for us.
  1. $cProd = Mage::getModel('catalog/product');
  2. $id = $cProd->getIdBySku("$sku");
Now that we have our ID we can do something useful with it - such as add it to the cart with the default of 1 product as the quantity. You can use other $_GET variables and conditions to simulate the adding of the quanitity to the querystring using the PHP Header Redirect:
  1. header('Location: '. Mage::getUrl('checkout/cart/add', array('product' => $id)));



For bundle product 





The standard URL format for adding a bundle item to the cart via URL is as such:
/path/to/app/checkout/cart/add/product/[id]/?bundle_option
[[option_id]][]=[selection_id]

  1. $w = Mage::getSingleton('core/resource')->getConnection('core_write');
  2. $result = $w->query("select `option_id`, `selection_id` from `catalog_product_bundle_selection` where `parent_product_id`=$id");

his will provide us with our dataset. Using PDO and looping for each individual SKU inside of a given bundle ID we find that the following code will build the parameters for the cart add and then redirect to it:

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

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));
}    

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




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

 
Hope this will help .thanks :)