PluginProbe
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More / 2.2.0
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More v2.2.0
2.3.0 2.2.0 2.1.1 2.1.0 2.0.0 1.10.0 1.9.1 1.9.0 1.2.1 1.2.2 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 All 59 releases
storeengine / includes / cli / order.php

order.php in StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More 2.2.0, at includes/cli/order.php

179 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace StoreEngine\Cli;
4
5 use StoreEngine\Classes\OrderCollection;
6 use StoreEngine\Utils\Helper;
7 use WP_CLI;
8 use WP_CLI_Command;
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit;
12 }
13
14 class Order extends WP_CLI_Command {
15
16 /**
17 * List orders.
18 *
19 * ## OPTIONS
20 *
21 * [--status=<status>]
22 * : Order status to filter by (e.g. pending, processing, completed, on_hold, cancelled, refunded, failed).
23 *
24 * [--limit=<limit>]
25 * : Maximum number of orders to list. Defaults to 10. Set to -1 for all.
26 * ---
27 * default: 10
28 * ---
29 *
30 * ## EXAMPLES
31 *
32 * wp storeengine order list --status=completed --limit=50
33 *
34 * @subcommand list
35 */
36 public function list_orders( $args, $assoc_args ) {
37 $status = $assoc_args['status'] ?? '';
38 $limit = $assoc_args['limit'] ?? 10;
39
40 $query_args = [
41 'per_page' => $limit,
42 'orderby' => 'id',
43 'order' => 'DESC',
44 ];
45
46 if ( $status ) {
47 $query_args['where'][] = [
48 'key' => 'status',
49 'value' => $status,
50 ];
51 }
52
53 $orders_collection = new OrderCollection( $query_args );
54 $orders = $orders_collection->get_results();
55
56 if ( empty( $orders ) ) {
57 WP_CLI::warning( 'No orders found.' );
58
59 return;
60 }
61
62 $items = [];
63 foreach ( $orders as $order ) {
64 $items[] = [
65 'ID' => $order->get_id(),
66 'Status' => $order->get_status(),
67 'Type' => $order->get_type(),
68 'Total' => $order->get_total(),
69 'Currency' => $order->get_currency(),
70 'Customer ID' => $order->get_customer_id(),
71 'Date' => $order->get_date_created_gmt() ? $order->get_date_created_gmt()->format( 'Y-m-d H:i:s' ) : '',
72 ];
73 }
74
75 WP_CLI\Utils\format_items( 'table', $items, [
76 'ID',
77 'Status',
78 'Type',
79 'Total',
80 'Currency',
81 'Customer ID',
82 'Date'
83 ] );
84 }
85
86 /**
87 * Delete orders.
88 *
89 * ## OPTIONS
90 *
91 * [--id=<id>]
92 * : Specific order ID to delete. Set to "all" to delete all orders.
93 *
94 * [--status=<status>]
95 * : Delete all orders matching a specific status.
96 *
97 * [--force]
98 * : Skip confirmation prompt.
99 *
100 * [--skip-trash]
101 * : Deletes directly from database without setting status to trash.
102 *
103 * ## EXAMPLES
104 *
105 * wp storeengine order delete --id=123
106 * wp storeengine order delete --status=cancelled --force
107 *
108 * @subcommand delete
109 */
110 public function delete_orders( $args, $assoc_args ) {
111 $id = $assoc_args['id'] ?? null;
112 $status = $assoc_args['status'] ?? null;
113 $force = $assoc_args['force'] ?? false;
114 $skip_trash = $assoc_args['skip-trash'] ?? false;
115
116 if ( ! $id && ! $status ) {
117 WP_CLI::error( 'Please provide either --id=<id>, --id=all, or --status=<status>' );
118 }
119
120 $orders_to_delete = [];
121
122 if ( $id && 'all' !== $id ) {
123 $order = Helper::get_order( $id );
124 if ( ! $order || is_wp_error( $order ) ) {
125 WP_CLI::error( "Order $id not found." );
126 }
127 $orders_to_delete[] = $order;
128 } else {
129 $query_args = [
130 'per_page' => - 1,
131 ];
132
133 if ( $status ) {
134 $query_args['where'][] = [
135 'key' => 'status',
136 'value' => $status,
137 ];
138 }
139
140 $orders_collection = new OrderCollection( $query_args );
141 $orders_to_delete = $orders_collection->get_results();
142
143 if ( empty( $orders_to_delete ) ) {
144 $msg = $status ? "No orders found with status '$status'." : "No orders found.";
145 WP_CLI::success( $msg );
146
147 return;
148 }
149 }
150
151 $count = count( $orders_to_delete );
152
153 if ( ! $force ) {
154 WP_CLI::confirm( "Are you sure you want to delete $count order(s)?" );
155 }
156
157 $progress = \WP_CLI\Utils\make_progress_bar( 'Deleting orders', $count );
158
159 $deleted_count = 0;
160 foreach ( $orders_to_delete as $order ) {
161 try {
162 if ( $order->delete( $skip_trash ) ) {
163 $deleted_count ++;
164 } else {
165 WP_CLI::warning( "Failed to delete order {$order->get_id()}." );
166 }
167 } catch ( \Throwable $e ) {
168 WP_CLI::warning( "Failed to delete order {$order->get_id()}. Error: {$e->getMessage()}" );
169 }
170
171 $progress->tick();
172 }
173
174 $progress->finish();
175
176 WP_CLI::success( "Successfully deleted $deleted_count order(s)." );
177 }
178 }
179