Fix Merchant API table name - use snake_case

The new Merchant API uses snake_case table names:
- Changed from 'PriceCompetitivenessProductView' to 'price_competitiveness_product_view'
- Updated field names in SELECT query
- Added debug logging to see actual API response structure
- Handle both camelCase and snake_case field names in response

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-23 18:08:38 +01:00
parent 4a34578e59
commit 0d1bb72214

View File

@@ -407,13 +407,12 @@ class Informatiq_SP_Google_API {
// Use the Reports API to search for competitive visibility data. // Use the Reports API to search for competitive visibility data.
$endpoint = "/reports/v1beta/accounts/{$this->merchant_id}/reports:search"; $endpoint = "/reports/v1beta/accounts/{$this->merchant_id}/reports:search";
// Try searching by offer_id first. // Try searching by offer_id first (snake_case table name for new API).
$query = array( $query = array(
'query' => "SELECT 'query' => "SELECT
offer_id, offer_id,
price_benchmark.price_benchmark_value, benchmark_price
price_benchmark.price_benchmark_currency_code FROM price_competitiveness_product_view
FROM PriceCompetitivenessProductView
WHERE offer_id = '{$sku}'", WHERE offer_id = '{$sku}'",
); );
@@ -421,8 +420,11 @@ class Informatiq_SP_Google_API {
if ( ! empty( $response['results'] ) ) { if ( ! empty( $response['results'] ) ) {
foreach ( $response['results'] as $result ) { foreach ( $response['results'] as $result ) {
if ( isset( $result['priceBenchmark']['priceBenchmarkValue']['amountMicros'] ) ) { if ( isset( $result['benchmarkPrice']['amountMicros'] ) ) {
return (float) $result['priceBenchmark']['priceBenchmarkValue']['amountMicros'] / 1000000; return (float) $result['benchmarkPrice']['amountMicros'] / 1000000;
}
if ( isset( $result['benchmark_price']['amountMicros'] ) ) {
return (float) $result['benchmark_price']['amountMicros'] / 1000000;
} }
} }
} }
@@ -432,9 +434,8 @@ class Informatiq_SP_Google_API {
$query = array( $query = array(
'query' => "SELECT 'query' => "SELECT
gtin, gtin,
price_benchmark.price_benchmark_value, benchmark_price
price_benchmark.price_benchmark_currency_code FROM price_competitiveness_product_view
FROM PriceCompetitivenessProductView
WHERE gtin = '{$sku}'", WHERE gtin = '{$sku}'",
); );
@@ -442,8 +443,11 @@ class Informatiq_SP_Google_API {
if ( ! empty( $response['results'] ) ) { if ( ! empty( $response['results'] ) ) {
foreach ( $response['results'] as $result ) { foreach ( $response['results'] as $result ) {
if ( isset( $result['priceBenchmark']['priceBenchmarkValue']['amountMicros'] ) ) { if ( isset( $result['benchmarkPrice']['amountMicros'] ) ) {
return (float) $result['priceBenchmark']['priceBenchmarkValue']['amountMicros'] / 1000000; return (float) $result['benchmarkPrice']['amountMicros'] / 1000000;
}
if ( isset( $result['benchmark_price']['amountMicros'] ) ) {
return (float) $result['benchmark_price']['amountMicros'] / 1000000;
} }
} }
} }
@@ -471,15 +475,16 @@ class Informatiq_SP_Google_API {
$page_token = null; $page_token = null;
do { do {
// New Merchant API uses snake_case table names.
$query = array( $query = array(
'query' => "SELECT 'query' => "SELECT
id,
offer_id, offer_id,
product_view.gtin, gtin,
price_benchmark.price_benchmark_value, title,
price_benchmark.price_benchmark_currency_code, price,
product_view.price, benchmark_price
product_view.title FROM price_competitiveness_product_view",
FROM PriceCompetitivenessProductView",
); );
if ( $page_token ) { if ( $page_token ) {
@@ -488,23 +493,35 @@ class Informatiq_SP_Google_API {
$response = $this->api_request( 'POST', $endpoint, $query ); $response = $this->api_request( 'POST', $endpoint, $query );
$this->logger->info( 'Benchmark API response keys: ' . wp_json_encode( array_keys( $response ) ) );
if ( ! empty( $response['results'][0] ) ) {
$this->logger->info( 'Sample benchmark result: ' . wp_json_encode( $response['results'][0] ) );
}
if ( ! empty( $response['results'] ) ) { if ( ! empty( $response['results'] ) ) {
foreach ( $response['results'] as $result ) { foreach ( $response['results'] as $result ) {
$offer_id = $result['offerId'] ?? null; // Extract data - field names in response use camelCase.
$gtin = $result['productView']['gtin'] ?? null; $offer_id = $result['offerId'] ?? ( $result['offer_id'] ?? null );
$gtin = $result['gtin'] ?? null;
$title = $result['title'] ?? '';
$benchmark_price = null; $benchmark_price = null;
if ( isset( $result['priceBenchmark']['priceBenchmarkValue']['amountMicros'] ) ) { // Try different possible response structures.
$benchmark_price = (float) $result['priceBenchmark']['priceBenchmarkValue']['amountMicros'] / 1000000; 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;
} }
$own_price = null; $own_price = null;
if ( isset( $result['productView']['price']['amountMicros'] ) ) { if ( isset( $result['price']['amountMicros'] ) ) {
$own_price = (float) $result['productView']['price']['amountMicros'] / 1000000; $own_price = (float) $result['price']['amountMicros'] / 1000000;
} elseif ( isset( $result['price']['priceMicros'] ) ) {
$own_price = (float) $result['price']['priceMicros'] / 1000000;
} }
$title = $result['productView']['title'] ?? '';
if ( $offer_id && $benchmark_price ) { if ( $offer_id && $benchmark_price ) {
$all_benchmarks[ 'offer_' . $offer_id ] = array( $all_benchmarks[ 'offer_' . $offer_id ] = array(
'benchmark_price' => $benchmark_price, 'benchmark_price' => $benchmark_price,