問題描述
我在結(jié)賬時(shí)有幾個(gè)產(chǎn)品,我需要能夠通過代碼獲取為它們選擇的所有自定義選項(xiàng).
非常感謝任何幫助!
我只給你一個(gè)產(chǎn)品的例子.假設(shè)您知道所需產(chǎn)品的 Sku(例如,將其設(shè)為ABCDE").這樣您就可以獲取該產(chǎn)品的 ID.
代碼有點(diǎn)像:-
$productSku = "ABCDE";$product = Mage::getModel('目錄/產(chǎn)品');$productId = $product->getIdBySku( $productSku );$product->load($productId);/*** 在 Magento 模型或數(shù)據(jù)庫架構(gòu)級(jí)別,產(chǎn)品的自定義選項(xiàng)是* 執(zhí)行 &僅保留為選項(xiàng)".所以,在檢查任何產(chǎn)品是否有* 是否自定義選項(xiàng),我們應(yīng)該只使用hasOptions()"這個(gè)方法來檢查.*/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>';}
我認(rèn)為這會(huì)讓你開始.
根據(jù)變量$optionType
"中選項(xiàng)的類型,您需要調(diào)用另一個(gè)嵌套的foreach
"循環(huán).我曾研究過文本框、文本字段、下拉列表,但沒有研究過其他類型.所以我想你需要自己做更多的 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.
這篇關(guān)于如何在 Magento 中以編程方式獲取自定義選項(xiàng)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!