Magento SOAP API Product List Pagination
Asked Answered
I

4

6

I'm trying to read the list of products from Magento over the SOAP API (V2) and try to do some/any type of pagination.

Simple scenario:

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

This crashes Magento with: "Allowed memory size of 1073741824 bytes exhausted (tried to allocate 72 bytes."

I've tried to add pagination by specifying two complex filters on the 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"
        }
    }
};

However in this scenario only the second filter is applied, the first one is ignored.

I was thinking of reading the category tree and then the assigned products but there are lots of products that are not assigned to any category or to multiple categories so I'll either miss them or get them multiple times.

Is there a way to read the products list using some type of pagination so I don't read the complete list at once? (Note: Requesting to increase memory is not really an option)

Impurity answered 29/3, 2012 at 23:43 Comment(5)
Which language is the client side of your request? Java?Haught
Did you ever make any progress on this?Varitype
Having the same problem as you. The "gt" filter is ignored for some reasonLipfert
Of any help? #1921422Decrypt
This guy tells why two like filters don't work. sophomoredev.com/2012/07/hacking-a-stubborn-apiPanhellenic
C
3

I've come up with a pretty good solution for this. Hope this helps someone.

$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)
            )
        )
    )
);
Certificate answered 23/9, 2015 at 17:27 Comment(0)
L
1

It looks like the answer you need is described at https://mcmap.net/q/771755/-combined-complex-filter-for-ranges

Apparently, you can capitalize PRODUCT_ID in one of the two filter conditions to work around Magento's limitation that prevents two filter conditions on the same key.

Lozada answered 13/11, 2014 at 22:56 Comment(0)
S
0

I've successfully done the following in PHP:

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

Although, according to

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

Maybe it needs to be "$filters->complex_filter = array();"? The first code appeared to have worked for me.

Salian answered 17/5, 2012 at 15:4 Comment(0)
L
0

Ugly as hell, but works for me :

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();
}
Lipfert answered 23/5, 2012 at 13:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.