2
votes

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?

3
Can you paste here the code you use to generate the URL ?blmage
A simple product url is generated by: $this->helper('checkout/cart')->getAddUrl($child) where $child is drawn from $childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null,$_product); foreach($childProducts as $child) {}James

3 Answers

6
votes

You can use something like this :

$_typeInstance = $_product->getTypeInstance(true);
$_children     = $_typeInstance->getUsedProducts(null, $_product);
$_attributes   = $_typeInstance->getUsedProductAttributes($_product);
$_cartHelper   = Mage::helper('checkout/cart');

foreach ($_children as $_child) {
    $_superAttributes = array();

    foreach ($_attributes as $_attribute) {
        $_superAttributes[$_attribute->getAttributeId()] = $_child->getData($_attribute->getAttributeCode());
    }

    $_addUrl = $_cartHelper->getAddUrl($_product, array(
        '_query' => array(
            'super_attribute' => $_superAttributes
        )));
}
3
votes

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.

0
votes

In latest magento we need to add form_key also:

https://{site-name}/checkout/cart/add/product/{product_id}/form_key/{form_key}?super_attribute[{attribute_id}]={attribute_value}&super_attribute[{attribute_id}]={attribute_value}