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

BIN
favicon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB