Magento - 从选项值ID中获取自定义选项值详细信息

Kno*_*ing 14 magento

我有一些与产品自定义选项相关的有趣问题: -

  1. 选项和自定义选项之间有什么区别吗?这是因为我在几乎所有与产品相关的模块中都为每个产品细节找到了两个不同的属性: -

    • options
    • custom_options

    但是,只有一个类只用于产品选项,它倾向于处理自定义选项.请有人澄清这一点.

  2. 我正在尝试获取订购商品的自定义选项,包括自定义选项价格和价格类型.问题是Magento只存储相应订购商品的期权价值,而不是其所有细节(如自定义期权价格和价格类型).
    所以我创建了这个类的对象Mage_Catalog_Model_Product_Option_Value,仅考虑drop_down自定义选项类型.我在下面提供了我的代码,但它仍然是徒劳的,并没有取得预期的结果.有人可以纠正这个代码并帮助我吗?

第2点的代码: -

echo "<pre>";
// $collection contains the whole Order Collection
foreach ($collection as $order) {
    foreach ($order->getAllItems() as $item) {
        $customOptions = $item->getProductOptions();

        foreach ($customOptions['options'] as $_eachOption) {
            // Value ID is stored in this field "option_value"
            $objModel = Mage::getModel('catalog/product_option_value')->load($_eachOption['option_value']);

            // This should provide all the details of this particular Option Value as chosen by the Customer when ordering this Product, but unfortunately it doesn't
            print_r($objModel->getData());

            /**
             * This gives the output as, without any details on Price and Price Type:-
             * Array
             * {
             *     [option_type_id] => 13014
             *     [option_id] => 4921
             *     [sku] => XBPS22
             *     [sort_order] => 0
             * }
             */

            unset($objModel);
        }
    }
}
echo "</pre>";
Run Code Online (Sandbox Code Playgroud)

在做了一些检查之后,我发现与每个选项值相关的价格都存储在catalog_product_option_type_price数据库表中,并且与每个选项相关的价格都存储在catalog_product_option_price数据库表中.因此,必须有一些方法来了解Magento如何获取相应的自定义选项价格.请赐教,并纠正上面的代码?

感谢所有人,提前!

小智 16

我知道这是一个老问题,但它没有答案,在遇到同样的问题后疯狂地寻找答案,我想我会在这里为其他可能找到这个问题的人提供答案.

在这段代码中,我按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());;
        }
    }
}
?>
Run Code Online (Sandbox Code Playgroud)

示例日志输出:

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
Run Code Online (Sandbox Code Playgroud)

$ 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
)
Run Code Online (Sandbox Code Playgroud)

$ value的示例可用数据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"
                )
        )
)
Run Code Online (Sandbox Code Playgroud)