From c13efdcb0d3463d66e54e503b5ae0dd48ee97b69 Mon Sep 17 00:00:00 2001 From: Malin Date: Fri, 23 Jan 2026 18:11:06 +0100 Subject: [PATCH] Fix field names with product_view prefix The Merchant API Reports require product fields to be prefixed: - product_view.offer_id instead of offer_id - product_view.gtin instead of gtin - product_view.title, product_view.price, etc. - benchmark_price remains without prefix (it's a report-level field) Also update response parsing to extract from nested productView object. Co-Authored-By: Claude Opus 4.5 --- includes/class-informatiq-sp-google-api.php | 48 ++++++++++----------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/includes/class-informatiq-sp-google-api.php b/includes/class-informatiq-sp-google-api.php index bc3e6287..383efb3a 100644 --- a/includes/class-informatiq-sp-google-api.php +++ b/includes/class-informatiq-sp-google-api.php @@ -407,13 +407,13 @@ class Informatiq_SP_Google_API { // Use the Reports API to search for competitive visibility data. $endpoint = "/reports/v1beta/accounts/{$this->merchant_id}/reports:search"; - // Try searching by offer_id first (snake_case table name for new API). + // Try searching by offer_id first (fields prefixed with product_view.). $query = array( 'query' => "SELECT - offer_id, + product_view.offer_id, benchmark_price FROM price_competitiveness_product_view - WHERE offer_id = '{$sku}'", + WHERE product_view.offer_id = '{$sku}'", ); $response = $this->api_request( 'POST', $endpoint, $query ); @@ -423,9 +423,6 @@ class Informatiq_SP_Google_API { if ( isset( $result['benchmarkPrice']['amountMicros'] ) ) { return (float) $result['benchmarkPrice']['amountMicros'] / 1000000; } - if ( isset( $result['benchmark_price']['amountMicros'] ) ) { - return (float) $result['benchmark_price']['amountMicros'] / 1000000; - } } } @@ -433,10 +430,10 @@ class Informatiq_SP_Google_API { if ( preg_match( '/^\d{8,14}$/', $sku ) ) { $query = array( 'query' => "SELECT - gtin, + product_view.gtin, benchmark_price FROM price_competitiveness_product_view - WHERE gtin = '{$sku}'", + WHERE product_view.gtin = '{$sku}'", ); $response = $this->api_request( 'POST', $endpoint, $query ); @@ -446,9 +443,6 @@ class Informatiq_SP_Google_API { if ( isset( $result['benchmarkPrice']['amountMicros'] ) ) { return (float) $result['benchmarkPrice']['amountMicros'] / 1000000; } - if ( isset( $result['benchmark_price']['amountMicros'] ) ) { - return (float) $result['benchmark_price']['amountMicros'] / 1000000; - } } } } @@ -476,13 +470,14 @@ class Informatiq_SP_Google_API { do { // New Merchant API uses snake_case table names. + // Fields from product are prefixed with product_view. $query = array( 'query' => "SELECT - id, - offer_id, - gtin, - title, - price, + product_view.id, + product_view.offer_id, + product_view.gtin, + product_view.title, + product_view.price, benchmark_price FROM price_competitiveness_product_view", ); @@ -500,26 +495,27 @@ class Informatiq_SP_Google_API { if ( ! empty( $response['results'] ) ) { foreach ( $response['results'] as $result ) { - // Extract data - field names in response use camelCase. - $offer_id = $result['offerId'] ?? ( $result['offer_id'] ?? null ); - $gtin = $result['gtin'] ?? null; - $title = $result['title'] ?? ''; + // Extract data - response nests product fields under productView. + $product_view = $result['productView'] ?? array(); + $offer_id = $product_view['offerId'] ?? ( $product_view['offer_id'] ?? null ); + $gtin = $product_view['gtin'] ?? null; + $title = $product_view['title'] ?? ''; $benchmark_price = null; - // Try different possible response structures. + // Try different possible response structures for benchmark. if ( isset( $result['benchmarkPrice']['amountMicros'] ) ) { $benchmark_price = (float) $result['benchmarkPrice']['amountMicros'] / 1000000; } elseif ( isset( $result['benchmark_price']['amountMicros'] ) ) { $benchmark_price = (float) $result['benchmark_price']['amountMicros'] / 1000000; - } elseif ( isset( $result['benchmarkPrice']['priceMicros'] ) ) { - $benchmark_price = (float) $result['benchmarkPrice']['priceMicros'] / 1000000; + } elseif ( isset( $result['priceBenchmark']['priceBenchmarkValue']['amountMicros'] ) ) { + $benchmark_price = (float) $result['priceBenchmark']['priceBenchmarkValue']['amountMicros'] / 1000000; } $own_price = null; - if ( isset( $result['price']['amountMicros'] ) ) { + if ( isset( $product_view['price']['amountMicros'] ) ) { + $own_price = (float) $product_view['price']['amountMicros'] / 1000000; + } elseif ( isset( $result['price']['amountMicros'] ) ) { $own_price = (float) $result['price']['amountMicros'] / 1000000; - } elseif ( isset( $result['price']['priceMicros'] ) ) { - $own_price = (float) $result['price']['priceMicros'] / 1000000; } if ( $offer_id && $benchmark_price ) {