Control the number of results from a Magento API call

前端 未结 3 511
孤独总比滥情好
孤独总比滥情好 2020-12-30 07:54

I have a program that we use to connect our Magento store to our back end Inventory Control system via the API. Currently what it does is query the Magento API for all order

3条回答
  •  遥遥无期
    2020-12-30 08:06

    All API calls are, eventually, just executed PHP code. There will be a single PHP method somewhere that accepts the arguments passed in via the API call, so your best bet is to track down where that PHP code is executed.

    Step 1 is to find the API call's configuration. In modern versions of Magento, API configurations are kept in files named api.xml

    $ find app/code/core/Mage/ -name 'api.xml'
    app/code/core/Mage/Api/etc/api.xml
    app/code/core/Mage/Catalog/etc/api.xml
    app/code/core/Mage/CatalogInventory/etc/api.xml
    app/code/core/Mage/Checkout/etc/api.xml
    app/code/core/Mage/Customer/etc/api.xml
    app/code/core/Mage/Directory/etc/api.xml
    app/code/core/Mage/GiftMessage/etc/api.xml
    app/code/core/Mage/Sales/etc/api.xml
    

    Once you've found all the api.xml files, search through them to fine which one configures your "top level api namespace" (unsure what this is really called by the internal developers)

    $ find app/code/core/Mage/ -name 'api.xml' | xargs grep sales_order
    app/code/core/Mage/Sales/etc/api.xml:            
    app/code/core/Mage/Sales/etc/api.xml:            
    app/code/core/Mage/Sales/etc/api.xml:            
    app/code/core/Mage/Sales/etc/api.xml:            
    app/code/core/Mage/Sales/etc/api.xml:            
    app/code/core/Mage/Sales/etc/api.xml:            
    app/code/core/Mage/Sales/etc/api.xml:            sales_order
    app/code/core/Mage/Sales/etc/api.xml:            sales_order_shipment
    app/code/core/Mage/Sales/etc/api.xml:            sales_order_invoice
    

    It looks like app/code/core/Mage/Sales/etc/api.xml is the file we want, since it has a tag. Next, open the file and look at the node.

    
        sales/order_api
        Order API
        sales/order
        
            
                Retrieve list of orders by filters
                items
                sales/order/info
            
            
                Retrieve order information
                sales/order/info
            
    

    The first node we're interested in is sales/order_api. This specifies the object that will be instantiated to handle any API call in the sales_order namespace.

    Next, we're going to look for the method list in the node.

    
        Retrieve list of orders by filters
        items
        sales/order/info
    
    

    This node tells us that a call to sales_order.list corresponds to the method items. Combining that with the information found above, we now know the API call sales_order.list will run PHP code equivalent to the following

    $m = Mage::getModel('sales/order_api');
    $results = $m->items($args);
    

    Next, open your model file and look at the items method

    #File: app/code/core/Mage/Sales/Model/Order/Api.php
    public function items($filters = null)
    {
        //..a bunch of code to instantiate a collection object..
        if (is_array($filters)) {
            try {
                foreach ($filters as $field => $value) {
                    if (isset($this->_attributesMap['order'][$field])) {
                        $field = $this->_attributesMap['order'][$field];
                    }
    
                    $collection->addFieldToFilter($field, $value);
                }
            } catch (Mage_Core_Exception $e) {
                $this->_fault('filters_invalid', $e->getMessage());
            }
        }        
    }
    

    At the end of this method, you can see that the method will run through each argument and attempt to use it as a filter on the collection. The key is the field, the value is the value search for. If you inspect the rest of the method you'll see there's no other way the paramaters interact with the collection to add any sort of paging or limits.

    So, this leaves you with three options. The first is to find a set of values to pass into

    $collection->addFieldToFilter($field, $value);
    

    that will limit your collection. My suggestion would be some sort of date filter using the array('from'=>'10','to'=>'20') syntax.

    Your second option would be to create a class rewrite for Mage_Sales_Model_Order_Api::items that does some extra filtering.

    Your third option would be to investigate creating a module that adds a custom API method for you to call.

提交回复
热议问题