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

Add option value to product, then to cart with Magento(将选项值添加到产品,然后使用 Magento 添加到购物车)
本文介绍了将选项值添加到产品,然后使用 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.

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

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)