Search code examples
magentomagento-1.7

Magento 1.7: Add Configurable Product To Cart Via Query String


The Magento Wiki has a resource for adding a product to cart via Query String for Magento < 1.3 HERE

This quotes a method using this example:

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

It also mentions that this was valid up to version 1.3.

I have been playing around with this in 1.7 and have noticed a Major difference in 1.7 is the encrypted key in the ->getAddUrl() method for the Form Action Attribtue so now the URLs look more like

http://www.your_domain.com.au/checkout/cart/add/uenc/aHR0cDovL3d3dy5jdWx0dXJla2luZ3MuY29tLmF1L2FjY2Vzc29yaWVzL3NvbC1yZXB1YmxpYy90cmFja3Mtb24tZWFyLWJsYWNrLTM1OTg5Lmh0bWw_X19fU0lEPVU,/product/35900/

With the product ID being the 35900.

If I use this URL in the browser it will direct me to the product page with a message saying Please specify the product's option(s).

I have been trying to pass the desired attribute options value in the URL to add the product to the cart with no success. (For the sake of saving space I'm omitting the URL up to and including the encrypted key) I've tried methods these to no avail:

/product/35900/super_attribute/49265/4834
/product/35900/super_attribute/49265=4834
/product/35900/49265=4834
/product/35900/49265/4834

My question is: Is it possible to add a configurable product via URL to the cart in Magento and if so, what is the format for passing the super_attribute id and Attribute Option Value?


Solution

  • This question was also posted on magento.stackexchange and user Marius kindly gave me the solution...

    This has worked for me on CE 1.7.0.2 (with sample data):
    
    /checkout/cart/add/product/126?super_attribute[525]=100&super_attribute[272]=22
    NOTE (this puzzles me a bit):
    There is a difference between calling:
    
    /checkout/cart/add/product/126?super_attribute[525]=100&super_attribute[272]=22
    and
    
    /checkout/cart/add/product/126?super_attribute[272]=22&super_attribute[525]=100
    I mean the order of the super_attribute parameters is important. After calling the 2 URLs above I ended up with 2 cart lines of the same product with the same options. one looked like this:
    
    Size Small Color Green
    and the other was
    
    Color Green Size Small
    I guess if you add the products to cart via URL you should keep the order of the attributes as shown in the product view page for consistency.
    

    As per his suggestion, you can build the add to cart link using that method.