使用 magento API V2 限制结果数量

发布于 2024-12-27 20:07:59 字数 675 浏览 0 评论 0原文

您好,我已经在网站上搜索了我的问题,但没有找到简单的解决方案,我认为这个问题非常基本。

我正在使用 Api V2,所以也许现在有一个解决方案。在这里,这是我的代码:

$api_soap_url = 'http://localhost/magento/api/v2_soap?wsdl=1';

$client = new SoapClient($api_soap_url);
$session_id = $client->__soapCall('login',array($user, $pw));

$data = array($session_id);
$result = $client->__soapCall('customerCustomerList', $data);

这会返回所有结果,我需要限制结果数量,因此我尝试使用此处找到的过滤器和其他解决方案,但没有运气。

我唯一没有尝试过的是这个:

控制 Magento API 调用的结果数量

但是按日期过滤并不能解决我的问题,对于如此简单的需求,重写类是一个非常复杂的解决方案。

提前致谢

Hi I have searched the site for my question but haven't found an easy solution and I think the issue is so basic.

I'm using Api V2 so maybe there's a solution now. Here I go, this is my code:

$api_soap_url = 'http://localhost/magento/api/v2_soap?wsdl=1';

$client = new SoapClient($api_soap_url);
$session_id = $client->__soapCall('login',array($user, $pw));

$data = array($session_id);
$result = $client->__soapCall('customerCustomerList', $data);

This returns all results, I need to limit number of result so I have tried using filters and other solutions found here but no luck.

The only one I haven't tried is this one:

Control the number of results from a Magento API call

But filtering by date doesn't solve my problem and rewriting classes is a ver complex solution for such a simple need.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抱猫软卧 2025-01-03 20:07:59

我不确定过滤器可以限制结果数量,但您可以尝试以下操作:

$complexFilter = array(
    'complex_filter' => array(
        array(
            'key' => 'created_at',
            'value' => array('key' => 'gt', 'value' => '2012-05-13 06:11:00')
            // where created_at is greater than 2012-05-13 06:11:00
            // For example: eq (equals), neq (not equals), gt (greater than), lt (less than), etc.
        )
    )
);
$result = $client->customerCustomerList($session, $complexFilter);

I'm not sure the filter can limit number of result but you can try this:

$complexFilter = array(
    'complex_filter' => array(
        array(
            'key' => 'created_at',
            'value' => array('key' => 'gt', 'value' => '2012-05-13 06:11:00')
            // where created_at is greater than 2012-05-13 06:11:00
            // For example: eq (equals), neq (not equals), gt (greater than), lt (less than), etc.
        )
    )
);
$result = $client->customerCustomerList($session, $complexFilter);
仄言 2025-01-03 20:07:59

我最终覆盖了app/code/core/Mage/Sales/Model/Order/Api.php,添加了一个名为“collection.limit”的“特殊魔法”字段。您的里程可能会有所不同;我对 Magento 安装和访问 Magento 安装的程序(在本例中是一组 C# 程序)都有严格的控制。

我的调用者只是使用“魔法字段”作为键/值对,如下所示(请再次测试,我是从 C# 调用的,所以这个 php 应该被认为是可疑的):

$collectionLimitClause = array (
  'key' => 'collection.limit',
  'value' => array('key' => 'eq', 'value' => '10')
);

在我的 Magento 安装中(这部分经过测试,实时运行),我在本地命名空间中创建了一个 Sales/Model/Order/Api.php 并重写了 items 函数。在该函数的第 32 行左右,您将看到以下内容:

    $apiHelper = Mage::helper('api');
    $filters = $apiHelper->parseFilters($filters, $this->_attributesMap['order']);
    try {
        foreach ($filters as $field => $value) {
            $orderCollection->addFieldToFilter($field, $value);
        }
    } catch (Mage_Core_Exception $e) {
        $this->_fault('filters_invalid', $e->getMessage());
    }

相反,我使用 strncmp 在这里“捕获”我自己的魔法限制器,并在 foreach 内使用 if-else:

    $apiHelper = Mage::helper('api');
    $filters = $apiHelper->parseFilters($filters, $this->_attributesMap['order']);
    try {
        foreach ($filters as $field => $value) {
            if( !strncmp($field,"collection.limit",16) ) {
                $orderCollection->getSelect()->limit($value['eq']);
            }
            else {
                $orderCollection->addFieldToFilter($field, $value);
            }
        }
    } catch (Mage_Core_Exception $e) {
        $this->_fault('filters_invalid', $e->getMessage());
    }

我对此并没有过于兴奋,但是,我认为它非常安全并且有效。

I ended up overriding app/code/core/Mage/Sales/Model/Order/Api.php, adding a "special magic" field called "collection.limit". Your mileage may vary; I have tight controls on both the Magento installation and the programs (in this case, a set of C# programs) accessing the Magento installation.

My caller simply uses the "magic field" as a key/ value pair, something like this (please test, again, I was calling from C#, so this php should be considered suspect):

$collectionLimitClause = array (
  'key' => 'collection.limit',
  'value' => array('key' => 'eq', 'value' => '10')
);

In my Magento installation (this part is tested, live and running), I created a Sales/Model/Order/Api.php in my local namespace and over-rode the items function. Around the 32nd or so line of that function, you'll see this:

    $apiHelper = Mage::helper('api');
    $filters = $apiHelper->parseFilters($filters, $this->_attributesMap['order']);
    try {
        foreach ($filters as $field => $value) {
            $orderCollection->addFieldToFilter($field, $value);
        }
    } catch (Mage_Core_Exception $e) {
        $this->_fault('filters_invalid', $e->getMessage());
    }

Instead, I "catch" my own magic limiter with the strncmp here, with an if-else inside the foreach:

    $apiHelper = Mage::helper('api');
    $filters = $apiHelper->parseFilters($filters, $this->_attributesMap['order']);
    try {
        foreach ($filters as $field => $value) {
            if( !strncmp($field,"collection.limit",16) ) {
                $orderCollection->getSelect()->limit($value['eq']);
            }
            else {
                $orderCollection->addFieldToFilter($field, $value);
            }
        }
    } catch (Mage_Core_Exception $e) {
        $this->_fault('filters_invalid', $e->getMessage());
    }

I'm not overly excited by this, but, I think it's pretty safe and it works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文