为产品添加选项值,然后使用Magento添加到购物车 [英] Add option value to product, then to cart with Magento

查看:53
本文介绍了为产品添加选项值,然后使用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($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.

这篇关于为产品添加选项值,然后使用Magento添加到购物车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆