将选项值添加到产品,然后使用 Magento 添加到购物车
我搜索了一段时间,只找到了为 Magento 商店中的产品添加全新选项集的机智解决方案.
I searched around for a while and only came up wit solutions that added whole new option sets to products in a Magento store.
我想要完成的是一种将简单产品添加到购物车的方法.这个简单的产品有一些预定义的自定义选项(自由文本字段),必须由 php 函数填充.
What I'm trying to accomplish is a way to add a Simple Product to the cart. This Simple Product has some predifined custom options (free text fields) that has to be filled by a php function.
那么,我该怎么做呢?假设我有一个 ID 为111"的产品和一个自定义选项.
So, how can I do this? Let's say I have a product with the ID "111" and a one custom option.
$qty = '1';
$product = Mage::getModel('catalog/product')->load("111");
// set option value in product model?
$cart = Mage::helper('checkout/cart')->getCart();
$cart->addProduct($product, $qty);
// set option value while passing product to car?
$cart->save();
提前感谢您的帮助.
顺便说一句:通过 QueryString 设置选项值相对容易,如此处所示.
BTW: setting option values via QueryString is relativly easy as seen here.
推荐答案
您没有在产品模型上设置自定义选项,而是通过第二个参数将其传递给 $cart->addProduct($产品,$params)
.
You don't set the custom option on the product model, you pass it in through the second argument to $cart->addProduct($product, $params)
.
我们为一个需要外部应用程序添加到 Magento 购物车的项目进行的设置是使用以下格式的 $params
数组:
The set up we have for a project, that requires an external app to add to the Magento cart, is to use a $params
array of the following format:
$params = array(
'product' => 1, // This would be $product->getId()
'qty' => 1,
'options' => array(
34 => "value",
35 => "other value",
53 => "some other value"
)
);
$params['options']
包含自定义选项信息.键是自定义选项 ID,如果您使用 Firebug 或类似工具检查产品屏幕的自定义选项部分,您可以看到它们.
The $params['options']
contains the custom option information. The keys are the custom option ids, you can see them if you inspect the custom options section of the product screen with Firebug, or similar.
$params['product']
可能是多余的,我不久前为 Magento 的早期版本编写了这个脚本.
The $params['product']
may be redundant, I wrote this script a while ago for a much earlier version of Magento.
另外,我相当确定,当您以这种方式添加时,标准的添加到购物车 事件会触发,因此您需要自己设置它们.可能会有副作用.
Also, I'm fairly sure that the standard add to cart events will fire when you add this way, so you'll need to set them off yourself. There may be side effects.
相关文章