且构网

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

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

更新时间:2023-11-29 23:38:40

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