false, 'order_id' => $order_id, 'old_status' => '', 'new_status' => '', 'message' => 'Order not found.' ); } $valid_statuses = array_keys( wc_get_order_statuses() ); // Normalize status $normalized = preg_replace( '/^wc-/', '', sanitize_key( $new_status ) ); $slug = 'wc-' . $normalized; if ( ! in_array( $slug, $valid_statuses, true ) ) { return array( 'success' => false, 'order_id' => $order_id, 'old_status' => $order->get_status(), 'new_status' => $new_status, 'message' => "Invalid order status: {$new_status}." ); } $old_status = $order->get_status(); $order->update_status( $normalized, sanitize_textarea_field( $note ), true ); self::log_debug( 'Order status updated', array( 'order_id' => $order_id, 'old' => $old_status, 'new' => $normalized ) ); return array( 'success' => true, 'order_id' => $order_id, 'old_status' => $old_status, 'new_status' => $order->get_status(), 'message' => sprintf( 'Order #%d status changed from %s to %s.', $order_id, $old_status, $order->get_status() ), ); } /** * Creates a WooCommerce coupon with a percentage or fixed-amount discount. * * The coupon title is stored as the coupon code (the string customers enter at checkout). * * @param array $data { * @type string $title Coupon code / title. * @type string $discount_type `percent` or `fixed`. * @type float|int|string $amount Percentage (1–100) or fixed amount in store currency. * @type string $fixed_scope When `discount_type` is `fixed`: `cart` (default) or `product`. * } * @return array{ success: bool, coupon_id: int|null, code: string, message: string, edit_url: string|null } */ public static function create_coupon( array $data ) { if ( ! class_exists( 'WC_Coupon' ) ) { return array( 'success' => false, 'coupon_id' => null, 'code' => '', 'message' => 'WooCommerce is not active.', 'edit_url' => null, ); } $raw_title = isset( $data['title'] ) ? (string) $data['title'] : ''; $code = trim( wc_format_coupon_code( $raw_title ) ); if ( '' === $code ) { return array( 'success' => false, 'coupon_id' => null, 'code' => '', 'message' => 'Coupon title (code) is required and cannot be empty.', 'edit_url' => null, ); } $discount_type_input = isset( $data['discount_type'] ) ? sanitize_text_field( (string) $data['discount_type'] ) : 'percent'; if ( ! in_array( $discount_type_input, array( 'percent', 'fixed' ), true ) ) { return array( 'success' => false, 'coupon_id' => null, 'code' => $code, 'message' => 'discount_type must be "percent" or "fixed".', 'edit_url' => null, ); } $amount = isset( $data['amount'] ) ? floatval( $data['amount'] ) : 0.0; if ( $amount <= 0 ) { return array( 'success' => false, 'coupon_id' => null, 'code' => $code, 'message' => 'Amount must be greater than zero.', 'edit_url' => null, ); } if ( 'percent' === $discount_type_input && $amount > 100 ) { return array( 'success' => false, 'coupon_id' => null, 'code' => $code, 'message' => 'Percentage amount cannot exceed 100.', 'edit_url' => null, ); } $wc_discount_type = 'percent'; if ( 'fixed' === $discount_type_input ) { $scope = isset( $data['fixed_scope'] ) ? sanitize_text_field( (string) $data['fixed_scope'] ) : 'cart'; $wc_discount_type = ( 'product' === $scope ) ? 'fixed_product' : 'fixed_cart'; } $existing_id = wc_get_coupon_id_by_code( $code ); if ( $existing_id ) { return array( 'success' => false, 'coupon_id' => null, 'code' => $code, 'message' => sprintf( 'A coupon with code "%s" already exists.', $code ), 'edit_url' => null, ); } try { $coupon = new WC_Coupon(); $coupon->set_code( $code ); $coupon->set_discount_type( $wc_discount_type ); $coupon->set_amount( wc_format_decimal( $amount ) ); $coupon->set_status( 'publish' ); $coupon_id = $coupon->save(); if ( ! $coupon_id ) { return array( 'success' => false, 'coupon_id' => null, 'code' => $code, 'message' => 'Failed to save the coupon.', 'edit_url' => null, ); } self::log_debug( 'Coupon created', array( 'coupon_id' => $coupon_id, 'code' => $coupon->get_code(), 'type' => $wc_discount_type, ) ); return array( 'success' => true, 'coupon_id' => (int) $coupon_id, 'code' => $coupon->get_code(), 'message' => sprintf( 'Coupon "%s" created successfully.', $coupon->get_code() ), 'edit_url' => get_edit_post_link( $coupon_id, 'raw' ), ); } catch ( Exception $e ) { self::log_debug( 'Coupon create exception', array( 'error' => $e->getMessage() ) ); return array( 'success' => false, 'coupon_id' => null, 'code' => $code, 'message' => $e->getMessage(), 'edit_url' => null, ); } } }