diff --git a/admin/class-informatiq-sp-admin.php b/admin/class-informatiq-sp-admin.php index fc4e8f78..2f01942c 100644 --- a/admin/class-informatiq-sp-admin.php +++ b/admin/class-informatiq-sp-admin.php @@ -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 ) { diff --git a/assets/js/admin.js b/assets/js/admin.js index 27eb5677..1e2b7b0b 100644 --- a/assets/js/admin.js +++ b/assets/js/admin.js @@ -202,7 +202,6 @@ console.log('Informatiq Smart Pricing: Script loaded'); e.preventDefault(); console.log('=== COMPARE PRODUCTS CLICKED ==='); - alert('Compare Products button clicked! Check console for details.'); var $button = $(this); var $spinner = $button.next('.spinner'); @@ -244,6 +243,16 @@ console.log('Informatiq Smart Pricing: Script loaded'); success: function(response) { console.log('AJAX success callback'); console.log('AJAX response:', response); + + // Log debug info if available. + if (response.data && response.data.debug) { + console.log('=== DEBUG INFO ==='); + console.log('Sample Google product:', response.data.debug.sample_google_product); + console.log('Sample indexed keys:', response.data.debug.sample_google_keys); + console.log('Total indexed entries:', response.data.debug.index_count); + console.log('=================='); + } + $status.hide(); if (response.success) {