Prestashop使用WebService按类别过滤产品

时间:2015-11-27 11:36:24

标签: php web-services prestashop

我正在尝试通过PHP使用Prestashop WebService按类别过滤产品,但似乎这是不可能的。

如何制作?必须是这样的

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

1 个答案:

答案 0 :(得分:1)

您使用哪个Prestashop版本?

我设法为 v1.5.6.1 获取特定类别的产品,如下所示:

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

在此阶段,您将获得产品系列。您可以使用标准对象表示法来访问属性..

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

您应该能够通过到达YOUR_SITE/api/products?schema=synopsis

来查看公开的元素

那很好但是我还没有能够检索产品网址以便打印锚点。任何人?有什么问题?

完整文档(1.5)here

希望它会有所帮助。

修改

动态构建产品网址

  1. 进行第一次API调用,以检索您想要的分类(ies / y)和它们 数据(url slug,他们拥有的产品的ID,......)
  2. 进行第二次API调用,以检索与第一步中检索到的ID相对应的实际数据。
  3. Slugs在items集合的link_rewrite属性下可用(如类别和产品)。将会有与从后端配置的语言总数一样多的slug,因此您可能希望循环遍历link_rewrite属性以获取所有这些并构建所有URL。

    ## 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 ..
        }
    }