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

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

问题描述

我有一个我们用来通过API我们的Magento商店连接到我们的后端库存控制系统的程序。目前,它的作用是查询所有订单Magento的API中待定状态,它们插入到后台系统,然后将在Magento他们的处理状态。由于我们的库存系统上的限制,我们只能一次插入的订单数量有限。我们通过一个循环,如果运行的全过程控制这一如下图所示(仅供参考,code已被编辑了下来,只显示这个问题的关键部位):

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调用的过滤器的状态处理的时候拉只有10个订单。这将让我的开销除去如果循环,更有效地使程序运行,因为它仅获得其所需的数据。

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 code。将有一个PHP方法的地方,接受通过API调用传入的参数,所以最好的办法就是在那里被执行的PHP code追查。

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.

第1步是找到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>

看起来应用程序/ code /核心/法师/销售/等/ api.xml 是我们想要的文件,因为它有一个&LT; sales_order的/&GT; 标记。接下来,打开该文件,并期待在&LT; sales_order的/&GT; 节点。

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>

我们感兴趣的第一个节点是&LT;模型&gt;销售/ order_api&LT; /模型&GT; 。此规定将被实例化处理在 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.

接下来,我们要查找的方法列表&LT;方法/&GT; 节点。

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 调用对应的方法项目。结合与以上找到的信息,我们现在知道的API调用 sales_order.list 将运行PHP code等同于以下

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

接下来,打开你的模型文件,并期待在项目

#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());
        }
    }        
}

在这个方法的末尾,你可以看到,该方法将通过每个参数运行,并尝试使用它作为收藏的过滤器。最关键的是领域中,价值是值搜索。如果您检查方法的其余部分,你会看到有没有其他办法的PARAMATERS与集合交互添加任何排序分页或限制的。

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

这会限制你的收藏。我的建议是使用数组是某种日期过滤器('从'=>'10','到'=>'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 ::物品类重写,做一些额外的过滤。

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天全站免登陆