Product Custom Options in REST API

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.

  1. Copy the file /app/code/core/Mage/Catalog/Model/Api2/Product/Rest.php to /app/code/local/Mage/Catalog/Model/Api2/Product/Rest.php
  2. Copy the file /app/code/core/Mage/Catalog/etc/api2.xml to /app/code/local/Mage/Catalog/etc/api2.xml
  3. Edit the api2.xml file adding the bolded/green lines below:

     
  4. NOTE: Add the “<custom_options>” label into the Customer or Admin tags if you want it to grant them access.
  5. Clear Cache
  6. Login into the Backend and go to System -> Web Services -> REST – Attributes -> Guest
  7. You will notice a new checkbox available called “Product Custom Options”. Check and save it.
    Product Custom Options added In Backend
  8. 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);
  9. 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;
    }
  10. Then you’ll see something like this if you request a Product Entity via Rest:Custom Options displayed

Leave a comment