控制 Magento API 调用的结果数量 [英] Control the number of results from a Magento API call

查看:24
本文介绍了控制 Magento API 调用的结果数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,用于通过 API 将我们的 Magento 商店连接到我们的后端库存控制系统.目前它所做的是在 Magento API 中查询所有处于 Pending 状态的订单,将它们插入后端系统,然后将它们的状态设置为 Magento 中的 Processing.由于我们的库存系统的限制,我们一次只能插入有限数量的订单.我们通过如下所示的 if 循环运行整个过程来控制这一点(仅供参考,代码已被编辑以仅显示此问题的关键部分):

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 orders in Pending status, inserts them into the back end system, then sets their status to Processing in Magento. Due to limitations on our Inventory system, we can only insert a limited number of orders at a time. We control this by running the whole process through an if loop as shown below (FYI, code has been edited down to show only the key parts of this problem):

//The maximum number of orders to download
$iMaxCount = 10 ;

try {
  $proxy = new SoapClient($wsdl_url);
} catch (Exception $e) {
  echo 'Caught exception: ',  $e->getMessage(), "\n";
}

$sessionId = $proxy->login($login, $password);

//fetch the pending orders from the site
$field = array(array('status'=>array( 'pending') ));
$arr = $proxy->call($sessionId, 'sales_order.list', $field);
$iCnt = 0;

foreach($arr as $key => $value){
//only down up to the max count
if ($iCnt < $iMaxCount) {

[... Do the updating and insertion part of the program ...]

   $iCnt++;
} //End of looping through orders

这样做的明显缺点是,即使我将只处理 10 个待处理订单,我仍然必须提取所有待处理订单.即如果我有 200 个挂单,API 会返回所有 200 个,处理 10 个,然后跳过其余的.我想要做的是修改API调用的过滤器,在状态Processing时只拉10个订单.这将允许我消除 if 循环开销并使程序运行更有效,因为它只获取所需的数据.

The obvious downfall of doing it this way is I still have to pull all Pending orders even though I am going to be working with only 10 of them. i.e. If I have 200 pending orders, the API returns all 200 of them, processes 10, then skips the rest. What I want to do is modify the filter of the API call to pull only 10 orders at a time of status Processing. This would allow me to remove the if loop overhead and make the program run more efficiently because it only gets the data it needs.

有谁知道如何应用这种类型的过滤器?我所看到的一切都表明,只有当您知道订单号并据此设置限制时,才能执行此操作.感谢您的帮助!!!

Does anyone know how to apply that type of filter? Everything I have seen suggest you can only do this if you know the order numbers and set limits based on that. Thanks for your help!!!

推荐答案

所有 API 调用最终都只是执行 PHP 代码.某处将有一个 PHP 方法接受通过 API 调用传入的参数,因此最好的办法是跟踪该 PHP 代码的执行位置.

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.

第一步是找到 API 调用的配置.在现代版本的 Magento 中,API 配置保存在名为 api.xml

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

找到所有 api.xml 文件后,搜索它们以确认哪个配置了您的顶级 api 命名空间"(不确定内部开发人员真正调用的是什么)

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:            <sales_order translate="title" module="sales">
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_shipment>
app/code/core/Mage/Sales/etc/api.xml:            <sales_order_invoice>
app/code/core/Mage/Sales/etc/api.xml:            </sales_order_invoice>
app/code/core/Mage/Sales/etc/api.xml:            <order>sales_order</order>
app/code/core/Mage/Sales/etc/api.xml:            <order_shipment>sales_order_shipment</order_shipment>
app/code/core/Mage/Sales/etc/api.xml:            <order_invoice>sales_order_invoice</order_invoice>

看起来 app/code/core/Mage/Sales/etc/api.xml 是我们想要的文件,因为它有一个 <sales_order/> 标签.接下来,打开文件并查看 节点.

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

<sales_order translate="title" module="sales">
    <model>sales/order_api</model>
    <title>Order API</title>
    <acl>sales/order</acl>
    <methods>
        <list translate="title" module="sales">
            <title>Retrieve list of orders by filters</title>
            <method>items</method>
            <acl>sales/order/info</acl>
        </list>
        <info translate="title" module="sales">
            <title>Retrieve order information</title>
            <acl>sales/order/info</acl>
        </info>

我们感兴趣的第一个节点是 sales/order_api.这指定了将被实例化以处理 sales_order 命名空间中的任何 API 调用的对象.

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

接下来,我们将在 节点中查找方法 list.

Next, we're going to look for the method list in the <methods/> node.

<list translate="title" module="sales">
    <title>Retrieve list of orders by filters</title>
    <method>items</method>
    <acl>sales/order/info</acl>
</list>

这个节点告诉我们对sales_order.list 的调用对应于方法items.结合上面找到的信息,我们现在知道 API 调用 sales_order.list 将运行等效于以下内容的 PHP 代码

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);

接下来,打开你的模型文件并查看items方法

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);

这将限制您的收藏.我的建议是使用 array('from'=>'10','to'=>'20') 语法的某种日期过滤器.

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

您的第二个选择是为 Mage_Sales_Model_Order_Api::items 创建一个类重写,以进行一些额外的过滤.

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

您的第三个选择是研究创建一个模块,该模块添加一个自定义 API 方法供您调用.

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

这篇关于控制 Magento API 调用的结果数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆