且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将选项值添加到产品,然后使用 Magento 添加到购物车

更新时间:2023-10-25 14:33:04

您没有在产品模型上设置自定义选项,而是通过第二个参数将其传递给 $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.