Cleanup Datamodel get_products

This commit is contained in:
Mark Pinkster 2026-01-10 16:12:21 +01:00
parent 80e38ef7df
commit 79d4861e6d
2 changed files with 41 additions and 17 deletions

View File

@ -1,6 +1,8 @@
<?php
/**
* Products Action - Get products with enriched data
*
* Optimized to fetch only required fields for telesales app
*/
require_once __DIR__ . '/../services/UpsellService.php';
@ -14,16 +16,33 @@ function handleGetProducts(): void
{
try {
$woocommerce = getWooCommerce();
$products = $woocommerce->get('products', ['status' => 'publish', 'per_page' => 100]);
// Only fetch fields needed for telesales app
$productFields = 'id,name,price,type,upsell_ids,cross_sell_ids';
$variationFields = 'id,price,attributes';
$products = $woocommerce->get('products', [
'status' => 'publish',
'per_page' => 100,
'_fields' => $productFields
]);
$cuw_map = UpsellService::buildProductMap();
$enriched = [];
foreach ($products as $product) {
// Get variations for variable products
$variation_details = ($product->type === 'variable')
? (array) $woocommerce->get("products/{$product->id}/variations", ['per_page' => 50])
: [];
// Get variations for variable products (only needed fields)
$variation_details = [];
if ($product->type === 'variable') {
$variation_details = (array) $woocommerce->get(
"products/{$product->id}/variations",
[
'per_page' => 50,
'_fields' => $variationFields
]
);
}
// Combine upsell + cross-sell + CUW IDs
$upsell_ids = !empty($product->upsell_ids)
@ -41,13 +60,18 @@ function handleGetProducts(): void
$cuw_ids
))));
// Convert product to array and add fields
$p = (array) $product;
$p['variation_details'] = $variation_details;
$p['cuw_ids'] = $cuw_ids;
$p['recommended_ids'] = $recommended_ids;
$enriched[] = $p;
// Build minimal product object with only needed fields
$enriched[] = [
'id' => (int) $product->id,
'name' => $product->name,
'price' => $product->price,
'type' => $product->type,
'upsell_ids' => $upsell_ids,
'cross_sell_ids' => $cross_sell_ids,
'variation_details' => $variation_details,
'cuw_ids' => $cuw_ids,
'recommended_ids' => $recommended_ids
];
}
echo json_encode($enriched);

BIN
favicon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB