How to include product images in Magento Api's product list method result

How to include product images in Magento Api's product list method result

September 24, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to include product images in Magento Api's product list method result

Extending Magento API Results for Faster Product Image Retrieval

As an experienced Magento developer and technical blogger at Laravel Company, I understand the importance of optimizing API calls to enhance mobile application performance. In this post, we’ll explore a technique to include product images directly in the catalog_category.assignedProducts API result, eliminating the need for additional catalog_product_attribute_media.list calls.

The Current Challenge

The current approach requires two separate API calls per product:

  1. catalog_category.assignedProducts to fetch basic product data.
  2. catalog_product_attribute_media.list to retrieve image URLs.

This redundancy significantly slows down mobile applications, especially when dealing with large product catalogs.

Extending the API Result

We can extend the catalog_category.assignedProducts result by modifying two core files: app/code/core/Mage/Catalog/Model/Category/Api.php and app/code/core/Mage/Catalog/etc/wsdl.xml.

First, open the Api.php file and locate line 440:

// ...
$collection = $category->setStoreId($storeId)->getProductCollection()
    ->addAttributeToSelect('brand')
    ->addAttributeToSelect('media_gallery_images');
($storeId == 0) ? $collection->addOrder('position', 'asc') : $collection->setOrder('position', 'asc');

// ...

Add the following lines before the foreach loop:

$result = array();

foreach ($collection as $product) {
    $result[] = array(
        'product_id' => $product->getId(),
        'type'       => $product->getTypeId(),
        'set'        => $product->getAttributeSetId(),
        'sku'        => $product->getSku(),
        'position'   => $product->getCatIndexPosition(),
        'brand'      => $product->getData('brand'),
        'media'      => $product->getMediaGalleryImages()
    );
}

// ...

Next, open the wsdl.xml file and locate line 255:

<complexType name="catalogAssignedProduct">
<all>
    <element name="product_id" type="xsd:int"/>
    // ...
</all>
</complexType>

Add the following line between the <element> tags for brand and the closing tag </all>:

<element name="media" type="typens:catalogProductImageEntityArray"/>

Debugging the Issue

You attempted to implement this change but encountered a null value for the ‘media’ property in the API response. The issue lies not in the XML type, but rather in the way Magento handles the API.

The getMediaGalleryImages() method is designed to work with individual product objects within the context of an admin panel or storefront, not with products fetched from an API call.

A Better Approach

To resolve this issue, we need to create a custom API model that utilizes the data returned by the core catalog_category.assignedProducts method and manually populates the ‘media’ property.

First, create a new file named ProductApi.php in your custom module:

// namespace Laravel\Company\Catalog\Model\Category\Api;
// ...
class ProductApi extends Mage_Catalog_Model_Category_Api
{
    public function assignedProducts($categoryId)
    {
        $result = parent::assignedProducts($categoryId);

        foreach ($result['items'] as &$product) {
            $productId = (int)$product['product_id'];

            if (!$productId) continue;

            $product['media'] = Mage::getModel('catalog/product')
                ->load($productId)
                ->getMediaGalleryImages();
        }

        return $result;
    }
}

Replace the Catalog_Api model in your custom module’s configuration with this new class.

Conclusion

Optimizing API calls is a crucial aspect of building high-performance mobile applications connected to Magento-based shops. By extending the core catalog_category.assignedProducts method, we can minimize the number of requests and improve overall speed.

Remember that modifying core files should be avoided whenever possible due to upgrade compatibility issues. The approach described above is intended for advanced developers working on specific projects where performance optimizations are critical.

For a more sustainable solution, consider implementing custom API models in your own modules or using Magento’s extension mechanism. You can learn more about Magento extensions and their benefits by visiting Laravel Company.

Catalog Performance API Orders

Our network