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

@@ -600,14 +600,15 @@ class Informatiq_SP_Admin {
</p>
<div id="informatiq-sp-comparison-results" style="display: none;">
<div id="informatiq-sp-comparison-summary" class="notice notice-info" style="margin: 10px 0; padding: 10px;"></div>
<table class="widefat striped">
<thead>
<tr>
<th><?php esc_html_e( 'Product', 'informatiq-smart-pricing' ); ?></th>
<th><?php esc_html_e( 'SKU', 'informatiq-smart-pricing' ); ?></th>
<th><?php esc_html_e( 'Local Price', 'informatiq-smart-pricing' ); ?></th>
<th><?php esc_html_e( 'Google Price', 'informatiq-smart-pricing' ); ?></th>
<th><?php esc_html_e( 'Google Match', 'informatiq-smart-pricing' ); ?></th>
<th><?php esc_html_e( 'Your Price', 'informatiq-smart-pricing' ); ?></th>
<th><?php esc_html_e( 'Competitor', 'informatiq-smart-pricing' ); ?></th>
<th><?php esc_html_e( 'Recommended', 'informatiq-smart-pricing' ); ?></th>
<th><?php esc_html_e( 'Potential +/-', 'informatiq-smart-pricing' ); ?></th>
<th><?php esc_html_e( 'Status', 'informatiq-smart-pricing' ); ?></th>
</tr>
</thead>
@@ -829,16 +830,14 @@ class Informatiq_SP_Admin {
$this->logger
);
// Get all Google products indexed by various identifiers.
// Get benchmark prices from Price Competitiveness Report.
$benchmark_data = $google_api->get_all_benchmark_prices();
$this->logger->info( sprintf( 'Loaded %d benchmark prices from Google', count( $benchmark_data ) ) );
// Also get all products for matching purposes.
$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 (top-level field).
if ( ! empty( $gp['offerId'] ) ) {
@@ -858,11 +857,6 @@ class Informatiq_SP_Admin {
$google_products['gtin_' . $gtin_value] = $gp;
}
}
// Index by MPN if available.
if ( ! empty( $gp['attributes']['mpn'] ) ) {
$google_products['mpn_' . $gp['attributes']['mpn']] = $gp;
}
}
// Get WooCommerce in-stock products (limit to 50 for performance).
@@ -895,12 +889,19 @@ class Informatiq_SP_Admin {
$local_price = wc_get_price_including_tax( $product, array( 'price' => $base_price ) );
}
// Try to find matching Google product.
// Try to find matching Google product and benchmark.
$google_product = null;
$benchmark_info = null;
$match_type = '';
$product_id = $product->get_id();
// Try offerId match.
if ( isset( $google_products['offer_' . $sku] ) ) {
// Try offerId match (using WooCommerce product ID).
if ( isset( $google_products['offer_' . $product_id] ) ) {
$google_product = $google_products['offer_' . $product_id];
$match_type = 'product_id';
}
// Try offerId match with SKU.
elseif ( isset( $google_products['offer_' . $sku] ) ) {
$google_product = $google_products['offer_' . $sku];
$match_type = 'offerId';
}
@@ -910,26 +911,64 @@ class Informatiq_SP_Admin {
$match_type = 'gtin';
}
// Get Google price (nested in attributes in new Merchant API).
$google_price = null;
// Get benchmark data (competitor price).
if ( isset( $benchmark_data['offer_' . $product_id] ) ) {
$benchmark_info = $benchmark_data['offer_' . $product_id];
} elseif ( isset( $benchmark_data['offer_' . $sku] ) ) {
$benchmark_info = $benchmark_data['offer_' . $sku];
} elseif ( isset( $benchmark_data['gtin_' . $sku] ) ) {
$benchmark_info = $benchmark_data['gtin_' . $sku];
}
// Get Google's own price from product data.
$google_own_price = null;
if ( $google_product ) {
if ( isset( $google_product['attributes']['price']['amountMicros'] ) ) {
$google_price = (float) $google_product['attributes']['price']['amountMicros'] / 1000000;
} elseif ( isset( $google_product['price']['amountMicros'] ) ) {
$google_price = (float) $google_product['price']['amountMicros'] / 1000000;
$google_own_price = (float) $google_product['attributes']['price']['amountMicros'] / 1000000;
}
}
// Get competitor benchmark price.
$benchmark_price = $benchmark_info['benchmark_price'] ?? null;
// Calculate recommended price (slightly below competitor).
$recommended_price = null;
$potential_gain = null;
$price_status = 'unknown';
if ( $benchmark_price && $local_price ) {
// Recommended: 20 cents below competitor (or 0.5% whichever is less).
$offset = min( 0.20, $benchmark_price * 0.005 );
$recommended_price = round( $benchmark_price - $offset, 2 );
// Calculate potential gain per unit.
$potential_gain = round( $recommended_price - $local_price, 2 );
// Determine status.
if ( $local_price < $benchmark_price * 0.98 ) {
$price_status = 'cheaper'; // More than 2% cheaper - could increase.
} elseif ( $local_price <= $benchmark_price ) {
$price_status = 'competitive'; // Within 2% below - good.
} else {
$price_status = 'expensive'; // Above competitor.
}
}
$comparison[] = array(
'id' => $product->get_id(),
'name' => $product->get_name(),
'sku' => $sku,
'local_price' => $local_price ? (float) $local_price : null,
'price_type' => $price_type,
'google_price' => $google_price,
'match_type' => $match_type,
'found' => ! empty( $google_product ),
'google_offer' => $google_product ? ( $google_product['offerId'] ?? '' ) : '',
'id' => $product_id,
'name' => $product->get_name(),
'sku' => $sku,
'local_price' => $local_price ? (float) $local_price : null,
'price_type' => $price_type,
'google_own_price' => $google_own_price,
'benchmark_price' => $benchmark_price,
'recommended_price' => $recommended_price,
'potential_gain' => $potential_gain,
'price_status' => $price_status,
'match_type' => $match_type,
'found' => ! empty( $google_product ),
'has_benchmark' => ! empty( $benchmark_price ),
'google_offer' => $google_product ? ( $google_product['offerId'] ?? '' ) : '',
);
}