Question

Hi Folks,

I have success on creating a custom api v1 in magento and it is giving me the data i am looking for, i am exhausted searching for solution where i could add up filters for my custom api's.

My custom API's should be give me records based on limit i pass and it should support filters also. As i could figure out how to use filters in predefined Magento SOAP API's. But i couldn't find how to limit the number of records coming out from Magento end.

Expecting speedy response from Magento Techies.

public function getCustomers() {
    $customerCollection = Mage::getModel('customer/customer')->getCollection(); //Fetch all Customers from magento
    $defaultData = array();
    $finalData = array();
    foreach ($customerCollection as $cust) {
        $data = array();
        $customer = Mage::getModel('customer/customer')->load($cust->getId()); // Get Customer info from Customer Id
        $defaultData = $customer->getData(); //Get Customer data

        if (array_key_exists('default_billing', $defaultData)) {
            $billingAddress = Mage::getModel('customer/address')->load($defaultData['default_billing']);
            $data['default_billing'] = $billingAddress->getData();
        }

        if (array_key_exists('default_shipping', $defaultData)) {
            $shippingAddress = Mage::getModel('customer/address')->load($defaultData['default_shipping']);
            $data['default_shipping'] = $shippingAddress->getData();
        }

        $finalData[] = array_merge($customer->getData(), $data);
    }
    return $finalData;
}

The Above API will POP out all Magento Customers With Billing and Shipping Address. Now just i need to add up filters and limit records which Magento pops. I dont have any idea how to do this, plz help.

Was it helpful?

Solution

First, extend your API call to pass the limit parameter.

$client = new SoapClient('http://magentohost/api/soap/?wsdl');

$session = $client->login('apiUser', 'apiKey');

$limit = 5;
$result = $client->call($session, 'your_extension.getCustomers', $limit);
var_dump($result);

Let's assume the getCustomers() method you posted above is the method which is handling the API request.

Read the limit value and limit the collection size.

public function getCustomers($limit) {
    $customerCollection = Mage::getModel('customer/customer')->getCollection();
    $customerCollection->setPageSize($limit);
    $customerCollection->setCurPage(1);
    // .. and so on
}

By replacing the fixed value in setCurPage() with a variable you can introduce paging.

As for using filters, check Mage_Catalog_Model_Product_Api::items():

/** @var $apiHelper Mage_Api_Helper_Data */
$apiHelper = Mage::helper('api');
$filters = $apiHelper->parseFilters($filters, $this->_filtersMap);
try {
    foreach ($filters as $field => $value) {
        $collection->addFieldToFilter($field, $value);
    }
} catch (Mage_Core_Exception $e) {
    $this->_fault('filters_invalid', $e->getMessage());
}

The API helper assists with handling the different SOAP APIs (v1, v2 with / without WS-I compliance mode enabled). Then you add the filter attributes and values to the collection

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top