views
2 years ago
AbstractExport.php
11 months ago
CustomersExport.php
5 months ago
OrdersExport.php
2 years ago
PlansExport.php
2 years ago
ProductSalesExport.php
2 years ago
SalesEarningsExport.php
2 years ago
SettingsPage.php
1 year ago
SubscriptionsExport.php
2 years ago
index.php
2 years ago
OrdersExport.php
138 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\Admin\SettingsPages\Membership\ExportPage; |
| 4 | |
| 5 | use ProfilePress\Core\Base; |
| 6 | use ProfilePressVendor\Carbon\CarbonImmutable; |
| 7 | |
| 8 | class OrdersExport extends AbstractExport |
| 9 | { |
| 10 | protected function headers() |
| 11 | { |
| 12 | return [ |
| 13 | __('Order ID', 'wp-user-avatar'), |
| 14 | __('Order Key', 'wp-user-avatar'), |
| 15 | __('Plan ID', 'wp-user-avatar'), |
| 16 | __('Plan', 'wp-user-avatar'), |
| 17 | __('Email', 'wp-user-avatar'), |
| 18 | __('Customer ID', 'wp-user-avatar'), |
| 19 | __('Customer Name', 'wp-user-avatar'), |
| 20 | __('Subscription ID', 'wp-user-avatar'), |
| 21 | __('Billing Address', 'wp-user-avatar'), |
| 22 | __('Billing City', 'wp-user-avatar'), |
| 23 | __('Billing State', 'wp-user-avatar'), |
| 24 | __('Billing Country', 'wp-user-avatar'), |
| 25 | __('Billing Postcode', 'wp-user-avatar'), |
| 26 | __('Billing Phone', 'wp-user-avatar'), |
| 27 | __('Order Status', 'wp-user-avatar'), |
| 28 | __('Order Type', 'wp-user-avatar'), |
| 29 | __('Order Mode', 'wp-user-avatar'), |
| 30 | __('Currency', 'wp-user-avatar'), |
| 31 | __('Coupon Code', 'wp-user-avatar'), |
| 32 | __('Subtotal', 'wp-user-avatar'), |
| 33 | __('Tax', 'wp-user-avatar'), |
| 34 | __('Tax Rate', 'wp-user-avatar'), |
| 35 | __('Discount', 'wp-user-avatar'), |
| 36 | __('Order Total', 'wp-user-avatar'), |
| 37 | __('Payment Method', 'wp-user-avatar'), |
| 38 | __('Transaction ID', 'wp-user-avatar'), |
| 39 | __('IP Address', 'wp-user-avatar'), |
| 40 | __('Date Created', 'wp-user-avatar'), |
| 41 | __('Date Completed', 'wp-user-avatar') |
| 42 | ]; |
| 43 | } |
| 44 | |
| 45 | public function get_data($page = 1, $limit = 9999) |
| 46 | { |
| 47 | global $wpdb; |
| 48 | |
| 49 | $start_date = $this->form['orders-export-start'] ?? ''; |
| 50 | $end_date = $this->form['orders-export-end'] ?? ''; |
| 51 | |
| 52 | $order_status = $this->form['order_status'] ?? ''; |
| 53 | $plan_id = (int)$this->form['plan_id'] ?? ''; |
| 54 | |
| 55 | $orders_table = Base::orders_db_table(); |
| 56 | $customers_table = Base::customers_db_table(); |
| 57 | $plans_table = Base::subscription_plans_db_table(); |
| 58 | $wp_user_table = $wpdb->users; |
| 59 | |
| 60 | $wp_timezone = wp_timezone(); |
| 61 | |
| 62 | $replacements = [1]; |
| 63 | $sql = " |
| 64 | SELECT |
| 65 | po.id as order_id, |
| 66 | po.order_key, |
| 67 | po.plan_id, |
| 68 | pp.name AS plan_name, |
| 69 | wpu.user_email, |
| 70 | po.customer_id, |
| 71 | wpu.display_name, |
| 72 | po.subscription_id, |
| 73 | po.billing_address, |
| 74 | po.billing_city, |
| 75 | po.billing_state, |
| 76 | po.billing_country, |
| 77 | po.billing_postcode, |
| 78 | po.billing_phone, |
| 79 | po.status, |
| 80 | po.order_type, |
| 81 | po.mode, |
| 82 | po.currency, |
| 83 | po.coupon_code, |
| 84 | po.subtotal, |
| 85 | po.tax, |
| 86 | po.tax_rate, |
| 87 | po.discount, |
| 88 | po.total, |
| 89 | po.payment_method, |
| 90 | po.transaction_id, |
| 91 | po.ip_address, |
| 92 | po.date_created, |
| 93 | po.date_completed |
| 94 | FROM |
| 95 | $orders_table AS po |
| 96 | INNER JOIN $customers_table AS pc ON po.customer_id = pc.id |
| 97 | INNER JOIN $wp_user_table AS wpu ON pc.user_id = wpu.ID |
| 98 | INNER JOIN $plans_table AS pp ON po.plan_id = pp.id |
| 99 | WHERE |
| 100 | 1 = %d"; |
| 101 | |
| 102 | if ( ! empty($plan_id)) { |
| 103 | $replacements[] = $plan_id; |
| 104 | $sql .= " AND po.plan_id = %s"; |
| 105 | } |
| 106 | |
| 107 | if ( ! empty($order_status)) { |
| 108 | $replacements[] = sanitize_text_field($order_status); |
| 109 | $sql .= " AND po.status = %s"; |
| 110 | } |
| 111 | |
| 112 | if ( ! empty($start_date)) { |
| 113 | $replacements[] = CarbonImmutable::parse($start_date, $wp_timezone)->startOfDay()->utc()->toDateTimeString(); |
| 114 | $sql .= " AND po.date_created >= %s"; |
| 115 | } |
| 116 | |
| 117 | if ( ! empty($end_date)) { |
| 118 | $replacements[] = CarbonImmutable::parse($end_date, $wp_timezone)->endOfDay()->utc()->toDateTimeString(); |
| 119 | $sql .= " AND po.date_created <= %s"; |
| 120 | } |
| 121 | |
| 122 | $page = max(1, intval($page)); |
| 123 | |
| 124 | $offset = ($page - 1) * intval($limit); |
| 125 | |
| 126 | if ($limit > 0) { |
| 127 | $sql .= " LIMIT %d"; |
| 128 | $replacements[] = $limit; |
| 129 | } |
| 130 | |
| 131 | if ($offset > 0) { |
| 132 | $sql .= " OFFSET %d"; |
| 133 | $replacements[] = $offset; |
| 134 | } |
| 135 | |
| 136 | return $wpdb->get_results($wpdb->prepare($sql, $replacements), ARRAY_A); |
| 137 | } |
| 138 | } |