問題描述
我在結賬時有幾個產品,我需要能夠通過代碼獲取為它們選擇的所有自定義選項.
非常感謝任何幫助!
我只給你一個產品的例子.假設您知道所需產品的 Sku(例如,將其設為ABCDE").這樣您就可以獲取該產品的 ID.
代碼有點像:-
$productSku = "ABCDE";$product = Mage::getModel('目錄/產品');$productId = $product->getIdBySku( $productSku );$product->load($productId);/*** 在 Magento 模型或數據庫架構級別,產品的自定義選項是* 執行 &僅保留為選項".所以,在檢查任何產品是否有* 是否自定義選項,我們應該只使用hasOptions()"這個方法來檢查.*/if($product->hasOptions()) {echo '';foreach ($product->getOptions() as $o) {$optionType = $o->getType();echo 'Type = '.$optionType;如果($optionType == 'drop_down'){$values = $o->getValues();foreach ($values as $k => $v) {打印_r($v);}}別的 {打印_r($o);}}echo '</pre>';}
我認為這會讓你開始.
根據變量$optionType
"中選項的類型,您需要調用另一個嵌套的foreach
"循環.我曾研究過文本框、文本字段、下拉列表,但沒有研究過其他類型.所以我想你需要自己做更多的 RnD.
I have a couple products at checkout that I need to be able to get all of the custom options that are selected for them through code.
Any help is much appreciated!
I will just give you an example of one product. Let's say that you know the Sku (for example, let it be "ABCDE") of your required product. So you will be able to get the ID of that product.
The code will be somewhat like:-
$productSku = "ABCDE";
$product = Mage::getModel('catalog/product');
$productId = $product->getIdBySku( $productSku );
$product->load($productId);
/**
* In Magento Models or database schema level, the product's Custom Options are
* executed & maintained as only "options". So, when checking whether any product has
* Custom Options or not, we should check by using this method "hasOptions()" only.
*/
if($product->hasOptions()) {
echo '<pre>';
foreach ($product->getOptions() as $o) {
$optionType = $o->getType();
echo 'Type = '.$optionType;
if ($optionType == 'drop_down') {
$values = $o->getValues();
foreach ($values as $k => $v) {
print_r($v);
}
}
else {
print_r($o);
}
}
echo '</pre>';
}
I think this will let you get started.
Depending upon the type of the option in the variable "$optionType
", you need to call another nested "foreach
" loop. I have worked on text boxes, text fields, drop downs, but not on other types. So I suppose you need to do some more RnD by yourself.
這篇關于如何在 Magento 中以編程方式獲取自定義選項的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!