Magento-从选项价值ID获取自定义选项价值详细信息 [英] Magento - Get Custom Option Value details from Option Value ID

查看:51
本文介绍了Magento-从选项价值ID获取自定义选项价值详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  1. 选项&之间是否有任何区别?自定义选项?这是因为我在几乎所有与产品相关的模块中为每个产品"详细信息找到了两个不同的属性:

    • 选项
    • custom_options

    但是,产品选项只有一个类别,该类别通常会处理自定义选项".我正在寻求澄清.

  2. 我正在尝试获取已订购商品的自定义选项,包括自定义选项价格和价格类型.问题在于Magento仅存储相应订购商品的期权价值,而不是其所有详细信息(例如自定义期权价格和价格类型).

    因此,我仅考虑 drop_down 自定义选项类型,创建了一个此类 Mage_Catalog_Model_Product_Option_Value 的对象.我在下面提供了我的代码,但是它仍然是徒劳的,并且没有获取所需的结果.我该如何纠正此代码?

第2点的代码:

echo "

";//$ collection包含整个Order Collectionforeach(将$ collection作为$ order){foreach($ order-> getAllItems()as $ item){$ customOptions = $ item-> getProductOptions();foreach($ customOptions ['options'] as $ _eachOption){//值ID存储在此字段中.$ objModel = Mage :: getModel('catalog/product_option_value')-> load($ _ eachOption ['option_value']);//这应提供客户在订购此产品时选择的特定选项值的所有详细信息,但不幸的是,它没有print_r($ objModel-> getData());/***这给出了输出,没有价格和价格类型的任何详细信息:* 大批* {* [option_type_id] =>13014* [option_id] =>4921* [sku] =>XBPS22* [sort_order] =>0*}*/unset($ objModel);}}}回声</pre>"; 

经过检查,我发现与每个期权价值相关的价格存储在 catalog_product_option_type_price 数据库表中,与每个期权相关的价格存储在 catalog_product_option_price 中数据库表.因此,Magento 如何获取相应的自定义选项值价格肯定有某种方式.

解决方案

在此代码中,我通过id加载产品,获取选项集合,该集合在我的测试中仅包含产品的自定义选项,不包含属性或其他选项,遍历选项并为每个选项加载值集合.只要您具有产品ID,此演示代码就几乎可以在任何地方进行测试.

 <?php$ productID = $ this-> getProductId();//用您的方法替换以获得您的产品ID.$ product = Mage :: getModel('catalog/product')-> load($ productID);$ options = Mage :: getModel('catalog/product_option')-> getProductOptionCollection($ product);foreach(将$ options作为$ 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作为$ value){Mage :: log(''.$ value-> getTitle().'/'.$ value-> getPrice().'/'.$ value-> getPriceType());}}}?> 

示例日志输出:

 <代码> 2014-02-18T20:15:25 + 00:00调试(7):名称:乌龟色2014-02-18T20:15:25 + 00:00调试(7):类型:drop_down2014-02-18T20:15:25 + 00:00调试(7):类:Mage_Catalog_Model_Product_Option2014-02-18T20:15:25 + 00:00调试(7):价格/类型:0.00/drop_down2014-02-18T20:15:25 + 00:00调试(7):值:(名称/价格/类型)2014-02-18T20:15:25 + 00:00调试(7):红色/0.0000/固定2014-02-18T20:15:25 + 00:00调试(7):白色/0.0000/固定2014-02-18T20:15:25 + 00:00调试(7):蓝色/0.0000/固定 

$ option Mage :: log($ option-> toArray()):

的可用数据示例

注意:price和price_type仅在drop_down类型选项的选项值上可用.

  2014-02-18T20:19:44 + 00:00 DEBUG(7):数组([option_id] =>135[product_id] =>80[类型] =>场地[is_require] =>0[sku] =>[max_characters] =>25[file_extension] =>[image_size_x] =>[image_size_y] =>[sort_order] =>90[说明] =>[default_title] =>龟名[store_title] =>[title] =>龟名[default_price] =>0.0000[default_price_type] =>固定的[store_price] =>[store_price_type] =>[价格] =>0.0000[价格类型] =>固定的) 

$ values Mage :: log($ values-> toArray()):

的可用数据示例

  2014-02-18T20:25:21 + 00:00 DEBUG(7):数组([totalRecords] =>2个[项目] =>大批([0] =>大批([option_type_id] =>1149[option_id] =>229[sku] =>[sort_order] =>10[default_price] =>0.0000[default_price_type] =>固定的[store_price] =>0.0000[store_price_type] =>固定的[价格] =>0.0000[price_type] =>固定的[default_title] =>31英寸[store_title] =>31英寸[title] =>31英寸)[1] =>大批([option_type_id] =>1150[option_id] =>229[sku] =>[sort_order] =>20[default_price] =>0.0000[default_price_type] =>固定的[store_price] =>0.0000[store_price_type] =>固定的[价格] =>0.0000[price_type] =>固定的[default_title] =>31.5";[store_title] =>31.5";[title] =>31.5";))) 

I have some intriguing questions related to Custom Options of Product:-

  1. Is there any difference between Options & Custom Options? This is because I have found two different properties for each Product details, in almost all product-related modules:

    • options
    • custom_options

    However there is only one class for only the Product Option, which tends to take care of the Custom Options. I am seeking clarification on this point.

  2. I am trying to fetch the Custom Options of an Ordered Item, including the Custom Option Price and Price Type. The problem is that Magento only stores the Option Value for the corresponding Ordered Item, and not all its details (like Custom Option Price & Price Type).

    So I created an object of this class Mage_Catalog_Model_Product_Option_Value, considering only the drop_down Custom Option Type. I've provided my code below, but it is still in vain and not fetching the desired results. How can I rectify this code?

Code for point #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>";

After doing some checking, I found that the price related to each Option Values are stored in catalog_product_option_type_price database table, and the price related to each Options are stored in catalog_product_option_price database table. So there must be some way as to how Magento fetches the corresponding Custom Option Value prices.

解决方案

In this code, I load the product by id, get the option collection, which in my tests only contains the custom options for a product, not attributes or other options, iterate through the options and load the values collection for each option. This demo code should be testable pretty much anywhere as long as you have a product 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());
        }
    }
}
?>

Example Log Output:

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

Example available data for $option Mage::log($option->toArray()):

note: price and price_type are only available on the option values for drop_down type options.

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
)

Example available data for $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"
                )
        )
)

这篇关于Magento-从选项价值ID获取自定义选项价值详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆