Add debug logging and expand Google product matching

- Log sample Google product structure to help debug matching
- Expand matching to check nested 'attributes' field (new API format)
- Extract offerId from productId field (channel:lang:country:id format)
- Extract offerId from name field (resource path format)
- Return debug info in AJAX response (sample keys, sample product)
- Log debug info in browser console
- Remove alert popup since button is now working

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-23 17:45:35 +01:00
parent 7ab4505fc8
commit e2ea95f445
2 changed files with 59 additions and 3 deletions

View File

@@ -833,15 +833,54 @@ class Informatiq_SP_Admin {
$google_products_raw = $google_api->get_all_products();
$google_products = array();
// Log first product structure for debugging.
if ( ! empty( $google_products_raw[0] ) ) {
$this->logger->info( 'Sample Google product structure: ' . wp_json_encode( array_keys( $google_products_raw[0] ) ) );
$this->logger->info( 'Sample Google product data: ' . wp_json_encode( $google_products_raw[0] ) );
}
foreach ( $google_products_raw as $gp ) {
// Index by offerId.
// Index by offerId (direct field).
if ( ! empty( $gp['offerId'] ) ) {
$google_products['offer_' . $gp['offerId']] = $gp;
}
// Index by gtin.
// 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;
}
}
// 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;
}
}
}
}
// Get WooCommerce in-stock products (limit to 50 for performance).
@@ -903,11 +942,19 @@ class Informatiq_SP_Admin {
);
}
// Get sample of indexed keys for debugging.
$sample_keys = array_slice( array_keys( $google_products ), 0, 10 );
wp_send_json_success( array(
'products' => $comparison,
'google_count' => count( $google_products_raw ),
'wc_count' => count( $wc_products ),
'currency' => get_woocommerce_currency_symbol(),
'debug' => array(
'sample_google_keys' => $sample_keys,
'sample_google_product' => ! empty( $google_products_raw[0] ) ? $google_products_raw[0] : null,
'index_count' => count( $google_products ),
),
) );
} catch ( Exception $e ) {