‘Create Product’,
‘description’ => ‘Creates a new WooCommerce product.’,
‘category’ => ‘master’,
‘input_schema’ => [
‘type’ => ‘object’,
‘properties’ => [
‘name’ => [ ‘type’ => ‘string’, ‘description’ => ‘The name of the product.’ ],
‘regular_price’ => [ ‘type’ => ‘string’, ‘description’ => ‘The regular price of the product.’ ],
‘sale_price’ => [ ‘type’ => ‘string’, ‘description’ => ‘The sale price of the product.’ ],
‘sku’ => [ ‘type’ => ‘string’, ‘description’ => ‘The SKU of the product.’ ],
‘description’ => [ ‘type’ => ‘string’, ‘description’ => ‘The main description of the product.’ ],
‘short_description’ => [ ‘type’ => ‘string’, ‘description’ => ‘The short description of the product.’ ],
‘status’ => [ ‘type’ => ‘string’, ‘description’ => ‘The product status.’, ‘enum’ => [ ‘publish’, ‘draft’, ‘pending’ ], ‘default’ => ‘draft’ ],
],
‘required’ => [ ‘name’ ],
],
‘output_schema’ => [
‘type’ => ‘object’,
‘properties’ => [
‘product_id’ => [ ‘type’ => ‘integer’, ‘description’ => ‘The ID of the created product.’ ],
],
],
‘execute_callback’ => [ $this, ‘create_product’ ],
‘permission_callback’ => function() { return current_user_can( ‘publish_products’ ); },
‘meta’ => [ ‘mcp’ => [ ‘public’ => true, ‘type’ => ‘tool’ ] ],
] );
wp_register_ability( ‘master/update-product’, [
‘label’ => ‘Update Product’,
‘description’ => ‘Updates an existing WooCommerce product.’,
‘category’ => ‘master’,
‘input_schema’ => [
‘type’ => ‘object’,
‘properties’ => [
‘product_id’ => [ ‘type’ => ‘integer’, ‘description’ => ‘The ID of the product to update.’ ],
‘name’ => [ ‘type’ => ‘string’, ‘description’ => ‘The new name for the product.’ ],
‘regular_price’ => [ ‘type’ => ‘string’, ‘description’ => ‘The new regular price for the product.’ ],
‘sale_price’ => [ ‘type’ => ‘string’, ‘description’ => ‘The new sale price for the product.’ ],
‘sku’ => [ ‘type’ => ‘string’, ‘description’ => ‘The new SKU for the product.’ ],
‘description’ => [ ‘type’ => ‘string’, ‘description’ => ‘The new main description for the product.’ ],
‘short_description’ => [ ‘type’ => ‘string’, ‘description’ => ‘The new short description for the product.’ ],
‘status’ => [ ‘type’ => ‘string’, ‘description’ => ‘The new product status.’, ‘enum’ => [ ‘publish’, ‘draft’, ‘pending’ ] ],
],
‘required’ => [ ‘product_id’ ],
],
‘output_schema’ => [
‘type’ => ‘object’,
‘properties’ => [
‘success’ => [ ‘type’ => ‘boolean’, ‘description’ => ‘Whether the update was successful.’ ],
],
],
‘execute_callback’ => [ $this, ‘update_product’ ],
‘permission_callback’ => function( $request ) { return current_user_can( ‘edit_product’, $request[‘product_id’] ); },
‘meta’ => [ ‘mcp’ => [ ‘public’ => true, ‘type’ => ‘tool’ ] ],
] );
wp_register_ability( ‘master/get-product’, [
‘label’ => ‘Get Product’,
‘description’ => ‘Retrieves a single WooCommerce product.’,
‘category’ => ‘master’,
‘input_schema’ => [
‘type’ => ‘object’,
‘properties’ => [
‘product_id’ => [ ‘type’ => ‘integer’, ‘description’ => ‘The ID of the product to retrieve.’ ],
],
‘required’ => [ ‘product_id’ ],
],
‘output_schema’ => [
‘type’ => ‘object’,
‘properties’ => [
‘product’ => [ ‘type’ => ‘object’, ‘description’ => ‘The product object.’ ],
],
],
‘execute_callback’ => [ $this, ‘get_product’ ],
‘permission_callback’ => function( $request ) { return current_user_can( ‘edit_product’, $request[‘product_id’] ); },
‘meta’ => [ ‘mcp’ => [ ‘public’ => true, ‘type’ => ‘tool’ ], ‘annotations’ => [ ‘readOnlyHint’ => true ] ],
] );
wp_register_ability( ‘master/list-products’, [
‘label’ => ‘List Products’,
‘description’ => ‘Retrieves a list of WooCommerce products.’,
‘category’ => ‘master’,
‘input_schema’ => [
‘type’ => ‘object’,
‘properties’ => [
‘limit’ => [ ‘type’ => ‘integer’, ‘description’ => ‘The maximum number of products to return.’, ‘default’ => 10 ],
‘status’ => [ ‘type’ => ‘string’, ‘description’ => ‘Filter by product status.’, ‘default’ => ‘publish’ ],
],
],
‘output_schema’ => [
‘type’ => ‘object’,
‘properties’ => [
‘products’ => [ ‘type’ => ‘array’, ‘description’ => ‘Array of product objects.’, ‘items’ => [ ‘type’ => ‘object’ ] ],
],
],
‘execute_callback’ => [ $this, ‘list_products’ ],
‘permission_callback’ => function() { return current_user_can( ‘edit_products’ ); },
‘meta’ => [ ‘mcp’ => [ ‘public’ => true, ‘type’ => ‘tool’ ], ‘annotations’ => [ ‘readOnlyHint’ => true ] ],
] );
wp_register_ability( ‘master/delete-product’, [
‘label’ => ‘Delete Product’,
‘description’ => ‘Deletes a WooCommerce product.’,
‘category’ => ‘master’,
‘input_schema’ => [
‘type’ => ‘object’,
‘properties’ => [
‘product_id’ => [ ‘type’ => ‘integer’, ‘description’ => ‘The ID of the product to delete.’ ],
‘force_delete’ => [ ‘type’ => ‘boolean’, ‘description’ => ‘Whether to bypass the trash and permanently delete.’, ‘default’ => false ],
],
‘required’ => [ ‘product_id’ ],
],
‘output_schema’ => [
‘type’ => ‘object’,
‘properties’ => [
‘success’ => [ ‘type’ => ‘boolean’, ‘description’ => ‘Whether the deletion was successful.’ ],
],
],
‘execute_callback’ => [ $this, ‘delete_product’ ],
‘permission_callback’ => function( $request ) { return current_user_can( ‘delete_product’, $request[‘product_id’] ); },
‘meta’ => [ ‘mcp’ => [ ‘public’ => true, ‘type’ => ‘tool’ ] ],
] );
// === CART MANAGEMENT ABILITIES ===
// NOTE: These abilities are disabled by default. Uncomment to enable cart/checkout functionality.
/*
wp_register_ability( ‘master/get-cart’, [
‘label’ => ‘Get Cart’,
‘description’ => ‘Retrieves the current WooCommerce cart contents and totals.’,
‘category’ => ‘master’,
‘input_schema’ => [
‘type’ => ‘object’,
‘properties’ => [
‘customer_id’ => [ ‘type’ => ‘integer’, ‘description’ => ‘Optional customer ID to load their cart.’ ],
],
],
‘output_schema’ => [
‘type’ => ‘object’,
‘properties’ => [
‘cart’ => [ ‘type’ => ‘object’, ‘description’ => ‘Cart data including items, totals, and coupons.’ ],
],
],
‘execute_callback’ => [ $this, ‘get_cart’ ],
‘permission_callback’ => function() { return true; }, // Public endpoint
‘meta’ => [ ‘mcp’ => [ ‘public’ => true, ‘type’ => ‘tool’ ], ‘annotations’ => [ ‘readOnlyHint’ => true ] ],
] );
wp_register_ability( ‘master/add-to-cart’, [
‘label’ => ‘Add to Cart’,
‘description’ => ‘Adds a product to the WooCommerce cart.’,
‘category’ => ‘master’,
‘input_schema’ => [
‘type’ => ‘object’,
‘properties’ => [
‘product_id’ => [ ‘type’ => ‘integer’, ‘description’ => ‘The product ID to add to cart.’ ],
‘quantity’ => [ ‘type’ => ‘integer’, ‘description’ => ‘Quantity to add.’, ‘default’ => 1 ],
‘variation_id’ => [ ‘type’ => ‘integer’, ‘description’ => ‘Variation ID for variable products.’ ],
‘variation’ => [ ‘type’ => ‘object’, ‘description’ => ‘Variation attributes as key-value pairs.’ ],
],
‘required’ => [ ‘product_id’ ],
],
‘output_schema’ => [
‘type’ => ‘object’,
‘properties’ => [
‘cart_item_key’ => [ ‘type’ => ‘string’, ‘description’ => ‘Unique key for the cart item.’ ],
‘cart’ => [ ‘type’ => ‘object’, ‘description’ => ‘Updated cart data.’ ],
],
],
‘execute_callback’ => [ $this, ‘add_to_cart’ ],
‘permission_callback’ => function() { return true; }, // Public endpoint
‘meta’ => [ ‘mcp’ => [ ‘public’ => true, ‘type’ => ‘tool’ ] ],
] );
wp_register_ability( ‘master/update-cart-item’, [
‘label’ => ‘Update Cart Item’,
‘description’ => ‘Updates the quantity of a cart item.’,
‘category’ => ‘master’,
‘input_schema’ => [
‘type’ => ‘object’,
‘properties’ => [
‘cart_item_key’ => [ ‘type’ => ‘string’, ‘description’ => ‘The unique key of the cart item to update.’ ],
‘quantity’ => [ ‘type’ => ‘integer’, ‘description’ => ‘New quantity (0 to remove).’ ],
],
‘required’ => [ ‘cart_item_key’, ‘quantity’ ],
],
‘output_schema’ => [
‘type’ => ‘object’,
‘properties’ => [
‘success’ => [ ‘type’ => ‘boolean’, ‘description’ => ‘Whether the update was successful.’ ],
‘cart’ => [ ‘type’ => ‘object’, ‘description’ => ‘Updated cart data.’ ],
],
],
‘execute_callback’ => [ $this, ‘update_cart_item’ ],
‘permission_callback’ => function() { return true; }, // Public endpoint
‘meta’ => [ ‘mcp’ => [ ‘public’ => true, ‘type’ => ‘tool’ ] ],
] );
wp_register_ability( ‘master/remove-cart-item’, [
‘label’ => ‘Remove Cart Item’,
‘description’ => ‘Removes an item from the WooCommerce cart.’,
‘category’ => ‘master’,
‘input_schema’ => [
‘type’ => ‘object’,
‘properties’ => [
‘cart_item_key’ => [ ‘type’ => ‘string’, ‘description’ => ‘The unique key of the cart item to remove.’ ],
],
‘required’ => [ ‘cart_item_key’ ],
],
‘output_schema’ => [
‘type’ => ‘object’,
‘properties’ => [
‘success’ => [ ‘type’ => ‘boolean’, ‘description’ => ‘Whether the removal was successful.’ ],
‘cart’ => [ ‘type’ => ‘object’, ‘description’ => ‘Updated cart data.’ ],
],
],
‘execute_callback’ => [ $this, ‘remove_cart_item’ ],
‘permission_callback’ => function() { return true; }, // Public endpoint
‘meta’ => [ ‘mcp’ => [ ‘public’ => true, ‘type’ => ‘tool’ ] ],
] );
wp_register_ability( ‘master/clear-cart’, [
‘label’ => ‘Clear Cart’,
‘description’ => ‘Empties the WooCommerce cart completely.’,
‘category’ => ‘master’,
‘input_schema’ => [
‘type’ => ‘object’,
‘properties’ => [],
],
‘output_schema’ => [
‘type’ => ‘object’,
‘properties’ => [
‘success’ => [ ‘type’ => ‘boolean’, ‘description’ => ‘Whether the cart was cleared.’ ],
],
],
‘execute_callback’ => [ $this, ‘clear_cart’ ],
‘permission_callback’ => function() { return true; }, // Public endpoint
‘meta’ => [ ‘mcp’ => [ ‘public’ => true, ‘type’ => ‘tool’ ] ],
] );
wp_register_ability( ‘master/apply-coupon’, [
‘label’ => ‘Apply Coupon’,
‘description’ => ‘Applies a coupon code to the cart.’,
‘category’ => ‘master’,
‘input_schema’ => [
‘type’ => ‘object’,
‘properties’ => [
‘coupon_code’ => [ ‘type’ => ‘string’, ‘description’ => ‘The coupon code to apply.’ ],
],
‘required’ => [ ‘coupon_code’ ],
],
‘output_schema’ => [
‘type’ => ‘object’,
‘properties’ => [
‘success’ => [ ‘type’ => ‘boolean’, ‘description’ => ‘Whether the coupon was applied.’ ],
‘message’ => [ ‘type’ => ‘string’, ‘description’ => ‘Success or error message.’ ],
‘cart’ => [ ‘type’ => ‘object’, ‘description’ => ‘Updated cart with coupon applied.’ ],
],
],
‘execute_callback’ => [ $this, ‘apply_coupon’ ],
‘permission_callback’ => function() { return true; }, // Public endpoint
‘meta’ => [ ‘mcp’ => [ ‘public’ => true, ‘type’ => ‘tool’ ] ],
] );
wp_register_ability( ‘master/remove-coupon’, [
‘label’ => ‘Remove Coupon’,
‘description’ => ‘Removes a coupon from the cart.’,
‘category’ => ‘master’,
‘input_schema’ => [
‘type’ => ‘object’,
‘properties’ => [
‘coupon_code’ => [ ‘type’ => ‘string’, ‘description’ => ‘The coupon code to remove.’ ],
],
‘required’ => [ ‘coupon_code’ ],
],
‘output_schema’ => [
‘type’ => ‘object’,
‘properties’ => [
‘success’ => [ ‘type’ => ‘boolean’, ‘description’ => ‘Whether the coupon was removed.’ ],
‘cart’ => [ ‘type’ => ‘object’, ‘description’ => ‘Updated cart data.’ ],
],
],
‘execute_callback’ => [ $this, ‘remove_coupon’ ],
‘permission_callback’ => function() { return true; }, // Public endpoint
‘meta’ => [ ‘mcp’ => [ ‘public’ => true, ‘type’ => ‘tool’ ] ],
] );
// === ORDER MANAGEMENT ABILITIES ===
wp_register_ability( ‘master/create-order’, [
‘label’ => ‘Create Order’,
‘description’ => ‘Creates a new WooCommerce order from the current cart or provided data.’,
‘category’ => ‘master’,
‘input_schema’ => [
‘type’ => ‘object’,
‘properties’ => [
‘customer_id’ => [ ‘type’ => ‘integer’, ‘description’ => ‘Customer ID (0 for guest).’ ],
‘billing’ => [ ‘type’ => ‘object’, ‘description’ => ‘Billing address details.’ ],
‘shipping’ => [ ‘type’ => ‘object’, ‘description’ => ‘Shipping address details.’ ],
‘payment_method’ => [ ‘type’ => ‘string’, ‘description’ => ‘Payment method ID.’ ],
‘payment_method_title’ => [ ‘type’ => ‘string’, ‘description’ => ‘Payment method title.’ ],
‘set_paid’ => [ ‘type’ => ‘boolean’, ‘description’ => ‘Whether to mark order as paid.’, ‘default’ => false ],
‘status’ => [ ‘type’ => ‘string’, ‘description’ => ‘Order status.’, ‘default’ => ‘pending’ ],
],
],
‘output_schema’ => [
‘type’ => ‘object’,
‘properties’ => [
‘order_id’ => [ ‘type’ => ‘integer’, ‘description’ => ‘The created order ID.’ ],
‘order_key’ => [ ‘type’ => ‘string’, ‘description’ => ‘Order key for tracking.’ ],
‘total’ => [ ‘type’ => ‘string’, ‘description’ => ‘Order total.’ ],
],
],
‘execute_callback’ => [ $this, ‘create_order’ ],
‘permission_callback’ => function() { return current_user_can( ‘edit_shop_orders’ ); },
‘meta’ => [ ‘mcp’ => [ ‘public’ => true, ‘type’ => ‘tool’ ] ],
] );
wp_register_ability( ‘master/get-order’, [
‘label’ => ‘Get Order’,
‘description’ => ‘Retrieves a WooCommerce order by ID.’,
‘category’ => ‘master’,
‘input_schema’ => [
‘type’ => ‘object’,
‘properties’ => [
‘order_id’ => [ ‘type’ => ‘integer’, ‘description’ => ‘The order ID to retrieve.’ ],
],
‘required’ => [ ‘order_id’ ],
],
‘output_schema’ => [
‘type’ => ‘object’,
‘properties’ => [
‘order’ => [ ‘type’ => ‘object’, ‘description’ => ‘Complete order data.’ ],
],
],
‘execute_callback’ => [ $this, ‘get_order’ ],
‘permission_callback’ => function( $request ) {
$order = wc_get_order( $request[‘order_id’] );
return $order && ( current_user_can( ‘edit_shop_orders’ ) || get_current_user_id() === $order->get_customer_id() );
},
‘meta’ => [ ‘mcp’ => [ ‘public’ => true, ‘type’ => ‘tool’ ], ‘annotations’ => [ ‘readOnlyHint’ => true ] ],
] );
wp_register_ability( ‘master/list-orders’, [
‘label’ => ‘List Orders’,
‘description’ => ‘Retrieves a list of WooCommerce orders.’,
‘category’ => ‘master’,
‘input_schema’ => [
‘type’ => ‘object’,
‘properties’ => [
‘limit’ => [ ‘type’ => ‘integer’, ‘description’ => ‘Maximum orders to return.’, ‘default’ => 10 ],
‘status’ => [ ‘type’ => ‘string’, ‘description’ => ‘Filter by order status (e.g., processing, completed).’ ],
‘customer_id’ => [ ‘type’ => ‘integer’, ‘description’ => ‘Filter by customer ID.’ ],
],
],
‘output_schema’ => [
‘type’ => ‘object’,
‘properties’ => [
‘orders’ => [ ‘type’ => ‘array’, ‘description’ => ‘Array of order objects.’ ],
],
],
‘execute_callback’ => [ $this, ‘list_orders’ ],
‘permission_callback’ => function() { return current_user_can( ‘edit_shop_orders’ ); },
‘meta’ => [ ‘mcp’ => [ ‘public’ => true, ‘type’ => ‘tool’ ], ‘annotations’ => [ ‘readOnlyHint’ => true ] ],
] );
wp_register_ability( ‘master/update-order-status’, [
‘label’ => ‘Update Order Status’,
‘description’ => ‘Updates the status of a WooCommerce order.’,
‘category’ => ‘master’,
‘input_schema’ => [
‘type’ => ‘object’,
‘properties’ => [
‘order_id’ => [ ‘type’ => ‘integer’, ‘description’ => ‘The order ID to update.’ ],
‘status’ => [ ‘type’ => ‘string’, ‘description’ => ‘New status (e.g., processing, completed, cancelled).’ ],
‘note’ => [ ‘type’ => ‘string’, ‘description’ => ‘Optional note to add to order.’ ],
],
‘required’ => [ ‘order_id’, ‘status’ ],
],
‘output_schema’ => [
‘type’ => ‘object’,
‘properties’ => [
‘success’ => [ ‘type’ => ‘boolean’, ‘description’ => ‘Whether status was updated.’ ],
],
],
‘execute_callback’ => [ $this, ‘update_order_status’ ],
‘permission_callback’ => function() { return current_user_can( ‘edit_shop_orders’ ); },
‘meta’ => [ ‘mcp’ => [ ‘public’ => true, ‘type’ => ‘tool’ ] ],
] );
*/
// END ALL WOOCOMMERCE ABILITIES (commented out)
}
/*
public function create_product( $request ) {
$product = new WC_Product_Simple();
$product->set_name( $request[‘name’] );
$product->set_regular_price( $request[‘regular_price’] ?? ” );
$product->set_sale_price( $request[‘sale_price’] ?? ” );
$product->set_sku( $request[‘sku’] ?? ” );
$product->set_description( $request[‘description’] ?? ” );
$product->set_short_description( $request[‘short_description’] ?? ” );
$product->set_status( $request[‘status’] ?? ‘draft’ );
$product_id = $product->save();
if ( $product_id === 0 ) {
return new WP_Error( ‘product_creation_failed’, ‘Failed to create product.’ );
}
return [ ‘product_id’ => $product_id ];
}
public function update_product( $request ) {
$product = wc_get_product( $request[‘product_id’] );
if ( ! $product ) {
return new WP_Error( ‘product_not_found’, ‘Product not found.’ );
}
if ( isset( $request[‘name’] ) ) $product->set_name( $request[‘name’] );
if ( isset( $request[‘regular_price’] ) ) $product->set_regular_price( $request[‘regular_price’] );
if ( isset( $request[‘sale_price’] ) ) $product->set_sale_price( $request[‘sale_price’] );
if ( isset( $request[‘sku’] ) ) $product->set_sku( $request[‘sku’] );
if ( isset( $request[‘description’] ) ) $product->set_description( $request[‘description’] );
if ( isset( $request[‘short_description’] ) ) $product->set_short_description( $request[‘short_description’] );
if ( isset( $request[‘status’] ) ) $product->set_status( $request[‘status’] );
$product->save();
return [ ‘success’ => true ];
}
public function get_product( $request ) {
$product = wc_get_product( $request[‘product_id’] );
if ( ! $product ) {
return new WP_Error( ‘product_not_found’, ‘Product not found.’ );
}
return [ ‘product’ => $product->get_data() ];
}
public function list_products( $request ) {
$args = [
‘limit’ => $request[‘limit’] ?? 10,
‘status’ => $request[‘status’] ?? ‘publish’,
];
$products = wc_get_products( $args );
$product_data = [];
foreach ( $products as $product ) {
$product_data[] = $product->get_data();
}
return [ ‘products’ => $product_data ];
}
public function delete_product( $request ) {
$product = wc_get_product( $request[‘product_id’] );
if ( ! $product ) {
return new WP_Error( ‘product_not_found’, ‘Product not found.’ );
}
$force = $request[‘force_delete’] ?? false;
$result = $product->delete( $force );
if ( ! $result ) {
return new WP_Error( ‘product_deletion_failed’, ‘Failed to delete product.’ );
}
return [ ‘success’ => true ];
}
// === CART MANAGEMENT IMPLEMENTATIONS ===
// NOTE: These implementation methods are disabled. Uncomment when enabling cart/order abilities.
/*
private function init_cart() {
if ( ! function_exists( ‘WC’ ) || ! WC()->cart ) {
return new WP_Error( ‘woocommerce_not_active’, ‘WooCommerce is not active or cart not initialized.’ );
}
// Ensure session is started for cart operations
if ( ! WC()->session || ! WC()->session->has_session() ) {
WC()->session->set_customer_session_cookie( true );
}
return WC()->cart;
}
private function get_cart_data() {
$cart = WC()->cart;
return [
‘items’ => array_values( $cart->get_cart() ),
‘item_count’ => $cart->get_cart_contents_count(),
‘subtotal’ => $cart->get_subtotal(),
‘total’ => $cart->get_total( ‘edit’ ),
‘tax_total’ => $cart->get_total_tax(),
‘shipping_total’ => $cart->get_shipping_total(),
‘discount_total’ => $cart->get_discount_total(),
‘coupons’ => $cart->get_applied_coupons(),
‘needs_shipping’ => $cart->needs_shipping(),
‘needs_payment’ => $cart->needs_payment(),
];
}
public function get_cart( $request ) {
$cart = $this->init_cart();
if ( is_wp_error( $cart ) ) {
return $cart;
}
// Optionally load a specific customer’s cart
if ( isset( $request[‘customer_id’] ) && current_user_can( ‘edit_users’ ) ) {
WC()->session->set( ‘customer_id’, $request[‘customer_id’] );
}
WC()->cart->calculate_totals();
return [ ‘cart’ => $this->get_cart_data() ];
}
public function add_to_cart( $request ) {
$cart = $this->init_cart();
if ( is_wp_error( $cart ) ) {
return $cart;
}
$product_id = $request[‘product_id’];
$quantity = $request[‘quantity’] ?? 1;
$variation_id = $request[‘variation_id’] ?? 0;
$variation = $request[‘variation’] ?? [];
$cart_item_key = WC()->cart->add_to_cart( $product_id, $quantity, $variation_id, $variation );
if ( ! $cart_item_key ) {
return new WP_Error( ‘add_to_cart_failed’, ‘Failed to add product to cart.’ );
}
WC()->cart->calculate_totals();
return [
‘cart_item_key’ => $cart_item_key,
‘cart’ => $this->get_cart_data(),
];
}
public function update_cart_item( $request ) {
$cart = $this->init_cart();
if ( is_wp_error( $cart ) ) {
return $cart;
}
$cart_item_key = $request[‘cart_item_key’];
$quantity = $request[‘quantity’];
$success = WC()->cart->set_quantity( $cart_item_key, $quantity, true );
if ( ! $success ) {
return new WP_Error( ‘update_failed’, ‘Failed to update cart item quantity.’ );
}
WC()->cart->calculate_totals();
return [
‘success’ => true,
‘cart’ => $this->get_cart_data(),
];
}
public function remove_cart_item( $request ) {
$cart = $this->init_cart();
if ( is_wp_error( $cart ) ) {
return $cart;
}
$cart_item_key = $request[‘cart_item_key’];
$success = WC()->cart->remove_cart_item( $cart_item_key );
if ( ! $success ) {
return new WP_Error( ‘remove_failed’, ‘Failed to remove cart item.’ );
}
WC()->cart->calculate_totals();
return [
‘success’ => true,
‘cart’ => $this->get_cart_data(),
];
}
public function clear_cart( $request ) {
$cart = $this->init_cart();
if ( is_wp_error( $cart ) ) {
return $cart;
}
WC()->cart->empty_cart();
return [ ‘success’ => true ];
}
public function apply_coupon( $request ) {
$cart = $this->init_cart();
if ( is_wp_error( $cart ) ) {
return $cart;
}
$coupon_code = wc_format_coupon_code( $request[‘coupon_code’] );
$success = WC()->cart->apply_coupon( $coupon_code );
if ( ! $success ) {
$error_message = wc_get_notices( ‘error’ );
wc_clear_notices();
return [
‘success’ => false,
‘message’ => ! empty( $error_message ) ? $error_message[0][‘notice’] : ‘Coupon could not be applied.’,
];
}
WC()->cart->calculate_totals();
wc_clear_notices();
return [
‘success’ => true,
‘message’ => ‘Coupon applied successfully.’,
‘cart’ => $this->get_cart_data(),
];
}
public function remove_coupon( $request ) {
$cart = $this->init_cart();
if ( is_wp_error( $cart ) ) {
return $cart;
}
$coupon_code = wc_format_coupon_code( $request[‘coupon_code’] );
$success = WC()->cart->remove_coupon( $coupon_code );
if ( ! $success ) {
return new WP_Error( ‘coupon_remove_failed’, ‘Failed to remove coupon.’ );
}
WC()->cart->calculate_totals();
return [
‘success’ => true,
‘cart’ => $this->get_cart_data(),
];
}
// === ORDER MANAGEMENT IMPLEMENTATIONS ===
public function create_order( $request ) {
if ( ! function_exists( ‘wc_create_order’ ) ) {
return new WP_Error( ‘woocommerce_not_active’, ‘WooCommerce is not active.’ );
}
$order = wc_create_order();
if ( is_wp_error( $order ) ) {
return $order;
}
// Set customer
if ( isset( $request[‘customer_id’] ) ) {
$order->set_customer_id( $request[‘customer_id’] );
}
// Set addresses
if ( isset( $request[‘billing’] ) && is_array( $request[‘billing’] ) ) {
$order->set_address( $request[‘billing’], ‘billing’ );
}
if ( isset( $request[‘shipping’] ) && is_array( $request[‘shipping’] ) ) {
$order->set_address( $request[‘shipping’], ‘shipping’ );
}
// Add items from cart if no custom items provided
if ( WC()->cart && ! WC()->cart->is_empty() ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item[‘data’];
$order->add_product( $product, $cart_item[‘quantity’] );
}
}
// Set payment method
if ( isset( $request[‘payment_method’] ) ) {
$order->set_payment_method( $request[‘payment_method’] );
}
if ( isset( $request[‘payment_method_title’] ) ) {
$order->set_payment_method_title( $request[‘payment_method_title’] );
}
// Calculate totals
$order->calculate_totals();
// Set status
$status = $request[‘status’] ?? ‘pending’;
$order->set_status( $status );
// Mark as paid if requested
if ( isset( $request[‘set_paid’] ) && $request[‘set_paid’] ) {
$order->payment_complete();
}
$order->save();
// Clear cart after order creation
if ( WC()->cart ) {
WC()->cart->empty_cart();
}
return [
‘order_id’ => $order->get_id(),
‘order_key’ => $order->get_order_key(),
‘total’ => $order->get_total(),
];
}
public function get_order( $request ) {
$order = wc_get_order( $request[‘order_id’] );
if ( ! $order ) {
return new WP_Error( ‘order_not_found’, ‘Order not found.’ );
}
return [ ‘order’ => $order->get_data() ];
}
public function list_orders( $request ) {
$args = [
‘limit’ => $request[‘limit’] ?? 10,
‘return’ => ‘objects’,
];
if ( isset( $request[‘status’] ) ) {
$args[‘status’] = $request[‘status’];
}
if ( isset( $request[‘customer_id’] ) ) {
$args[‘customer_id’] = $request[‘customer_id’];
}
$orders = wc_get_orders( $args );
$order_data = [];
foreach ( $orders as $order ) {
$order_data[] = $order->get_data();
}
return [ ‘orders’ => $order_data ];
}
public function update_order_status( $request ) {
$order = wc_get_order( $request[‘order_id’] );
if ( ! $order ) {
return new WP_Error( ‘order_not_found’, ‘Order not found.’ );
}
$status = $request[‘status’];
$note = $request[‘note’] ?? ”;
$order->update_status( $status, $note, true );
return [ ‘success’ => true ];
}
*/
// END ALL WOOCOMMERCE IMPLEMENTATION METHODS (commented out)
}
