'product', 'post_status' => $args['status'] ?? 'publish', 'posts_per_page' => (int) ($args['per_page'] ?? 20), 'paged' => (int) ($args['page'] ?? 1)]; if (!empty($args['search'])) $qArgs['s'] = $args['search']; $query = new \WP_Query($qArgs); $products = []; foreach ($query->posts as $p) { $product = wc_get_product($p->ID); $products[] = ['id' => $p->ID, 'name' => $p->post_title, 'status' => $p->post_status, 'price' => $product ? $product->get_price() : null, 'sku' => $product ? $product->get_sku() : null, 'stock' => $product ? $product->get_stock_quantity() : null, 'type' => $product ? $product->get_type() : 'simple']; } return ['products' => $products, 'total' => $query->found_posts]; } public static function getProduct(array $args): array { $product = wc_get_product((int) $args['product_id']); if (!$product) throw new \RuntimeException("Product not found: {$args['product_id']}"); return ['id' => $product->get_id(), 'name' => $product->get_name(), 'type' => $product->get_type(), 'status' => $product->get_status(), 'sku' => $product->get_sku(), 'regular_price' => $product->get_regular_price(), 'sale_price' => $product->get_sale_price(), 'price' => $product->get_price(), 'stock_quantity' => $product->get_stock_quantity(), 'manage_stock' => $product->get_manage_stock(), 'description' => $product->get_description(), 'short_description' => $product->get_short_description(), 'categories' => wp_get_post_terms($product->get_id(), 'product_cat', ['fields' => 'names'])]; } public static function createProduct(array $args): array { $product = new \WC_Product_Simple(); $product->set_name($args['name']); $product->set_regular_price($args['regular_price']); $product->set_status($args['status'] ?? 'draft'); if (!empty($args['description'])) $product->set_description($args['description']); if (!empty($args['sku'])) $product->set_sku($args['sku']); if (isset($args['stock_quantity'])) { $product->set_manage_stock(true); $product->set_stock_quantity((int) $args['stock_quantity']); } $id = $product->save(); return ['id' => $id, 'name' => $args['name']]; } public static function updateProduct(array $args): array { $product = wc_get_product((int) $args['product_id']); if (!$product) throw new \RuntimeException("Product not found: {$args['product_id']}"); if (isset($args['name'])) $product->set_name($args['name']); if (isset($args['regular_price'])) $product->set_regular_price($args['regular_price']); if (isset($args['sale_price'])) $product->set_sale_price($args['sale_price']); if (isset($args['status'])) $product->set_status($args['status']); if (isset($args['description'])) $product->set_description($args['description']); if (isset($args['stock_quantity'])) { $product->set_manage_stock(true); $product->set_stock_quantity((int) $args['stock_quantity']); } $product->save(); return ['id' => $product->get_id(), 'updated' => true]; } public static function listOrders(array $args): array { $qArgs = ['limit' => (int) ($args['per_page'] ?? 20), 'paged' => (int) ($args['page'] ?? 1), 'status' => $args['status'] ?? 'any']; if (!empty($args['customer_id'])) $qArgs['customer_id'] = (int) $args['customer_id']; $orders = wc_get_orders($qArgs); return ['orders' => array_map(fn($o) => ['id' => $o->get_id(), 'status' => $o->get_status(), 'total' => $o->get_total(), 'currency' => $o->get_currency(), 'customer_id' => $o->get_customer_id(), 'date' => $o->get_date_created()->date('Y-m-d H:i:s'), 'items_count' => $o->get_item_count()], $orders)]; } public static function getOrder(array $args): array { $order = wc_get_order((int) $args['order_id']); if (!$order) throw new \RuntimeException("Order not found: {$args['order_id']}"); $items = []; foreach ($order->get_items() as $item) { $items[] = ['product_id' => $item->get_product_id(), 'name' => $item->get_name(), 'qty' => $item->get_quantity(), 'total' => $item->get_total()]; } return ['id' => $order->get_id(), 'status' => $order->get_status(), 'total' => $order->get_total(), 'subtotal' => $order->get_subtotal(), 'currency' => $order->get_currency(), 'customer_id' => $order->get_customer_id(), 'billing' => $order->get_address('billing'), 'shipping' => $order->get_address('shipping'), 'items' => $items, 'date' => $order->get_date_created()->date('Y-m-d H:i:s'), 'note' => $order->get_customer_note()]; } public static function updateOrderStatus(array $args): array { $order = wc_get_order((int) $args['order_id']); if (!$order) throw new \RuntimeException("Order not found: {$args['order_id']}"); $order->update_status($args['status']); return ['id' => $order->get_id(), 'status' => $args['status'], 'updated' => true]; } public static function listCustomers(array $args): array { $qArgs = ['role' => 'customer', 'number' => (int) ($args['per_page'] ?? 20), 'paged' => (int) ($args['page'] ?? 1)]; if (!empty($args['search'])) $qArgs['search'] = '*' . $args['search'] . '*'; $query = new \WP_User_Query($qArgs); $customers = []; foreach ($query->get_results() as $u) { $customer = new \WC_Customer($u->ID); $customers[] = ['id' => $u->ID, 'name' => $u->display_name, 'email' => $u->user_email, 'orders_count' => $customer->get_order_count(), 'total_spent' => $customer->get_total_spent()]; } return ['customers' => $customers, 'total' => $query->get_total()]; } public static function getStoreStats(array $args): array { $period = $args['period'] ?? 'month'; $after = match($period) { 'day' => date('Y-m-d', strtotime('-1 day')), 'week' => date('Y-m-d', strtotime('-7 days')), 'year' => date('Y-m-d', strtotime('-1 year')), default => date('Y-m-01'), // month }; $orders = wc_get_orders(['status' => ['completed', 'processing'], 'date_after' => $after, 'limit' => -1]); $revenue = array_sum(array_map(fn($o) => (float) $o->get_total(), $orders)); return ['period' => $period, 'revenue' => $revenue, 'currency' => get_woocommerce_currency(), 'orders_count' => count($orders), 'products_count' => wp_count_posts('product')->publish ?? 0]; } }