Add competitor benchmark pricing and profit optimization

Features:
- Fetch competitor benchmark prices from Google Price Competitiveness Report
- New get_all_benchmark_prices() method in Google API class
- Display competitor price instead of own Google price
- Calculate recommended price (slightly below competitor)
- Show potential gain/loss per product if price is optimized
- Color-coded status:
  - Green: Your price is cheaper (opportunity to increase)
  - Blue: Competitive (within 2% of competitor)
  - Red: Expensive (above competitor)
- Summary statistics showing:
  - Products with benchmark data
  - Count by status (cheaper/competitive/expensive)
  - Total potential gain if all prices optimized

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-23 18:05:17 +01:00
parent 848a29dfa7
commit 4a34578e59
3 changed files with 235 additions and 51 deletions

View File

@@ -458,6 +458,88 @@ class Informatiq_SP_Google_API {
}
}
/**
* Get all benchmark prices from Price Competitiveness Report.
*
* @return array Associative array of offer_id => benchmark_price.
*/
public function get_all_benchmark_prices() {
try {
$endpoint = "/reports/v1beta/accounts/{$this->merchant_id}/reports:search";
$all_benchmarks = array();
$page_token = null;
do {
$query = array(
'query' => "SELECT
offer_id,
product_view.gtin,
price_benchmark.price_benchmark_value,
price_benchmark.price_benchmark_currency_code,
product_view.price,
product_view.title
FROM PriceCompetitivenessProductView",
);
if ( $page_token ) {
$query['pageToken'] = $page_token;
}
$response = $this->api_request( 'POST', $endpoint, $query );
if ( ! empty( $response['results'] ) ) {
foreach ( $response['results'] as $result ) {
$offer_id = $result['offerId'] ?? null;
$gtin = $result['productView']['gtin'] ?? null;
$benchmark_price = null;
if ( isset( $result['priceBenchmark']['priceBenchmarkValue']['amountMicros'] ) ) {
$benchmark_price = (float) $result['priceBenchmark']['priceBenchmarkValue']['amountMicros'] / 1000000;
}
$own_price = null;
if ( isset( $result['productView']['price']['amountMicros'] ) ) {
$own_price = (float) $result['productView']['price']['amountMicros'] / 1000000;
}
$title = $result['productView']['title'] ?? '';
if ( $offer_id && $benchmark_price ) {
$all_benchmarks[ 'offer_' . $offer_id ] = array(
'benchmark_price' => $benchmark_price,
'own_price' => $own_price,
'title' => $title,
'gtin' => $gtin,
);
// Also index by GTIN if available.
if ( $gtin ) {
$all_benchmarks[ 'gtin_' . $gtin ] = array(
'benchmark_price' => $benchmark_price,
'own_price' => $own_price,
'title' => $title,
'offer_id' => $offer_id,
);
}
}
}
}
$page_token = $response['nextPageToken'] ?? null;
} while ( $page_token );
$this->logger->info( sprintf( 'Fetched benchmark prices for %d products', count( $all_benchmarks ) ) );
return $all_benchmarks;
} catch ( Exception $e ) {
$this->logger->error( 'Error fetching benchmark prices: ' . $e->getMessage() );
return array();
}
}
/**
* Test API connection.
*