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:
@@ -300,9 +300,16 @@ class Informatiq_SP_Google_API {
|
||||
}
|
||||
|
||||
// Fallback: Use product's own price as reference.
|
||||
// Price is nested inside attributes in the new Merchant API.
|
||||
if ( isset( $product['attributes']['price']['amountMicros'] ) ) {
|
||||
$price = (float) $product['attributes']['price']['amountMicros'] / 1000000;
|
||||
$this->logger->warning( "No competitive data available for SKU={$sku}, using own price as reference" );
|
||||
return $price;
|
||||
}
|
||||
// Also check top-level price (in case API changes).
|
||||
if ( isset( $product['price']['amountMicros'] ) ) {
|
||||
$price = (float) $product['price']['amountMicros'] / 1000000;
|
||||
$this->logger->warning( "No competitive data available for SKU={$sku}, using own price as reference" );
|
||||
$this->logger->warning( "No competitive data available for SKU={$sku}, using own price as reference (top-level)" );
|
||||
return $price;
|
||||
}
|
||||
|
||||
@@ -347,15 +354,31 @@ class Informatiq_SP_Google_API {
|
||||
return $product;
|
||||
}
|
||||
|
||||
// Check if SKU matches Google's GTIN (for stores where SKU is the barcode).
|
||||
if ( isset( $product['gtin'] ) && $product['gtin'] === $sku ) {
|
||||
$this->logger->info( "Product matched by GTIN={$sku} (SKU used as barcode)" );
|
||||
return $product;
|
||||
// Check if SKU matches Google's GTIN array (for stores where SKU is the barcode).
|
||||
// GTIN is stored in attributes.gtin as an array.
|
||||
if ( ! empty( $product['attributes']['gtin'] ) && is_array( $product['attributes']['gtin'] ) ) {
|
||||
if ( in_array( $sku, $product['attributes']['gtin'], true ) ) {
|
||||
$this->logger->info( "Product matched by attributes.gtin={$sku} (SKU used as barcode)" );
|
||||
return $product;
|
||||
}
|
||||
}
|
||||
|
||||
// Check if separate GTIN field matches.
|
||||
if ( ! empty( $gtin ) && isset( $product['gtin'] ) && $product['gtin'] === $gtin ) {
|
||||
return $product;
|
||||
// Also check attributes.gtins (alternative field).
|
||||
if ( ! empty( $product['attributes']['gtins'] ) && is_array( $product['attributes']['gtins'] ) ) {
|
||||
if ( in_array( $sku, $product['attributes']['gtins'], true ) ) {
|
||||
$this->logger->info( "Product matched by attributes.gtins={$sku} (SKU used as barcode)" );
|
||||
return $product;
|
||||
}
|
||||
}
|
||||
|
||||
// Check if separate GTIN parameter matches.
|
||||
if ( ! empty( $gtin ) ) {
|
||||
if ( ! empty( $product['attributes']['gtin'] ) && is_array( $product['attributes']['gtin'] ) ) {
|
||||
if ( in_array( $gtin, $product['attributes']['gtin'], true ) ) {
|
||||
$this->logger->info( "Product matched by GTIN parameter={$gtin}" );
|
||||
return $product;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user