Magento获取选项标签或标题

时间:2016-02-12 16:24:53

标签: magento magento-1.9

我使用Magento函数getCustomOptions,它似乎返回了我需要的所有值,除了选项标签标题。这是我的测试代码:

    foreach($product->getCustomOptions() as $o) {
        echo '<br /><strong>[Item ID]</strong> ' . $o->getItemId() . '<br /><strong>[Value ID]</strong> ' . $o->getId() . '<br /><strong>[Value]</strong> ' . $o->getValue() . '<br /><strong>[Code]</strong> ' . $o->getCode() . '<br />';
    }

以下是结果输出:

[Item ID] 288
[Value ID] 1035
[Value] a:9:{s:7:"product";s:2:"81";s:8:"form_key";s:16:"MJfb8w59zQw2j3T6";s:7:"handles";a:13:{i:0;s:7:"default";i:1;s:13:"STORE_default";i:2;s:27:"THEME_frontend_argento_mall";i:3;s:20:"catalog_product_view";i:4;s:25:"PRODUCT_TYPE_configurable";i:5;s:10:"PRODUCT_81";i:6;s:19:"customer_logged_out";i:7;s:9:"MAP_popup";i:8;s:19:"MAP_price_msrp_item";i:9;s:14:"SHORTCUT_popup";i:10;s:17:"SHORTCUT_uk_popup";i:11;s:33:"tm_lihgtboxpro_product_info_media";i:12;s:15:"page_one_column";}s:7:"ajaxpro";s:1:"1";s:7:"in_cart";s:1:"1";s:15:"related_product";s:0:"";s:15:"super_attribute";a:3:{i:92;s:3:"107";i:151;s:3:"114";i:161;s:2:"67";}s:7:"options";a:1:{i:35;s:7:"536";}s:3:"qty";s:1:"1";}
[Code] info_buyRequest

[Item ID] 288
[Value ID] 1036
[Value] 35
[Code] option_ids

[Item ID] 288
[Value ID] 1037
[Value] 536
[Code] option_35

[Item ID] 288
[Value ID] 1038
[Value] a:3:{i:92;s:3:"107";i:151;s:3:"114";i:161;s:2:"67";}
[Code] attributes

[Item ID] 288
[Value ID] 1039
[Value] 1
[Code] product_qty_71

[Item ID] 288
[Value ID] 1040
[Value] 71
[Code] simple_product

现在我需要知道如何使用商品ID 代码来获取选项标签(或标题)。

3 个答案:

答案 0 :(得分:1)

在产品页

上尝试以下代码前标题和选项ID
<?php 
   $product_options = array();
   foreach ($_product->getOptions() as $_option) {
     $product_options[] = $_option->getTitle();
     if($_option->getTitle() == 'Sweatshirt Color')
        {   
            $values = $_option->getValues();
            foreach ($values as $value) {
                echo $value['option_id'];
                echo $value->getTitle();
            }
        }
    }
?>

答案 1 :(得分:1)

以下是我如何从购物车页面获取特定自定义选项的价值和标签(假设您的自定义选项在前端使用相同的标签)。

$params = Mage::app()->getRequest()->getParams();
/** @var Mage_Catalog_Model_Product $product */
$info = new Varien_Object($params);

// Don't throw an exception if required options are missing
$processMode = Mage_Catalog_Model_Product_Type_Abstract::PROCESS_MODE_LITE;

$options = array();
foreach ($product->getOptions() as $option) {
    /* @var $option Mage_Catalog_Model_Product_Option */
    $group = $option->groupFactory($option->getType())
        ->setOption($option)
        ->setProduct($product)
        ->setRequest($info)
        ->setProcessMode($processMode)
        ->validateUserValue($info->getOptions());

    $optionValue = $info->getData('options/' . $option->getId());
    $options[] = array(
        'label' => $option->getTitle(),
        'option_id' => $option->getId(),
        'option_type' => $option->getType()
    );
    if($options[0]['label'] == 'Your Option Label') {
        $thecode = 'option_' . $options[0]['option_id'];    //Append the Option ID to "option_" resulting in "option_XX" which you'll need in the next step
        foreach($product->getCustomOptions() as $o) {
            if($o->getCode() == $thecode) {             //if Custom Option code = $thecode
                $opt_val = $o->getValue();
            }
        }
    }
}

答案 2 :(得分:0)

使用这个希望对你有帮助....

<?php
    $product = Mage::getModel("catalog/product")->load($_product->getId()); //product id
    foreach ($product->getOptions() as $_option) {
        $values = $_option->getValues();
        echo $values_t = $_option->getTitle().'<br>';
        echo $values_t = $_option->getType().'<br>';
        foreach ($values as $v) {
            echo $v->getTitle().'<br>';
        }
    }
?>