register( 'woo_products_list', array( 'description' => 'List WooCommerce products.', 'domain' => 'woocommerce', 'capability' => 'manage_woocommerce', 'inputSchema' => array( 'type' => 'object', 'properties' => array( 'search' => array( 'type' => 'string' ), 'status' => array( 'type' => 'string', 'default' => 'publish' ), 'per_page' => array( 'type' => 'integer', 'default' => 20 ), 'page' => array( 'type' => 'integer', 'default' => 1 ), ), ), ), function ( $args ) { $query = new WP_Query( array( 'post_type' => 'product', 'post_status' => isset( $args['status'] ) ? sanitize_key( $args['status'] ) : 'publish', 's' => isset( $args['search'] ) ? sanitize_text_field( $args['search'] ) : '', 'posts_per_page' => isset( $args['per_page'] ) ? min( 100, absint( $args['per_page'] ) ) : 20, 'paged' => isset( $args['page'] ) ? max( 1, absint( $args['page'] ) ) : 1, ) ); $items = array(); foreach ( $query->posts as $post ) { $product = wc_get_product( $post->ID ); if ( $product ) { $items[] = agentdesk_format_wc_product( $product ); } } return array( 'products' => $items, 'total' => (int) $query->found_posts ); } ); $registry->register( 'woo_products_get', array( 'description' => 'Get a WooCommerce product by ID.', 'domain' => 'woocommerce', 'capability' => 'manage_woocommerce', 'inputSchema' => array( 'type' => 'object', 'properties' => array( 'id' => array( 'type' => 'integer' ) ), 'required' => array( 'id' ), ), ), function ( $args ) { $product = wc_get_product( absint( $args['id'] ) ); if ( ! $product ) { return new WP_Error( 'agentdesk_not_found', 'Product not found' ); } return agentdesk_format_wc_product( $product, true ); } ); $registry->register( 'woo_products_create', array( 'description' => 'Create a WooCommerce product.', 'domain' => 'woocommerce', 'capability' => 'manage_woocommerce', 'inputSchema' => array( 'type' => 'object', 'properties' => array( 'name' => array( 'type' => 'string' ), 'description' => array( 'type' => 'string' ), 'regular_price' => array( 'type' => 'string' ), 'sale_price' => array( 'type' => 'string' ), 'sku' => array( 'type' => 'string' ), 'status' => array( 'type' => 'string', 'default' => 'draft' ), 'type' => array( 'type' => 'string', 'default' => 'simple' ), ), 'required' => array( 'name' ), ), ), function ( $args ) { $product = new WC_Product_Simple(); $product->set_name( sanitize_text_field( $args['name'] ) ); if ( isset( $args['description'] ) ) { $product->set_description( wp_kses_post( $args['description'] ) ); } if ( isset( $args['regular_price'] ) ) { $product->set_regular_price( wc_format_decimal( $args['regular_price'] ) ); } if ( isset( $args['sale_price'] ) ) { $product->set_sale_price( wc_format_decimal( $args['sale_price'] ) ); } if ( isset( $args['sku'] ) ) { $product->set_sku( sanitize_text_field( $args['sku'] ) ); } if ( isset( $args['status'] ) ) { $product->set_status( sanitize_key( $args['status'] ) ); } $id = $product->save(); return agentdesk_format_wc_product( wc_get_product( $id ), true ); } ); $registry->register( 'woo_products_update', array( 'description' => 'Update a WooCommerce product.', 'domain' => 'woocommerce', 'capability' => 'manage_woocommerce', 'inputSchema' => array( 'type' => 'object', 'properties' => array( 'id' => array( 'type' => 'integer' ), 'name' => array( 'type' => 'string' ), 'description' => array( 'type' => 'string' ), 'regular_price' => array( 'type' => 'string' ), 'sale_price' => array( 'type' => 'string' ), 'status' => array( 'type' => 'string' ), ), 'required' => array( 'id' ), ), ), function ( $args ) { $product = wc_get_product( absint( $args['id'] ) ); if ( $product && ! current_user_can( 'edit_post', $product->get_id() ) ) { return new WP_Error( 'agentdesk_forbidden', __( 'You are not allowed to modify this product.', 'agentdesk-site-operations' ), array( 'status' => 403 ) ); } if ( ! $product ) { return new WP_Error( 'agentdesk_not_found', 'Product not found' ); } if ( isset( $args['name'] ) ) { $product->set_name( sanitize_text_field( $args['name'] ) ); } if ( isset( $args['description'] ) ) { $product->set_description( wp_kses_post( $args['description'] ) ); } if ( isset( $args['regular_price'] ) ) { $product->set_regular_price( wc_format_decimal( $args['regular_price'] ) ); } if ( isset( $args['sale_price'] ) ) { $product->set_sale_price( wc_format_decimal( $args['sale_price'] ) ); } if ( isset( $args['status'] ) ) { $product->set_status( sanitize_key( $args['status'] ) ); } $product->save(); return agentdesk_format_wc_product( $product, true ); } ); $registry->register( 'woo_products_delete', array( 'description' => 'Delete a WooCommerce product.', 'domain' => 'woocommerce', 'capability' => 'manage_woocommerce', 'blocked_in_safe_mode' => true, 'inputSchema' => array( 'type' => 'object', 'properties' => array( 'id' => array( 'type' => 'integer' ), 'force' => array( 'type' => 'boolean', 'default' => false ), ), 'required' => array( 'id' ), ), ), function ( $args ) { $product = wc_get_product( absint( $args['id'] ) ); if ( $product && ! current_user_can( 'delete_post', $product->get_id() ) ) { return new WP_Error( 'agentdesk_forbidden', __( 'You are not allowed to modify this product.', 'agentdesk-site-operations' ), array( 'status' => 403 ) ); } if ( ! $product ) { return new WP_Error( 'agentdesk_not_found', 'Product not found' ); } $product->delete( ! empty( $args['force'] ) ); return array( 'deleted' => true, 'id' => absint( $args['id'] ) ); } ); $registry->register( 'woo_variations_list', array( 'description' => 'List variations for a variable product.', 'domain' => 'woocommerce', 'capability' => 'manage_woocommerce', 'inputSchema' => array( 'type' => 'object', 'properties' => array( 'product_id' => array( 'type' => 'integer' ) ), 'required' => array( 'product_id' ), ), ), function ( $args ) { $product = wc_get_product( absint( $args['product_id'] ) ); if ( ! $product || ! $product->is_type( 'variable' ) ) { return new WP_Error( 'agentdesk_invalid_product', 'Variable product not found' ); } $items = array(); foreach ( $product->get_children() as $variation_id ) { $variation = wc_get_product( $variation_id ); if ( $variation ) { $items[] = array( 'id' => $variation->get_id(), 'sku' => $variation->get_sku(), 'price' => $variation->get_price(), 'attributes' => $variation->get_attributes(), ); } } return array( 'variations' => $items ); } ); $registry->register( 'woo_coupons_list', array( 'description' => 'List WooCommerce coupons.', 'domain' => 'woocommerce', 'capability' => 'manage_woocommerce', 'inputSchema' => array( 'type' => 'object', 'properties' => array( 'per_page' => array( 'type' => 'integer', 'default' => 20 ) ), ), ), function ( $args ) { $query = new WP_Query( array( 'post_type' => 'shop_coupon', 'posts_per_page' => isset( $args['per_page'] ) ? min( 100, absint( $args['per_page'] ) ) : 20, ) ); $items = array(); foreach ( $query->posts as $post ) { $coupon = new WC_Coupon( $post->ID ); $items[] = array( 'id' => $post->ID, 'code' => $coupon->get_code(), 'amount' => $coupon->get_amount(), 'type' => $coupon->get_discount_type(), ); } return array( 'coupons' => $items ); } ); } ); /** * Format WC product. * * @param WC_Product $product Product. * @param bool $full Full details. * @return array */ function agentdesk_format_wc_product( $product, $full = false ) { $data = array( 'id' => $product->get_id(), 'name' => $product->get_name(), 'sku' => $product->get_sku(), 'price' => $product->get_price(), 'status' => $product->get_status(), 'type' => $product->get_type(), ); if ( $full ) { $data['description'] = $product->get_description(); $data['regular_price'] = $product->get_regular_price(); $data['sale_price'] = $product->get_sale_price(); $data['stock_quantity'] = $product->get_stock_quantity(); } return $data; }