This commit is contained in:
2026-05-13 23:17:22 +02:00
parent 9fb1db3006
commit b2c2ca0946
3 changed files with 299 additions and 43 deletions

146
api.php
View File

@@ -775,7 +775,140 @@ if ( \S::get( 'action' ) == 'products_unoptimized_list' )
] );
}
// Lista produktow bez unit pricing dla kategorii beauty/cosmetics
// Lista produktow bez zoptymalizowanego tytulu GMC
if ( \S::get( 'action' ) == 'products_get_missing_title' )
{
api_validate_api_key( $mdb );
$client_id_param = (int) \S::get( 'client_id' );
$limit = (int) \S::get( 'limit' );
if ( $client_id_param <= 0 )
{
api_json_response( [ 'result' => 'error', 'message' => 'Missing required param: client_id' ], 422 );
}
if ( $limit <= 0 || $limit > 200 )
{
$limit = 50;
}
$rows = $mdb -> query(
'SELECT p.id, p.offer_id, p.title AS name, p.title_gmc AS title, p.google_product_category,
COALESCE( SUM( pa.clicks_all_time ), 0 ) AS clicks_all_time,
COALESCE( SUM( pa.impressions_all_time ), 0 ) AS impressions_all_time,
COALESCE( SUM( pa.cost_all_time ), 0 ) AS cost_all_time
FROM products p
LEFT JOIN products_aggregate pa ON pa.product_id = p.id
WHERE p.client_id = :client_id
AND TRIM( COALESCE( p.offer_id, \'\' ) ) <> \'\'
AND (
p.title_gmc IS NULL OR TRIM( p.title_gmc ) = \'\' OR TRIM( p.title_gmc ) = TRIM( p.title )
)
GROUP BY p.id
ORDER BY clicks_all_time DESC, impressions_all_time DESC
LIMIT :limit',
[
':client_id' => (int) $client_id_param,
':limit' => (int) $limit
]
) -> fetchAll( \PDO::FETCH_ASSOC );
$products = [];
foreach ( $rows as $row )
{
$base_name = trim( (string) ( $row['name'] ?? '' ) );
$custom_title = trim( (string) ( $row['title'] ?? '' ) );
$google_category = trim( (string) ( $row['google_product_category'] ?? '' ) );
$products[] = [
'offer_id' => (string) ( $row['offer_id'] ?? '' ),
'default_name' => $base_name,
'custom_title' => $custom_title !== '' ? $custom_title : null,
'title_changed' => false,
'google_product_category' => $google_category !== '' ? $google_category : null,
'needs_title' => true,
'needs_category' => $google_category === '',
'clicks' => (int) $row['clicks_all_time'],
'impressions' => (int) $row['impressions_all_time'],
'cost' => round( (float) $row['cost_all_time'], 2 )
];
}
api_json_response( [
'result' => 'ok',
'client_id' => $client_id_param,
'count' => count( $products ),
'products' => $products
] );
}
// Lista produktow bez kategorii Google
if ( \S::get( 'action' ) == 'products_get_missing_google_category' )
{
api_validate_api_key( $mdb );
$client_id_param = (int) \S::get( 'client_id' );
$limit = (int) \S::get( 'limit' );
if ( $client_id_param <= 0 )
{
api_json_response( [ 'result' => 'error', 'message' => 'Missing required param: client_id' ], 422 );
}
if ( $limit <= 0 || $limit > 200 )
{
$limit = 50;
}
$rows = $mdb -> query(
'SELECT p.id, p.offer_id, p.title AS name, p.title_gmc AS title, p.google_product_category,
COALESCE( SUM( pa.clicks_all_time ), 0 ) AS clicks_all_time,
COALESCE( SUM( pa.impressions_all_time ), 0 ) AS impressions_all_time,
COALESCE( SUM( pa.cost_all_time ), 0 ) AS cost_all_time
FROM products p
LEFT JOIN products_aggregate pa ON pa.product_id = p.id
WHERE p.client_id = :client_id
AND TRIM( COALESCE( p.offer_id, \'\' ) ) <> \'\'
AND ( p.google_product_category IS NULL OR TRIM( p.google_product_category ) = \'\' )
GROUP BY p.id
ORDER BY clicks_all_time DESC, impressions_all_time DESC
LIMIT :limit',
[
':client_id' => (int) $client_id_param,
':limit' => (int) $limit
]
) -> fetchAll( \PDO::FETCH_ASSOC );
$products = [];
foreach ( $rows as $row )
{
$base_name = trim( (string) ( $row['name'] ?? '' ) );
$custom_title = trim( (string) ( $row['title'] ?? '' ) );
$products[] = [
'offer_id' => (string) ( $row['offer_id'] ?? '' ),
'default_name' => $base_name,
'custom_title' => $custom_title !== '' ? $custom_title : null,
'title_changed' => $custom_title !== '' && $custom_title !== $base_name,
'google_product_category' => null,
'needs_title' => !($custom_title !== '' && $custom_title !== $base_name),
'needs_category' => true,
'clicks' => (int) $row['clicks_all_time'],
'impressions' => (int) $row['impressions_all_time'],
'cost' => round( (float) $row['cost_all_time'], 2 )
];
}
api_json_response( [
'result' => 'ok',
'client_id' => $client_id_param,
'count' => count( $products ),
'products' => $products
] );
}
// Lista produktow bez unit pricing; opcjonalnie filtrowana po kategorii Google
if ( \S::get( 'action' ) == 'products_get_missing_unit_pricing' )
{
api_validate_api_key( $mdb );
@@ -808,17 +941,6 @@ if ( \S::get( 'action' ) == 'products_get_missing_unit_pricing' )
$where_sql .= ' AND p.google_product_category LIKE :category_filter';
$params[':category_filter'] = '%' . $category_filter . '%';
}
else
{
$where_sql .= " AND (
p.google_product_category LIKE '%Beauty%'
OR p.google_product_category LIKE '%Health%'
OR p.google_product_category LIKE '%Cosmetic%'
OR p.google_product_category LIKE '%Skin Care%'
OR p.google_product_category LIKE '%Higiena%'
OR p.google_product_category LIKE '%Pielegnacj%'
)";
}
$rows = $mdb -> query(
'SELECT p.id,