Magento 1.7:通过查询字符串将可配置产品添加到购物车

时间:2013-07-04 00:32:41

标签: magento magento-1.7

Magento Wiki有一个资源,可以通过Magento的查询字符串向购物车添加产品。 1.3 HERE

这引用了一个使用此示例的方法:

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

它还提到这在版本1.3之前有效。

我在1.7中一直在玩这个并注意到1.7中的一个主要区别是表单操作属性的 - > getAddUrl()方法中的加密密钥所以现在URL看起来更像

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

产品ID为35900。

如果我在浏览器中使用此网址,则会将我引导至产品页面,并显示一条消息Please specify the product's option(s).

我一直在尝试在网址中传递所需的属性选项值,以便将产品添加到购物车但没有成功。 (为了节省空间我省略了URL,包括加密密钥)我尝试过这些方法无济于事:

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

我的问题是:是否可以通过URL将可配置产品添加到Magento中的购物车中,如果是这样,传递super_attribute id和属性选项值的格式是什么?

3 个答案:

答案 0 :(得分:6)

您可以使用以下内容:

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

答案 1 :(得分:3)

此问题也发布在magento.stackexchange上,用户Marius亲切地给了我解决方案...

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.

根据他的建议,您可以使用该方法构建添加到购物车链接。

答案 2 :(得分:0)

在最新的magento中,我们还需要添加form_key:

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}
相关问题