Cleanup Datamodel get_products
This commit is contained in:
parent
80e38ef7df
commit
79d4861e6d
@ -1,6 +1,8 @@
|
|||||||
<?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';
|
||||||
@ -14,16 +16,33 @@ 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)
|
||||||
@ -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
BIN
favicon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 15 KiB |
Loading…
x
Reference in New Issue
Block a user