phpmagentocollectionsmagento-rest-api

How to get list of all products in magento shop using an extended custom rest api


I have created an custom api in magento and currently facing the problem that i can get all the products that are available in the magento store, everytime i test the custom endpoint i get empty array. How can I get all the products that are available inside the magento store.

V1.php code

<?php
class Class_Restapi_Model_Api2_Restapi_Rest_Admin_V1 extends Class_Restapi_Model_Api2_Restapi
{
        public function _retrieveCollection()
        {
                $product = Mage::getModel('catalog/product')->load(1);
                return $product;
        }
}

How can i get all products regardless of the category?

UPDATE

here is the updated code, now when I try to debug it in postman I get empty array

<?php
class Model_Restapi_Model_Api2_Restapi_Rest_Admin_V1 extends Model_Restapi_Model_Api2_Restapi
{
        public function _retrieveCollection()
        {
                $products = Mage::getModel("catalog/product")->getResourceCollection()->load();
                return $products->toArray();
        }
}

the result from postman

[
    [],
    []
]

Solution

  • your code is only loading a product model of first product.

    Use a product collection to get all products

    $collection = Mage::getModel('catalog/product')->getCollection();
    $collection->load();
    

    Beware this is a huge amount of data. Use Filters, Limits and other methods to keep collection items as small as you need them