Cleanup Datamodel get_products
This commit is contained in:
parent
80e38ef7df
commit
79d4861e6d
@ -1,36 +1,55 @@
|
||||
<?php
|
||||
/**
|
||||
* Products Action - Get products with enriched data
|
||||
*
|
||||
* Optimized to fetch only required fields for telesales app
|
||||
*/
|
||||
|
||||
require_once __DIR__ . '/../services/UpsellService.php';
|
||||
|
||||
/**
|
||||
* Handle get_products action
|
||||
*
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
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)
|
||||
? array_map('intval', (array) $product->upsell_ids)
|
||||
$upsell_ids = !empty($product->upsell_ids)
|
||||
? array_map('intval', (array) $product->upsell_ids)
|
||||
: [];
|
||||
$cross_sell_ids = !empty($product->cross_sell_ids)
|
||||
? array_map('intval', (array) $product->cross_sell_ids)
|
||||
$cross_sell_ids = !empty($product->cross_sell_ids)
|
||||
? array_map('intval', (array) $product->cross_sell_ids)
|
||||
: [];
|
||||
|
||||
$cuw_ids = $cuw_map[(int)$product->id] ?? [];
|
||||
@ -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
BIN
favicon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 15 KiB |
Loading…
x
Reference in New Issue
Block a user