| @@ -59,11 +59,25 @@ | ||
| 59 | 59 | */ |
| 60 | 60 | public function get_applied_discounts() { |
| 61 | 61 | check_ajax_referer( 'sellkit', 'nonce' ); |
| 62 | 62 | |
| 63 | - $page = sellkit_htmlspecialchars( INPUT_GET, 'page' ); | |
| 64 | - $id = sellkit_htmlspecialchars( INPUT_GET, 'id' ); | |
| 63 | + // Prefer `paged` — `page` can collide with other WP query usage; accept both GET/POST for ajax. | |
| 64 | + $page = sellkit_htmlspecialchars( INPUT_GET, 'paged' ); | |
| 65 | + if ( empty( $page ) ) { | |
| 66 | + $page = sellkit_htmlspecialchars( INPUT_POST, 'paged' ); | |
| 67 | + } | |
| 68 | + if ( empty( $page ) ) { | |
| 69 | + $page = sellkit_htmlspecialchars( INPUT_GET, 'page' ); | |
| 70 | + } | |
| 71 | + if ( empty( $page ) ) { | |
| 72 | + $page = sellkit_htmlspecialchars( INPUT_POST, 'page' ); | |
| 73 | + } | |
| 65 | 74 | |
| 75 | + $id = sellkit_htmlspecialchars( INPUT_GET, 'id' ); | |
| 76 | + if ( empty( $id ) ) { | |
| 77 | + $id = sellkit_htmlspecialchars( INPUT_POST, 'id' ); | |
| 78 | + } | |
| 79 | + | |
| 66 | 80 | if ( empty( $page ) ) { |
| 67 | 81 | $page = 1; |
| 68 | 82 | } |
| 69 | 83 | |
| @@ -95,9 +109,9 @@ | ||
| 95 | 109 | $total = $wpdb->get_results( |
| 96 | 110 | $prepared_total_query, |
| 97 | 111 | ARRAY_A ); |
| 98 | 112 | |
| 99 | - $total_discount_num = ! empty( $total[0]['total_discounts'] ) ? $total[0]['total_discounts'] : ''; | |
| 113 | + $total_discount_num = ! empty( $total[0]['total_discounts'] ) ? (int) $total[0]['total_discounts'] : 0; | |
| 100 | 114 | // phpcs:enable |
| 101 | 115 | |
| 102 | 116 | wp_send_json_success( [ |
| 103 | 117 | 'discounts' => $discounts, |
| @@ -136,9 +150,20 @@ | ||
| 136 | 150 | $expiry_date = get_post_meta( $post->ID, 'date_expires', true ); |
| 137 | 151 | $customer_email = get_post_meta( $post->ID, 'customer_email', true ); |
| 138 | 152 | $usage_limit = get_post_meta( $post->ID, 'usage_limit', true ); |
| 139 | 153 | $usage_count = get_post_meta( $post->ID, 'usage_count', true ); |
| 154 | + $used_by = get_post_meta( $post->ID, '_used_by', true ); | |
| 140 | 155 | |
| 156 | + if ( empty( $customer_email ) && ! empty( $used_by ) ) { | |
| 157 | + $used_by_customer = is_email( $used_by ) ? $used_by : ''; | |
| 158 | + | |
| 159 | + if ( empty( $used_by_customer ) ) { | |
| 160 | + $used_by_customer = get_userdata( intval( $used_by ) )->user_email; | |
| 161 | + } | |
| 162 | + | |
| 163 | + $customer_email = [ $used_by_customer ]; | |
| 164 | + } | |
| 165 | + | |
| 141 | 166 | $post->customer = ! empty( $customer_email ) ? $customer_email : ''; |
| 142 | 167 | $post->usage_limit = ! empty( $usage_limit ) ? $usage_limit : ''; |
| 143 | 168 | $post->usage_count = ! empty( $usage_count ) ? $usage_count : ''; |
| 144 | 169 | $post->created_at = date_i18n( 'Y/m/d h:i A', strtotime( $post->post_date ) ); |
| @@ -188,10 +213,16 @@ | ||
| 188 | 213 | where ct.funnel_id = %d |
| 189 | 214 | group by ct.user_id |
| 190 | 215 | order by ct.id desc limit %d , %d;", $funnel_id, $page_index, $limit ), ARRAY_A ); |
| 191 | 216 | |
| 217 | + // Must match the grouped list query (one row per user), not raw row count. | |
| 192 | 218 | $total_contacts = $wpdb->get_results( |
| 193 | - $wpdb->prepare( "SELECT count(*) as total_contacts FROM {$contact_table} where funnel_id = %d;", $funnel_id ), ARRAY_A ); | |
| 219 | + $wpdb->prepare( | |
| 220 | + "SELECT COUNT(*) AS total_contacts FROM ( SELECT user_id FROM {$contact_table} WHERE funnel_id = %d GROUP BY user_id ) AS grouped_contacts", | |
| 221 | + $funnel_id | |
| 222 | + ), | |
| 223 | + ARRAY_A | |
| 224 | + ); | |
| 194 | 225 | $total_contacts_num = ! empty( $total_contacts[0]['total_contacts'] ) ? $total_contacts[0]['total_contacts'] : ''; |
| 195 | 226 | // phpcs:enable |
| 196 | 227 | |
| 197 | 228 | wp_send_json_success( [ |
| @@ -238,9 +269,9 @@ | ||
| 238 | 269 | where ct.funnel_id = %d and ct.user_id = %d |
| 239 | 270 | order by ct.id desc limit %d , %d;", $funnel_id, $user_id, $page_index, $limit ), ARRAY_A ); |
| 240 | 271 | |
| 241 | 272 | $total_contacts = $wpdb->get_results( |
| 242 | - $wpdb->prepare( "SELECT count(*) as total_contacts FROM {$contact_table} where funnel_id = %d;", $funnel_id ), ARRAY_A ); | |
| 273 | + $wpdb->prepare( "SELECT count(*) as total_contacts FROM {$contact_table} WHERE funnel_id = %d AND user_id = %d;", $funnel_id, $user_id ), ARRAY_A ); | |
| 243 | 274 | $total_contacts_num = ! empty( $total_contacts[0]['total_contacts'] ) ? $total_contacts[0]['total_contacts'] : ''; |
| 244 | 275 | // phpcs:enable |
| 245 | 276 | |
| 246 | 277 | wp_send_json_success( [ |
| @@ -452,9 +483,9 @@ | ||
| 452 | 483 | |
| 453 | 484 | $funnel_orders[] = [ |
| 454 | 485 | 'order_number' => $order->get_order_number(), |
| 455 | 486 | 'order_date' => $order->get_date_created()->date( 'Y/m/d g:i A' ), |
| 456 | - 'order_revenue' => $order->get_total() - $order->get_total_discount() - $order->get_total_tax(), | |
| 487 | + 'order_revenue' => $order->get_total() - $order->get_total_tax(), | |
| 457 | 488 | 'products' => $products, |
| 458 | 489 | ]; |
| 459 | 490 | } |
| 460 | 491 | |