ayoseturru
ayoseturru

Reputation: 199

Prestashop filter products by category using WebService

I'm trying to use Prestashop WebService via PHP to filter products by categories but it seems that it's impossible.

How to make it? Must be something like this

array(‘resource’ =>’products’, ‘display’ => ‘[name]’, ‘filter[category]’ => ‘[x]');

Upvotes: 1

Views: 6697

Answers (1)

Stphane
Stphane

Reputation: 3456

Which Prestashop version do you use ?

I managed to get products for a specific category for v1.5.6.1 as follows:

$webService = new PrestaShopWebservice( YOUR_SITE_URL, YOUR_API_KEY, false );

$opt = array(
    'resource' => 'products',
    'display' => 'full',
    'filter[id_category_default]' => '[8]',
    'limit' => '5'
);

$xml = $webService->get($opt);
$resources = $xml->products->children();

At this stage you get a products collection. You can reach properties using standard object notation ..

$xml->categories->category->associations->products->product
foreach ( $resources as $key => $value ) :
    echo $value->id; // product's identifier
    echo $value->price; // product's .. guess what !
endforeach;

You should be able to see elements exposed by reaching YOUR_SITE/api/products?schema=synopsis

That's fine but I've not been able to retrieve products urls yet in order to print anchors.. Anyone ? Any suggustion ?

Complete documentation (1.5) here

Hope it will help.

EDIT

Construct products URLS on the fly

  1. Make a first API call to retrieve categor(ies/y) you want and their datas (url slug, ids of products they own, ...)
  2. Make a second API call to retrieve the actual datas corresponding to ids retrieved during the first step.

Slugs are available under the link_rewrite property of items collections (like categories and products). There will be as many slugs as the total of languages that have been configured from the back-end, so you may want to loop over the link_rewrite property to get them all and build all urls.

## Initialize Prestashop API
$webService = new PrestaShopWebservice( YOUR_SITE_URL, YOUR_API_KEY, false );

## Getting category I want
$opt = array(
    'resource' => 'categories',
    'display' => 'full',
    'filter[id]' => '[70]', # we are interested only in one category
    'limit' => '1'
);
$xml = $webService->get($opt);
$ws_cat = $xml->categories->category;
$products = $ws_cat->associations->products->product;

## Gathering products ids to feed the second API call filter parameter
$productsIds = array();
foreach ( $products as $p ) {
    $productsIds[] = (int)$p->id;
}

## Getting products ..
$opt = array (
    'resource' => 'products',
    'display' => 'full',
    'filter[id]' => '['.implode('|',$productsIds).']',
    'limit' => '4'
);
$xml = $webService->get($opt);
$products = $xml->products->product;
if ( count($products) ) {
    $products = array();
    foreach ( $products as $value ) {
        $products[] = array(
            'id' => $value->id
            ,'catalogURL' => "{$prestashop['url']}/{$ws_cat->link_rewrite->language[0]}/{$value->id}-{$value->link_rewrite->language[0]}.html";
        );
        # There you go ..
    }
}

Upvotes: 1

Related Questions