Prestashop 1.7 至 8 版本。 我无法使用 PS Web 服务库通过 API 获取不同语言的客户组名称,但我可以按照 PS 文档示例检索所有其他字段。 请参阅以下 xml API GET 响应,我的麻烦与名称标签内的多个语言标签有关。 我找不到任何有效的 php 代码来获得像这个数组这样简单的东西:
$groupNames = ("1" => 'Visitatore", "2" => "Visitor", "3" => "Visiteur")
其中1,2,3是我商店的语言id
有什么想法吗?感谢您的帮助。
<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<group>
<id>
<![CDATA[1]]>
</id>
<reduction>
<![CDATA[0.00]]>
</reduction>
<price_display_method>
<![CDATA[1]]>
</price_display_method>
<show_prices>
<![CDATA[1]]>
</show_prices>
<date_add>
<![CDATA[2020-10-07 15:20:23]]>
</date_add>
<date_upd>
<![CDATA[2020-10-15 16:00:34]]>
</date_upd>
<name>
<language id="1" xlink:href="https://myshop.it/api/languages/1">
<![CDATA[Visitatore]]>
</language>
<language id="2" xlink:href="https://myshop.it/api/languages/2">
<![CDATA[Visitor]]>
</language>
<language id="3" xlink:href="https://myshop.it/api/languages/3">
<![CDATA[Visiteur]]>
</language>
</name>
</group>
</prestashop>
这是我的代码..
try {
$webService = new \PrestaShopWebservice($activeShop->url, $activeShop->apikey, false);
// call to retrieve all customer groups
$xml = $webService->get([
'resource' => 'groups',
]);
} catch (\PrestaShopWebserviceException $ex) {
$exception = str($ex->getMessage());
return $exception;
}
$resources = $xml->groups->children();
foreach ($resources as $resource) {
$attributes = $resource->attributes();
$resourceId = $attributes['id'];
try {
// call to retrieve groups fields
$xml2 = $webService->get([
'resource' => 'groups',
'id' => $resourceId
]);
} catch (\PrestaShopWebserviceException $ex) {
$exception = str($ex->getMessage());
return $exception;
}
$groupsFields = $xml2->group->children();
$name = $groupsFields->name;
$name2 = $name->language;
//from here is the issue .. how to get the names ? see dump below
// I tried name2->attributes but it doesn't work
// .....
}
dump of $groupsFields --- all fields are easyily accessible, ex: $groupsFields->reduction
SimpleXMLElement {#1378 ▼
+"id": "1"
+"reduction": "0.00"
+"price_display_method": "1"
+"show_prices": "1"
+"date_add": "2020-10-07 15:20:23"
+"date_upd": "2020-10-15 16:00:34"
+"name": SimpleXMLElement {#1386 ▶}
}
dump of $name2 - how to manage @attributes ???????
SimpleXMLElement {#1379 ▼
+"@attributes": array:1 [▶]
+"0": "Visitatore"
+"1": "Visitor"
+"2": "Visiteur"
}
我终于找到了解决方案..
// retrieves the names of the groups in the different languages $name = $groupsFields->name;
$languages = $name->language;
foreach ($languages as $language) {
$groupNames[] = array((string)$language['id'] => (string)$language );
}