Sunday 13 July 2014

On 21:52 by Unknown in ,    2 comments
In this post, I would like to show you how can we get product option values.

For this, let us start with creating a sample product that holds some custom options. Our Product has name Test Product With Option and below I am showing you its custom option that we set through admin



Now its frontend view will look like this.



This is how we can obtain each option value



Here what we have done is, first we load our product that holds custom options. We load the product through its id. So $product holds our product. Now we assigns all options of product to $option. getOptions() will return an array of object that of class Mage_Catalog_Model_Product_Option. Then we are looping through each options and then loads option values by invoking getValues(). This method will also return an array that holds objects of class Mage_Catalog_Model_Product_Option_Value. Each of this object holds complete information of a particular value.

So the output will look like this
Option :custom checkbox
Values:
Array
(
[option_type_id] => 7
[option_id] => 4
[sku] =>
[sort_order] => 0
[default_title] => custom checkbox value 1
[store_title] =>
[title] => custom checkbox value 1
[default_price] => 0.0000
[default_price_type] => fixed
[store_price] =>
[store_price_type] =>
[price] => 0.0000
[price_type] => fixed
)
Array
(
[option_type_id] => 8
[option_id] => 4
[sku] =>
[sort_order] => 0
[default_title] => custom checkbox value 2
[store_title] =>
[title] => custom checkbox value 2
[default_price] => 0.0000
[default_price_type] => fixed
[store_price] =>
[store_price_type] =>
[price] => 0.0000
[price_type] => fixed
)
Option :custom dropdown
Values:
Array
(
[option_type_id] => 9
[option_id] => 5
[sku] =>
[sort_order] => 0
[default_title] => custom option value 1
[store_title] =>
[title] => custom option value 1
[default_price] => 0.0000
[default_price_type] => fixed
[store_price] =>
[store_price_type] =>
[price] => 0.0000
[price_type] => fixed
)
Array
( [option_type_id] => 10
[option_id] => 5
[sku] =>
[sort_order] => 0
[default_title] => custom option value 2
[store_title] =>
[title] => custom option value 2
[default_price] => 0.0000
[default_price_type] => fixed
[store_price] =>
[store_price_type] =>
[price] => 0.0000
[price_type] => fixed
)
Array
(
[option_type_id] => 11
[option_id] => 5
[sku] =>
[sort_order] => 0
[default_title] => custom option value 3
[store_title] =>
[title] => custom option value 3
[default_price] => 0.0000
[default_price_type] => fixed
[store_price] =>
[store_price_type] =>
[price] => 0.0000
[price_type] => fixed
)
Option :Custom Text Field
Values:

Thus getting option values of custom option of a product is pretty straight forward and easy to understand.

2 comments: