Magento 1.7:通过查询字符串将可配置产品添加到购物车
Magento Wiki有一个资源,用于通过Magento<1.3的查询字符串将产品添加到购物车 这引用了使用以下示例的方法:Magento 1.7:通过查询字符串将可配置产品添加到购物车,magento,magento-1.7,Magento,Magento 1.7,Magento Wiki有一个资源,用于通过MagentogetAddUrl()方法中的加密密钥,所以现在URL看起来更像 http://www.your_domain.com.au/checkout/cart/add/uenc/aHR0cDovL3d3dy5jdWx0dXJla2luZ3MuY29tLmF1L2FjY2Vzc29yaWVzL3NvbC1yZXB1YmxpYy90cmFja3Mtb24tZWFyLWJsYWNrLTM1OTg5Lmh0bWw_X19fU0lEPVU,/produc
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中的一个主要区别是表单操作Attribtue的->getAddUrl()方法中的加密密钥,所以现在URL看起来更像
http://www.your_domain.com.au/checkout/cart/add/uenc/aHR0cDovL3d3dy5jdWx0dXJla2luZ3MuY29tLmF1L2FjY2Vzc29yaWVzL3NvbC1yZXB1YmxpYy90cmFja3Mtb24tZWFyLWJsYWNrLTM1OTg5Lmh0bWw_X19fU0lEPVU,/product/35900/
其产品ID为35900
如果我在浏览器中使用此URL,它将引导我进入产品页面,并显示一条消息,内容是请指定产品的选项。
我一直试图在URL中传递所需的属性选项值,以便将产品添加到购物车中,但没有成功。(为了节省空间,我省略了包含加密密钥的URL)我尝试了以下方法,但都没有效果:
/product/35900/super_attribute/49265/4834
/product/35900/super_attribute/49265=4834
/product/35900/49265=4834
/product/35900/49265/4834
我的问题是:是否可以通过URL将可配置产品添加到Magento中的购物车中?如果可以,传递super_属性id和属性选项值的格式是什么?您可以使用类似以下内容:
$_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
)));
}
这个问题也发布在magento.stackexchange上,用户马吕斯友好地给了我解决方案
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.
根据他的建议,您可以使用该方法构建“添加到购物车”链接。在最新的magento中,我们还需要添加表单键:
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}
您可以将用于生成URL的代码粘贴到这里吗?简单的产品URL由以下方式生成:$this->helper('checkout/cart')->getAddUrl($child),其中$child来自$childProducts=Mage::getModel('catalog/product\u type\u configurable')->getUsedProducts(null,$\u product);foreach($childProducts作为$child){}