問題描述
我搜索了一段時間,只找到了為 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 添加到購物車的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!