Magento API V2 - add an additional attribute to API response

Magento API V2 - add an additional attribute to API response

August 11, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento API V2 - add an additional attribute to API response

Manipulating Magento API V2 Responses: Adding Custom Attributes

Magento’s API v2 provides robust functionality for interacting with your store’s data programmatically. However, there are times when the default responses do not provide all the information you need. In such cases, manual modification of the response structure can be necessary.

Understanding the Current Setup

The salesOrderCreditmemoInfo API call returns details about a specific credit memo, including an array of associated product items. By default, this array does not include the product_type attribute that you are interested in.

Attempted Solution: Direct Array Manipulation

Your initial approach was to directly manipulate the $result['items'] array within the info() function of Mage\Sales\Model\Order\Creditmemo\Api.php. You tried appending a new key-value pair (product_type) to this array for each item.

public function info($creditmemoIncrementId)
{
    ...
    $result['items'] = [];
    foreach ($creditmemo->getAllItems() as $item) {
        $product_type = '1'; //test value to check if works
        $attribs = $this->_getAttributes($item, 'creditmemo_item');
        $attribs['product_type'] = $product_type;

        $result['items'][] = $attribs;
    }
}

The Limitations of Direct Manipulation

While this manipulation seems to work locally—you can log the $result array and see the product_type key added—the change does not propagate to the actual API response. This is because Magento’s API uses a different mechanism for generating the final JSON output.

The API response is generated using the Mage_Api2_Resource_Creditmemo_Collection model, which formats the data according to its own rules before sending it to the client. Any manual changes made in the API model are not reflected in this final formatting stage.

A More Robust Approach: Using Observers

To add a custom attribute to the API response, you should use Magento’s observer system. An observer allows you to intercept and modify data at various points in the execution flow, including before it is sent back to the client via the API.

Here are the steps:

  1. Create an observer module that implements the sales_order_creditmemo_collection_get_items_after event. This event occurs after Magento has prepared the credit memo items for response but before they are formatted into JSON.

  2. In your observer method, modify the $items collection returned by the event. You can add a new attribute to each item using the setData() function.

public function addProductTypeToCreditMemoItems(Varien_Event_Observer $observer)
{
    $collection = $observer->getEvent()->getItems();

    foreach ($collection as $item) {
        // Assuming you want to set the product type based on a specific condition
        if ($item->getSku() == 'SKU-123') {
            $item->setData('product_type', 'custom');
        }
    }
}

Remember, you need to register this observer in your module’s etc/config.xml file:

<config>
  <modules>
    <LaravelCompany_MyModule>
      <version>1.0.0</version>
    </LaravelCompany_MyModule>
  </modules>
  <global>
    <events>
      <sales_order_creditmemo_collection_get_items_after>
        <observers>
          <laravelcompany_add_product_type_to_creditmemos>
            <type>singleton</type>
            <class>LaravelCompany_MyModule_Model_Observer</class>
            <method>addProductTypeToCreditMemoItems</method>
          </laravelcompany_add_product_type_to_creditmemos>
        </observers>
      </sales_order_creditmemo_collection_get_items_after>
    </events>
  </global>
</config>

Conclusion

Adding custom attributes to Magento API responses requires a deeper understanding of the execution flow and the use of observers. By leveraging the sales_order_creditmemo_collection_get_items_after event, you can intercept the data before it is formatted into JSON and add any necessary information.

Remember, this is a powerful tool but should be used judiciously to avoid creating inconsistencies between API responses and other parts of your system. Always ensure that your changes are logically sound and do not violate any business rules or data integrity constraints.

For more detailed guides on Magento module development and observer usage, consider exploring the resources available at Laravel Company.

Catalog API Orders

Our network