首页 文章

Magento SOAP API产品列表分页

提问于
浏览
6

我正在尝试通过SOAP API(V2)读取Magento的产品列表,并尝试做一些/任何类型的分页 .

简单场景:

var filters = new filters();
var products = catalogProductList(out pe, Connection.Session, filters, null);

这使Magento崩溃: "Allowed memory size of 1073741824 bytes exhausted (tried to allocate 72 bytes."

我试图通过在 product_id 上指定两个复杂的过滤器来添加分页:

filters.complex_filter = new complexFilter[]
{
    new complexFilter()
    {
        key = "product_id",
        value = new associativeEntity()
        {
            key = "gt",
            value = "400"
        }
    },
    new complexFilter()
    {
        key = "product_id",
        value = new associativeEntity()
        {
            key = "lt",
            value = "1000"
        }
    }
};

但是,在这种情况下,只应用第二个过滤器,忽略第一个过滤器 .

我正在考虑阅读类别树,然后是分配的产品,但有很多产品没有分配到任何类别或多个类别,所以我会错过它们或多次获取它们 .

有没有办法使用某种类型的分页来阅读产品列表,所以我不会立即阅读完整列表? (注意:要求增加内存不是一个真正的选择)

4 回答

  • 1

    我已经为此提出了一个非常好的解决方案 . 希望这有助于某人 .

    $in = array();
    for ($i = ($page * $size) - $size; $i < ($page * $size); $i++) {
        $in[] = $i + 1;
    }
    $complexFilter = array('complex_filter' => 
        array(
            array(
                'key' => 'product_id',
                'value' => array(
                    'key' => 'in', 
                    'value' => join(",", $in)
                )
            )
        )
    );
    
  • 1

    我在PHP中成功完成了以下操作:

    $filters = new StdClass();
    $filters->complexFilter = array(
        array( 'key' =>'sku', 'value' => array('lt'=>'03969999')),
        array( 'key' => 'sku', 'value' => array('gt'=>'03969000')),
    );
    

    虽然,据说

    <complexType name="filters"><all><element name="filter" type="typens:associativeArray" minOccurs="0"/><element name="complex_filter" type="typens:complexFilterArray" minOccurs="0"/></all></complexType>
    

    也许它需要是“$ filters-> complex_filter = array();”?第一个代码似乎对我有用 .

  • 0

    看起来你需要的答案在https://stackoverflow.com/a/22874035/2741137描述

    显然,您可以在两个过滤条件之一中大写 PRODUCT_ID 来解决Magento的限制,该限制会阻止同一个键上的两个过滤条件 .

  • 2

    丑陋,但对我有用:

    public catalogProductEntity[] GetProducts()
    {
        int storeId;
        catalogProductEntity[] catalogEntity;
        List<catalogProductEntity> res = new List<catalogProductEntity>();
        Client.catalogProductCurrentStore(out storeId, SessionId, null);
    
        var filters = new filters();
        filters.complex_filter = new complexFilter[1];
        filters.complex_filter[0] = new complexFilter();
        complexFilter filter = filters.complex_filter[0];
        filter.key = "name";
        filter.value = new associativeEntity();
        associativeEntity assoc = filter.value;
        assoc.key = "like";
    
        //A to Z.
        for (char i = 'A'; i <= 'Z'; i++)
        {          
            assoc.value = i + "%";
            Client.catalogProductList(out catalogEntity, SessionId, filters, null);
            res.AddRange(catalogEntity);                               
        }
    
        //Starts with #.
        assoc.value = "#%";
        Client.catalogProductList(out catalogEntity, SessionId, filters, null);
        res.AddRange(catalogEntity);
    
        //0 to 9
        for (int i = 0; i <= 9; i++)
        {             
            assoc.value = i + "%";
            Client.catalogProductList(out catalogEntity, SessionId, filters, null);
            res.AddRange(catalogEntity);                
        }
    
        return res.ToArray();
    }
    

相关问题