Prestashop 使用 WebService 按类别过滤产品 [英] Prestashop filter products by category using WebService

查看:82
本文介绍了Prestashop 使用 WebService 按类别过滤产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

怎么做?一定是这样的

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

解决方案

您使用哪个 Prestashop 版本?

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

$webService = new PrestaShopWebservice( YOUR_SITE_URL, YOUR_API_KEY, false );$opt = 数组('资源' =>'产品','显示' =>'满的','过滤器[id_category_default]' =>'[8]','限制' =>'5');$xml = $webService->get($opt);$resources = $xml->products->children();

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

$xml->categories->category->associations->products->productforeach ( $resources as $key => $value ) :echo $value->id;//产品标识符echo $value->price;//产品的 .. 你猜怎么着!Endforeach;

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

暴露的元素

没关系,但我还无法检索产品网址以打印锚点.. 有人吗?有什么建议吗?

完整文档 (1.5) 此处

希望它会有所帮助.

编辑

即时构建产品网址

  1. 进行第一个 API 调用以检索您想要的类别(ies/y)及其数据(网址 slug,他们拥有的产品的 ID,...)
  2. 进行第二次 API 调用以检索与第一步中检索到的 ID 对应的实际数据.

Slug 在项目集合(如类别和产品)的 link_rewrite 属性下可用.将有与从后端配置的语言总数一样多的 slug,因此您可能需要遍历 link_rewrite 属性以获取它们并构建所有 url.

## 初始化 Prestashop API$webService = new PrestaShopWebservice( YOUR_SITE_URL, YOUR_API_KEY, false );## 获取我想要的类别$opt = 数组('资源' =>'类别','显示' =>'满的','过滤器[id]' =>'[70]', # 我们只对一个类别感兴趣'限制' =>'1');$xml = $webService->get($opt);$ws_cat = $xml->categories->category;$products = $ws_cat->associations->products->product;## 收集产品 ID 以提供第二个 API 调用过滤器参数$productsIds = array();foreach ( $products as $p ) {$productsIds[] = (int)$p->id;}##获取产品..$opt = 数组 ('资源' =>'产品','显示' =>'满的','过滤器[id]' =>'['.implode('|',$productsIds).']','限制' =>'4');$xml = $webService->get($opt);$products = $xml->products->product;如果(计数($产品)){$products = array();foreach ( $products as $value ) {$产品[] = 数组('id' =>$value->id,'catalogURL' =>"{$prestashop['url']}/{$ws_cat->link_rewrite->language[0]}/{$value->id}-{$value->link_rewrite->language[0]}.html";);#给你..}}

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]');

解决方案

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

这篇关于Prestashop 使用 WebService 按类别过滤产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆