Fix Google product matching - GTIN is array in attributes

Based on actual API response analysis:
- offerId is at top level (e.g., "220216" = WooCommerce product ID)
- gtin is nested in attributes.gtin as an ARRAY (e.g., ["850018802833"])
- price is nested in attributes.price.amountMicros

Changes:
- Index Google products by each GTIN in the attributes.gtin array
- Also check attributes.gtins (alternative field name)
- Index by MPN if available
- Update find_product_by_identifier to check array GTINs with in_array()
- Fix price extraction to check attributes.price first, then top-level

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-23 17:50:30 +01:00
parent e2ea95f445
commit 6209229e06
2 changed files with 49 additions and 42 deletions

View File

@@ -840,47 +840,29 @@ class Informatiq_SP_Admin {
}
foreach ( $google_products_raw as $gp ) {
// Index by offerId (direct field).
// Index by offerId (top-level field).
if ( ! empty( $gp['offerId'] ) ) {
$google_products['offer_' . $gp['offerId']] = $gp;
}
// Index by gtin (direct field).
if ( ! empty( $gp['gtin'] ) ) {
$google_products['gtin_' . $gp['gtin']] = $gp;
}
// Also check nested attributes structure (new Merchant API format).
if ( ! empty( $gp['attributes']['offerId'] ) ) {
$google_products['offer_' . $gp['attributes']['offerId']] = $gp;
}
if ( ! empty( $gp['attributes']['gtin'] ) ) {
$google_products['gtin_' . $gp['attributes']['gtin']] = $gp;
}
// Check productId which might contain the identifier.
if ( ! empty( $gp['productId'] ) ) {
// productId format is usually: online:en:US:SKU or channel:language:country:offerId
$parts = explode( ':', $gp['productId'] );
if ( count( $parts ) >= 4 ) {
$extracted_offer = $parts[ count( $parts ) - 1 ];
$google_products['offer_' . $extracted_offer] = $gp;
// Index by GTIN - check attributes.gtin which is an ARRAY.
if ( ! empty( $gp['attributes']['gtin'] ) && is_array( $gp['attributes']['gtin'] ) ) {
foreach ( $gp['attributes']['gtin'] as $gtin_value ) {
$google_products['gtin_' . $gtin_value] = $gp;
}
}
// Check name field which contains full resource path.
if ( ! empty( $gp['name'] ) ) {
// name format: accounts/{account}/products/{product_id}
// product_id format: online~en~US~SKU
if ( preg_match( '/products\/(.+)$/', $gp['name'], $matches ) ) {
$product_id = $matches[1];
$parts = explode( '~', $product_id );
if ( count( $parts ) >= 4 ) {
$extracted_offer = $parts[ count( $parts ) - 1 ];
$google_products['offer_' . $extracted_offer] = $gp;
}
// Also check attributes.gtins (alternative field name).
if ( ! empty( $gp['attributes']['gtins'] ) && is_array( $gp['attributes']['gtins'] ) ) {
foreach ( $gp['attributes']['gtins'] as $gtin_value ) {
$google_products['gtin_' . $gtin_value] = $gp;
}
}
// Index by MPN if available.
if ( ! empty( $gp['attributes']['mpn'] ) ) {
$google_products['mpn_' . $gp['attributes']['mpn']] = $gp;
}
}
// Get WooCommerce in-stock products (limit to 50 for performance).
@@ -921,10 +903,12 @@ class Informatiq_SP_Admin {
$match_type = 'gtin';
}
// Get Google price.
// Get Google price (nested in attributes in new Merchant API).
$google_price = null;
if ( $google_product ) {
if ( isset( $google_product['price']['amountMicros'] ) ) {
if ( isset( $google_product['attributes']['price']['amountMicros'] ) ) {
$google_price = (float) $google_product['attributes']['price']['amountMicros'] / 1000000;
} elseif ( isset( $google_product['price']['amountMicros'] ) ) {
$google_price = (float) $google_product['price']['amountMicros'] / 1000000;
}
}