在此代码中,我按 id 加载产品,获取选项集合(在我的测试中仅包含产品的自定义选项,而不包含属性或其他选项),迭代选项并加载每个选项的值集合。只要您有产品 ID,此演示代码就应该可以在几乎任何地方进行测试。
<?php
$productID = $this->getProductId(); //Replace with your method to get your product Id.
$product = Mage::getModel('catalog/product')->load($productID);
$options = Mage::getModel('catalog/product_option')->getProductOptionCollection($product);
foreach ($options as $option) {
Mage::log('Name: ' . $option->getDefaultTitle());
Mage::log(' Type: ' . $option->getType());
Mage::log(' Class: ' . get_class($option));
Mage::log(' Price/Type: ' . ($option->getPrice() ? $option->getPrice() : '0.00') . ' / ' . $option->getType());
if ($option->getType() === 'drop_down') {
$values = Mage::getSingleton('catalog/product_option_value')->getValuesCollection($option);
Mage::log(' Values: (name/price/type)');
foreach ($values as $value) {
Mage::log(' ' . $value->getTitle() . ' / ' . $value->getPrice() . ' / ' . $value->getPriceType());
}
}
}
?>
日志输出示例:
2014-02-18T20:15:25+00:00 DEBUG (7): Name: Turtle Color
2014-02-18T20:15:25+00:00 DEBUG (7): Type: drop_down
2014-02-18T20:15:25+00:00 DEBUG (7): Class: Mage_Catalog_Model_Product_Option
2014-02-18T20:15:25+00:00 DEBUG (7): Price/Type: 0.00 / drop_down
2014-02-18T20:15:25+00:00 DEBUG (7): Values: (name/price/type)
2014-02-18T20:15:25+00:00 DEBUG (7): Red / 0.0000 / fixed
2014-02-18T20:15:25+00:00 DEBUG (7): White / 0.0000 / fixed
2014-02-18T20:15:25+00:00 DEBUG (7): Blue / 0.0000 / fixed
可用数据示例$option Mage::log($option->toArray())
:
注意:price 和 Price_type 仅适用于 drop_down 类型选项的选项值。
2014-02-18T20:19:44+00:00 DEBUG (7): Array
(
[option_id] => 135
[product_id] => 80
[type] => field
[is_require] => 0
[sku] =>
[max_characters] => 25
[file_extension] =>
[image_size_x] =>
[image_size_y] =>
[sort_order] => 90
[description] =>
[default_title] => Turtle Name
[store_title] =>
[title] => Turtle Name
[default_price] => 0.0000
[default_price_type] => fixed
[store_price] =>
[store_price_type] =>
[price] => 0.0000
[price_type] => fixed
)
可用数据示例$values Mage::log($values->toArray())
:
2014-02-18T20:25:21+00:00 DEBUG (7): Array
(
[totalRecords] => 2
[items] => Array
(
[0] => Array
(
[option_type_id] => 1149
[option_id] => 229
[sku] =>
[sort_order] => 10
[default_price] => 0.0000
[default_price_type] => fixed
[store_price] => 0.0000
[store_price_type] => fixed
[price] => 0.0000
[price_type] => fixed
[default_title] => 31"
[store_title] => 31"
[title] => 31"
)
[1] => Array
(
[option_type_id] => 1150
[option_id] => 229
[sku] =>
[sort_order] => 20
[default_price] => 0.0000
[default_price_type] => fixed
[store_price] => 0.0000
[store_price_type] => fixed
[price] => 0.0000
[price_type] => fixed
[default_title] => 31.5"
[store_title] => 31.5"
[title] => 31.5"
)
)
)