To be able to get the Products custom options vía REST API we need to extend our Api.
These are the steps we need to follow.
- Copy the file /app/code/core/Mage/Catalog/Model/Api2/Product/Rest.php to /app/code/local/Mage/Catalog/Model/Api2/Product/Rest.php
- Copy the file /app/code/core/Mage/Catalog/etc/api2.xml to /app/code/local/Mage/Catalog/etc/api2.xml
- Edit the api2.xml file adding the bolded/green lines below:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485<?xml version="1.0"?><!----><config><api2><resource_groups><catalog translate="title" module="api2"><title>Catalog</title><sort_order>30</sort_order><children><catalog_product translate="title" module="api2"><title>Product</title><sort_order>50</sort_order></catalog_product></children></catalog></resource_groups><resources><product translate="title" module="api2"><group>catalog_product</group><model>catalog/api2_product</model><working_model>catalog/product</working_model><title>Catalog Product</title><sort_order>10</sort_order><privileges><admin><create>1</create><retrieve>1</retrieve><update>1</update><delete>1</delete></admin><customer><retrieve>1</retrieve></customer><guest><retrieve>1</retrieve></guest></privileges><attributes translate="custom_options entity_id type_id attribute_set_id stock_data image_url is_saleable total_reviews_count url buy_now_url has_custom_options is_in_stock regular_price_with_tax regular_price_without_tax final_price_with_tax final_price_without_tax use_config_gift_message_available use_config_gift_wrapping_available url_key_create_redirect" module="api2"><entity_id>Product ID</entity_id><type_id>Product Type</type_id><attribute_set_id>Attribute Set</attribute_set_id><stock_data>Inventory Data</stock_data><image_url>Default Image</image_url><is_saleable>Salability Status</is_saleable><total_reviews_count>Total Reviews Count</total_reviews_count><url>Product URL</url><buy_now_url>Buy Now URL</buy_now_url><has_custom_options>Has Custom Options</has_custom_options><is_in_stock>Stock Status</is_in_stock><regular_price_with_tax>Regular Price With Tax</regular_price_with_tax><regular_price_without_tax>Regular Price Without Tax</regular_price_without_tax><final_price_with_tax>Final Price With Tax</final_price_with_tax><final_price_without_tax>Final Price Without Tax</final_price_without_tax><use_config_gift_message_available>Use Config Settings for Allow Gift Message</use_config_gift_message_available><use_config_gift_wrapping_available>Use Config Settings for Allow Gift Wrapping</use_config_gift_wrapping_available><url_key_create_redirect>Create Permanent Redirect for old URL</url_key_create_redirect><custom_options>Product Custom Options</custom_options></attributes><entity_only_attributes><customer><read><has_custom_options>1</has_custom_options><tier_price>1</tier_price><total_reviews_count>1</total_reviews_count><url>1</url><buy_now_url>1</buy_now_url><has_custom_options>1</has_custom_options><is_in_stock>1</is_in_stock></read></customer><guest><read><has_custom_options>1</has_custom_options><tier_price>1</tier_price><total_reviews_count>1</total_reviews_count><url>1</url><buy_now_url>1</buy_now_url><has_custom_options>1</has_custom_options><is_in_stock>1</is_in_stock><custom_options>1</custom_options></read></guest></entity_only_attributes>...
- NOTE: Add the “<custom_options>” label into the Customer or Admin tags if you want it to grant them access.
- Clear Cache
- Login into the Backend and go to System -> Web Services -> REST – Attributes -> Guest
- You will notice a new checkbox available called “Product Custom Options”. Check and save it.
- Open /app/code/local/Mage/Catalog/Model/Api2/Product/Rest.php and add the following line in the _prepareProductForResponse() method:
$productData['custom_options'] = $this->getCustomOptions($product);
- Then add this method:
/**
* @param $product
* @return array with the custom options formated to be displayed
*/
protected function getCustomOptions($product)
{
$options = Mage::getModel('catalog/product_option')->getProductOptionCollection($product);
$options_formatted = array();foreach ($options as $option) {
$options_formatted[$option->getOptionId()] = array(
'option_id' => $option->getOptionId(),
'title' => $option->getDefaultTitle(),
'type' => $option->getType(),
'price' => $option->getPrice() ? $option->getPrice() : '0.00'
);
if ($option->getType() === 'drop_down') {
$values = Mage::getSingleton('catalog/product_option_value')->getValuesCollection($option);
foreach ($values as $value) {
/*Mage::log(' ' . $value->getTitle() . ' / ' . $value->getPrice() . ' / ' . $value->getPriceType());;*/
$options_formatted[$option->getOptionId()]['dropdown_options'][] = array(
'option_type_id' => $value->getOptionTypeId(),
'title' => $value->getTitle(),
'price' => $value->getPrice(),
'price_type' => $value->getPriceType()
);
}
}
}
return $options_formatted;
} - Then you’ll see something like this if you request a Product Entity via Rest: