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:
@@ -407,13 +407,12 @@ 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.
|
||||
// Try searching by offer_id first (snake_case table name for new API).
|
||||
$query = array(
|
||||
'query' => "SELECT
|
||||
offer_id,
|
||||
price_benchmark.price_benchmark_value,
|
||||
price_benchmark.price_benchmark_currency_code
|
||||
FROM PriceCompetitivenessProductView
|
||||
benchmark_price
|
||||
FROM price_competitiveness_product_view
|
||||
WHERE offer_id = '{$sku}'",
|
||||
);
|
||||
|
||||
@@ -421,8 +420,11 @@ class Informatiq_SP_Google_API {
|
||||
|
||||
if ( ! empty( $response['results'] ) ) {
|
||||
foreach ( $response['results'] as $result ) {
|
||||
if ( isset( $result['priceBenchmark']['priceBenchmarkValue']['amountMicros'] ) ) {
|
||||
return (float) $result['priceBenchmark']['priceBenchmarkValue']['amountMicros'] / 1000000;
|
||||
if ( isset( $result['benchmarkPrice']['amountMicros'] ) ) {
|
||||
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' => "SELECT
|
||||
gtin,
|
||||
price_benchmark.price_benchmark_value,
|
||||
price_benchmark.price_benchmark_currency_code
|
||||
FROM PriceCompetitivenessProductView
|
||||
benchmark_price
|
||||
FROM price_competitiveness_product_view
|
||||
WHERE gtin = '{$sku}'",
|
||||
);
|
||||
|
||||
@@ -442,8 +443,11 @@ class Informatiq_SP_Google_API {
|
||||
|
||||
if ( ! empty( $response['results'] ) ) {
|
||||
foreach ( $response['results'] as $result ) {
|
||||
if ( isset( $result['priceBenchmark']['priceBenchmarkValue']['amountMicros'] ) ) {
|
||||
return (float) $result['priceBenchmark']['priceBenchmarkValue']['amountMicros'] / 1000000;
|
||||
if ( isset( $result['benchmarkPrice']['amountMicros'] ) ) {
|
||||
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;
|
||||
|
||||
do {
|
||||
// New Merchant API uses snake_case table names.
|
||||
$query = array(
|
||||
'query' => "SELECT
|
||||
id,
|
||||
offer_id,
|
||||
product_view.gtin,
|
||||
price_benchmark.price_benchmark_value,
|
||||
price_benchmark.price_benchmark_currency_code,
|
||||
product_view.price,
|
||||
product_view.title
|
||||
FROM PriceCompetitivenessProductView",
|
||||
gtin,
|
||||
title,
|
||||
price,
|
||||
benchmark_price
|
||||
FROM price_competitiveness_product_view",
|
||||
);
|
||||
|
||||
if ( $page_token ) {
|
||||
@@ -488,23 +493,35 @@ class Informatiq_SP_Google_API {
|
||||
|
||||
$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'] ) ) {
|
||||
foreach ( $response['results'] as $result ) {
|
||||
$offer_id = $result['offerId'] ?? null;
|
||||
$gtin = $result['productView']['gtin'] ?? null;
|
||||
// Extract data - field names in response use camelCase.
|
||||
$offer_id = $result['offerId'] ?? ( $result['offer_id'] ?? null );
|
||||
$gtin = $result['gtin'] ?? null;
|
||||
$title = $result['title'] ?? '';
|
||||
|
||||
$benchmark_price = null;
|
||||
if ( isset( $result['priceBenchmark']['priceBenchmarkValue']['amountMicros'] ) ) {
|
||||
$benchmark_price = (float) $result['priceBenchmark']['priceBenchmarkValue']['amountMicros'] / 1000000;
|
||||
// Try different possible response structures.
|
||||
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;
|
||||
if ( isset( $result['productView']['price']['amountMicros'] ) ) {
|
||||
$own_price = (float) $result['productView']['price']['amountMicros'] / 1000000;
|
||||
if ( isset( $result['price']['amountMicros'] ) ) {
|
||||
$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 ) {
|
||||
$all_benchmarks[ 'offer_' . $offer_id ] = array(
|
||||
'benchmark_price' => $benchmark_price,
|
||||
|
||||
Reference in New Issue
Block a user