mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2026-02-11 12:09:36 +00:00
Properly parse JSONLD product data if it is in an array with others
This commit is contained in:
parent
52be548170
commit
d868225260
1 changed files with 17 additions and 4 deletions
|
|
@ -97,10 +97,17 @@ class GenericWebProvider implements InfoProviderInterface
|
||||||
|
|
||||||
$vendor_infos = null;
|
$vendor_infos = null;
|
||||||
if (isset($jsonLd['offers'])) {
|
if (isset($jsonLd['offers'])) {
|
||||||
|
|
||||||
|
if (array_is_list($jsonLd['offers'])) {
|
||||||
|
$offer = $jsonLd['offers'][0];
|
||||||
|
} else {
|
||||||
|
$offer = $jsonLd['offers'];
|
||||||
|
}
|
||||||
|
|
||||||
$vendor_infos = [new PurchaseInfoDTO(
|
$vendor_infos = [new PurchaseInfoDTO(
|
||||||
distributor_name: $this->extractShopName($url),
|
distributor_name: $this->extractShopName($url),
|
||||||
order_number: $jsonLd['sku'] ?? $jsonLd['@id'] ?? $jsonLd['gtin'] ?? 'Unknown',
|
order_number: (string) ($jsonLd['sku'] ?? $jsonLd['@id'] ?? $jsonLd['gtin'] ?? 'Unknown'),
|
||||||
prices: [new PriceDTO(minimum_discount_amount: 1, price: (string) $jsonLd['offers']['price'], currency_iso_code: $jsonLd['offers']['priceCurrency'] ?? null)],
|
prices: [new PriceDTO(minimum_discount_amount: 1, price: (string) $offer['price'], currency_iso_code: $offer['priceCurrency'] ?? null)],
|
||||||
product_url: $jsonLd['url'] ?? $url,
|
product_url: $jsonLd['url'] ?? $url,
|
||||||
)];
|
)];
|
||||||
}
|
}
|
||||||
|
|
@ -189,8 +196,14 @@ class GenericWebProvider implements InfoProviderInterface
|
||||||
$jsonLdNodes = $dom->filter('script[type="application/ld+json"]');
|
$jsonLdNodes = $dom->filter('script[type="application/ld+json"]');
|
||||||
foreach ($jsonLdNodes as $node) {
|
foreach ($jsonLdNodes as $node) {
|
||||||
$jsonLd = $this->json_decode_forgiving($node->textContent);
|
$jsonLd = $this->json_decode_forgiving($node->textContent);
|
||||||
if (isset($jsonLd['@type']) && $jsonLd['@type'] === 'Product') { //If we find a product use that data
|
//If the content of json-ld is an array, try to find a product inside
|
||||||
return $this->productJsonLdToPart($jsonLd, $canonicalURL, $dom);
|
if (!array_is_list($jsonLd)) {
|
||||||
|
$jsonLd = [$jsonLd];
|
||||||
|
}
|
||||||
|
foreach ($jsonLd as $item) {
|
||||||
|
if (isset($item['@type']) && $item['@type'] === 'Product') {
|
||||||
|
return $this->productJsonLdToPart($item, $canonicalURL, $dom);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue