'wc-orders', 'action' => 'edit', 'id' => $order_id, ), admin_url( 'admin.php' ) ); } return add_query_arg( array( 'post' => $order_id, 'action' => 'edit', ), admin_url( 'post.php' ) ); } /** * Orders list filtered to one customer, built without a query. * * @since 1.0.0 * @param int $user_id Customer user ID. * @return string Unescaped URL. */ public static function customer_orders_url( int $user_id ): string { if ( self::hpos() ) { return add_query_arg( array( 'page' => 'wc-orders', '_customer_user' => $user_id, ), admin_url( 'admin.php' ) ); } return add_query_arg( array( 'post_type' => 'shop_order', '_customer_user' => $user_id, ), admin_url( 'edit.php' ) ); } /** * Meta keys that hold a custom order number (sequential order number plugins). * * @since 1.0.0 * @return string[] Non-empty meta keys. */ public static function number_meta_keys(): array { /** * Meta keys that hold a custom order number (sequential order number plugins). * * Documented in docs/hooks.md (Advanced order search). * * @since 1.0.0 * @param string[] $keys Meta keys. */ $keys = (array) apply_filters( 'apwc_order_number_meta_keys', array( '_order_number', '_order_number_formatted' ) ); return array_values( array_filter( $keys, static fn( $key ) => is_string( $key ) && '' !== $key ) ); } /** * Find an order by the number a person typed (with or without "#"). * * An order whose ID matches wins only when its displayed number is the same; otherwise a custom * order number (see number_meta_keys()) wins, so "105" means the order numbered 105, not the one * with ID 105. Falls back to the order with that ID. Refunds and other order types never match. * * @since 1.0.0 * @param string $term Typed order number. * @return WC_Order|null */ public static function find_by_number( string $term ): ?WC_Order { $term = ltrim( trim( $term ), '#' ); if ( '' === $term ) { return null; } $order = ctype_digit( $term ) ? wc_get_order( (int) $term ) : null; $order = $order instanceof WC_Order && 'shop_order' === $order->get_type() ? $order : null; if ( $order && (string) $order->get_order_number() === $term ) { return $order; } foreach ( self::number_meta_keys() as $key ) { $found = wc_get_orders( array( 'type' => 'shop_order', 'limit' => 1, 'return' => 'ids', 'meta_key' => $key, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key 'meta_value' => $term, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value ) ); if ( is_array( $found ) && $found ) { $match = wc_get_order( $found[0] ); // An ID: return => ids. if ( $match instanceof WC_Order && 'shop_order' === $match->get_type() ) { return $match; } } } return $order; } }