add-attribute-terms.php
8 months ago
add-customer.php
9 months ago
add-emails-to-coupon.php
8 months ago
add-order-note.php
10 months ago
add-product-attributes.php
8 months ago
add-product-to-cart.php
8 months ago
add-update-order-custom-fields.php
10 months ago
apply-coupon.php
8 months ago
check-cart-status.php
8 months ago
check-if-order-is-renewal.php
4 months ago
create-attribute.php
8 months ago
create-bundle-product-order.php
6 months ago
create-coupon-code.php
10 months ago
create-multi-product-order.php
7 months ago
create-new-order.php
10 months ago
create-product-variation.php
1 year ago
create-product.php
1 year ago
create-variable-product.php
8 months ago
delete-coupon.php
1 year ago
delete-customer.php
8 months ago
fetch-coupon-details.php
10 months ago
find-orders-by-user-id.php
10 months ago
get-all-customers.php
8 months ago
get-all-products.php
8 months ago
get-customer-by-email.php
8 months ago
get-customer-by-id.php
8 months ago
get-order-details-by-order-id.php
10 months ago
get-order-type-by-order-id.php
4 months ago
get-orders-by-email.php
8 months ago
get-product-by-id.php
1 year ago
list-all-order-notes.php
8 months ago
list-all-orders.php
8 months ago
remove-product-from-cart.php
8 months ago
retrieve-coupons-totals.php
10 months ago
retrieve-customers-totals.php
10 months ago
retrieve-orders-totals.php
10 months ago
retrieve-products-totals.php
10 months ago
retrieve-refunds-list.php
10 months ago
retrieve-reviews-totals.php
10 months ago
retrieve-sales-report.php
10 months ago
retrieve-top-sellers-report.php
10 months ago
update-product-price.php
8 months ago
update-product-stock-quantity.php
8 months ago
update-status-of-order.php
10 months ago
list-all-orders.php
275 lines
| 1 | <?php |
| 2 | /** |
| 3 | * ListAllOrders. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category ListAllOrders |
| 7 | * @package SureTriggers |
| 8 | * @author BSF <username@example.com> |
| 9 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 10 | * @link https://www.brainstormforce.com/ |
| 11 | * @since 1.0.0 |
| 12 | */ |
| 13 | |
| 14 | namespace SureTriggers\Integrations\WooCommerce\Actions; |
| 15 | |
| 16 | use SureTriggers\Integrations\AutomateAction; |
| 17 | use SureTriggers\Integrations\WooCommerce\WooCommerce; |
| 18 | use SureTriggers\Traits\SingletonLoader; |
| 19 | |
| 20 | /** |
| 21 | * ListAllOrders |
| 22 | * |
| 23 | * @category ListAllOrders |
| 24 | * @package SureTriggers |
| 25 | * @author BSF <username@example.com> |
| 26 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 27 | * @link https://www.brainstormforce.com/ |
| 28 | * @since 1.0.0 |
| 29 | */ |
| 30 | class ListAllOrders extends AutomateAction { |
| 31 | |
| 32 | use SingletonLoader; |
| 33 | |
| 34 | /** |
| 35 | * Integration type. |
| 36 | * |
| 37 | * @var string |
| 38 | */ |
| 39 | public $integration = 'WooCommerce'; |
| 40 | |
| 41 | /** |
| 42 | * Action name. |
| 43 | * |
| 44 | * @var string |
| 45 | */ |
| 46 | public $action = 'wc_list_all_orders'; |
| 47 | |
| 48 | /** |
| 49 | * Register the action. |
| 50 | * |
| 51 | * @param array $actions Actions array. |
| 52 | * @return array |
| 53 | */ |
| 54 | public function register( $actions ) { |
| 55 | $actions[ $this->integration ][ $this->action ] = [ |
| 56 | 'label' => __( 'List All Orders', 'suretriggers' ), |
| 57 | 'action' => $this->action, |
| 58 | 'function' => [ $this, 'action_listener' ], |
| 59 | ]; |
| 60 | |
| 61 | return $actions; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Action listener. |
| 66 | * |
| 67 | * @param int $user_id User ID. |
| 68 | * @param int $automation_id Automation ID. |
| 69 | * @param array $fields Fields. |
| 70 | * @param array $selected_options Selected options. |
| 71 | * @return array |
| 72 | */ |
| 73 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 74 | if ( ! function_exists( 'wc_get_orders' ) ) { |
| 75 | return [ |
| 76 | 'success' => false, |
| 77 | 'message' => __( 'WooCommerce order functions not available.', 'suretriggers' ), |
| 78 | ]; |
| 79 | } |
| 80 | |
| 81 | // Get parameters with defaults. |
| 82 | $limit = isset( $selected_options['limit'] ) ? intval( $selected_options['limit'] ) : 10; |
| 83 | $status = isset( $selected_options['status'] ) ? $selected_options['status'] : 'any'; |
| 84 | |
| 85 | // Validate limit. |
| 86 | if ( $limit <= 0 || $limit > 100 ) { |
| 87 | $limit = 10; |
| 88 | } |
| 89 | |
| 90 | // Get orders using WooCommerce function. |
| 91 | $args = [ |
| 92 | 'limit' => $limit, |
| 93 | 'status' => $status, |
| 94 | 'orderby' => 'date', |
| 95 | 'order' => 'DESC', |
| 96 | ]; |
| 97 | |
| 98 | $orders = wc_get_orders( $args ); |
| 99 | |
| 100 | if ( empty( $orders ) ) { |
| 101 | return [ |
| 102 | 'success' => true, |
| 103 | 'orders' => [], |
| 104 | 'orders_count' => 0, |
| 105 | 'message' => __( 'No orders found.', 'suretriggers' ), |
| 106 | ]; |
| 107 | } |
| 108 | |
| 109 | // Format orders data. |
| 110 | $formatted_orders = []; |
| 111 | if ( is_array( $orders ) ) { |
| 112 | foreach ( $orders as $order ) { |
| 113 | $formatted_orders[] = $this->format_order_data( $order ); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | $response_data = [ |
| 118 | 'success' => true, |
| 119 | 'orders' => $formatted_orders, |
| 120 | 'orders_count' => count( $formatted_orders ), |
| 121 | 'filters_applied' => [ |
| 122 | 'limit' => $limit, |
| 123 | 'status' => $status, |
| 124 | ], |
| 125 | 'message' => sprintf( __( 'Retrieved %d orders.', 'suretriggers' ), count( $formatted_orders ) ), |
| 126 | ]; |
| 127 | |
| 128 | return $response_data; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Format order data. |
| 133 | * |
| 134 | * @param \WC_Order $order WooCommerce order object. |
| 135 | * @return array |
| 136 | */ |
| 137 | private function format_order_data( $order ) { |
| 138 | // Type check to ensure we have a WC_Order object. |
| 139 | if ( ! is_a( $order, 'WC_Order' ) ) { |
| 140 | return []; |
| 141 | } |
| 142 | // Get dates with null checks. |
| 143 | $date_created = $order->get_date_created(); |
| 144 | $date_modified = $order->get_date_modified(); |
| 145 | $date_completed = $order->get_date_completed(); |
| 146 | $date_paid = $order->get_date_paid(); |
| 147 | |
| 148 | $order_data = [ |
| 149 | 'id' => $order->get_id(), |
| 150 | 'order_number' => $order->get_order_number(), |
| 151 | 'status' => $order->get_status(), |
| 152 | 'currency' => $order->get_currency(), |
| 153 | 'total' => $order->get_total(), |
| 154 | 'subtotal' => $order->get_subtotal(), |
| 155 | 'tax_total' => $order->get_total_tax(), |
| 156 | 'shipping_total' => $order->get_shipping_total(), |
| 157 | 'discount_total' => $order->get_discount_total(), |
| 158 | 'payment_method' => $order->get_payment_method(), |
| 159 | 'payment_method_title' => $order->get_payment_method_title(), |
| 160 | 'date_created' => $date_created ? $date_created->date( 'Y-m-d H:i:s' ) : '', |
| 161 | 'date_modified' => $date_modified ? $date_modified->date( 'Y-m-d H:i:s' ) : '', |
| 162 | 'date_completed' => $date_completed ? $date_completed->date( 'Y-m-d H:i:s' ) : '', |
| 163 | 'date_paid' => $date_paid ? $date_paid->date( 'Y-m-d H:i:s' ) : '', |
| 164 | ]; |
| 165 | |
| 166 | // Add customer information. |
| 167 | $customer_id = $order->get_customer_id(); |
| 168 | $order_data['customer'] = [ |
| 169 | 'id' => $customer_id, |
| 170 | 'email' => $order->get_billing_email(), |
| 171 | 'first_name' => $order->get_billing_first_name(), |
| 172 | 'last_name' => $order->get_billing_last_name(), |
| 173 | 'phone' => $order->get_billing_phone(), |
| 174 | ]; |
| 175 | |
| 176 | // Add billing address. |
| 177 | $order_data['billing'] = [ |
| 178 | 'first_name' => $order->get_billing_first_name(), |
| 179 | 'last_name' => $order->get_billing_last_name(), |
| 180 | 'company' => $order->get_billing_company(), |
| 181 | 'address_1' => $order->get_billing_address_1(), |
| 182 | 'address_2' => $order->get_billing_address_2(), |
| 183 | 'city' => $order->get_billing_city(), |
| 184 | 'state' => $order->get_billing_state(), |
| 185 | 'postcode' => $order->get_billing_postcode(), |
| 186 | 'country' => $order->get_billing_country(), |
| 187 | 'email' => $order->get_billing_email(), |
| 188 | 'phone' => $order->get_billing_phone(), |
| 189 | ]; |
| 190 | |
| 191 | // Add shipping address. |
| 192 | $order_data['shipping'] = [ |
| 193 | 'first_name' => $order->get_shipping_first_name(), |
| 194 | 'last_name' => $order->get_shipping_last_name(), |
| 195 | 'company' => $order->get_shipping_company(), |
| 196 | 'address_1' => $order->get_shipping_address_1(), |
| 197 | 'address_2' => $order->get_shipping_address_2(), |
| 198 | 'city' => $order->get_shipping_city(), |
| 199 | 'state' => $order->get_shipping_state(), |
| 200 | 'postcode' => $order->get_shipping_postcode(), |
| 201 | 'country' => $order->get_shipping_country(), |
| 202 | ]; |
| 203 | |
| 204 | // Add order items. |
| 205 | $items = []; |
| 206 | foreach ( $order->get_items() as $item_id => $item ) { |
| 207 | // Type check for WooCommerce order item. |
| 208 | if ( ! is_a( $item, 'WC_Order_Item_Product' ) ) { |
| 209 | continue; |
| 210 | } |
| 211 | $product = $item->get_product(); |
| 212 | $items[] = [ |
| 213 | 'id' => $item_id, |
| 214 | 'product_id' => $item->get_product_id(), |
| 215 | 'variation_id' => $item->get_variation_id(), |
| 216 | 'name' => $item->get_name(), |
| 217 | 'quantity' => $item->get_quantity(), |
| 218 | 'total' => $item->get_total(), |
| 219 | 'subtotal' => $item->get_subtotal(), |
| 220 | 'sku' => ( $product && is_object( $product ) && is_a( $product, 'WC_Product' ) ) ? $product->get_sku() : '', |
| 221 | ]; |
| 222 | } |
| 223 | $order_data['items'] = $items; |
| 224 | |
| 225 | // Add shipping methods. |
| 226 | $shipping_methods = []; |
| 227 | foreach ( $order->get_shipping_methods() as $shipping_method ) { |
| 228 | $shipping_methods[] = [ |
| 229 | 'id' => $shipping_method->get_id(), |
| 230 | 'method_title' => $shipping_method->get_method_title(), |
| 231 | 'method_id' => $shipping_method->get_method_id(), |
| 232 | 'total' => $shipping_method->get_total(), |
| 233 | ]; |
| 234 | } |
| 235 | $order_data['shipping_methods'] = $shipping_methods; |
| 236 | |
| 237 | // Add order notes count. |
| 238 | $notes = wc_get_order_notes( |
| 239 | [ |
| 240 | 'order_id' => $order->get_id(), |
| 241 | 'limit' => 1, |
| 242 | ] |
| 243 | ); |
| 244 | $order_data['notes_count'] = count( $notes ); |
| 245 | |
| 246 | // Add formatted country names if available. |
| 247 | if ( ! empty( $order_data['billing']['country'] ) ) { |
| 248 | $countries = WC()->countries->get_countries(); |
| 249 | $order_data['billing']['country_name'] = isset( $countries[ $order_data['billing']['country'] ] ) |
| 250 | ? $countries[ $order_data['billing']['country'] ] |
| 251 | : $order_data['billing']['country']; |
| 252 | } |
| 253 | |
| 254 | if ( ! empty( $order_data['shipping']['country'] ) ) { |
| 255 | $countries = WC()->countries->get_countries(); |
| 256 | $order_data['shipping']['country_name'] = isset( $countries[ $order_data['shipping']['country'] ] ) |
| 257 | ? $countries[ $order_data['shipping']['country'] ] |
| 258 | : $order_data['shipping']['country']; |
| 259 | } |
| 260 | |
| 261 | // Add order totals summary. |
| 262 | $order_data['totals'] = [ |
| 263 | 'subtotal' => wc_format_decimal( $order->get_subtotal(), 2 ), |
| 264 | 'discount_total' => wc_format_decimal( $order->get_discount_total(), 2 ), |
| 265 | 'shipping_total' => wc_format_decimal( $order->get_shipping_total(), 2 ), |
| 266 | 'tax_total' => wc_format_decimal( $order->get_total_tax(), 2 ), |
| 267 | 'total' => wc_format_decimal( $order->get_total(), 2 ), |
| 268 | ]; |
| 269 | |
| 270 | return $order_data; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | ListAllOrders::get_instance(); |
| 275 |