WC_Report_Coupon_Usage

WC_Report_Coupon_Usage.

Defined (1)

The class is defined in the following location(s).

/includes/admin/reports/class-wc-report-coupon-usage.php  
  1. class WC_Report_Coupon_Usage extends WC_Admin_Report { 
  2.  
  3. /** 
  4. * Chart colors. 
  5. * @var array 
  6. */ 
  7. public $chart_colours = array(); 
  8.  
  9. /** 
  10. * Coupon codes. 
  11. * @var array 
  12. */ 
  13. public $coupon_codes = array(); 
  14.  
  15. /** 
  16. * Constructor. 
  17. */ 
  18. public function __construct() { 
  19. if ( isset( $_GET['coupon_codes'] ) && is_array( $_GET['coupon_codes'] ) ) { 
  20. $this->coupon_codes = array_filter( array_map( 'sanitize_text_field', $_GET['coupon_codes'] ) ); 
  21. } elseif ( isset( $_GET['coupon_codes'] ) ) { 
  22. $this->coupon_codes = array_filter( array( sanitize_text_field( $_GET['coupon_codes'] ) ) ); 
  23.  
  24. /** 
  25. * Get the legend for the main chart sidebar. 
  26. * @return array 
  27. */ 
  28. public function get_chart_legend() { 
  29. $legend = array(); 
  30.  
  31. $total_discount_query = array( 
  32. 'data' => array( 
  33. 'discount_amount' => array( 
  34. 'type' => 'order_item_meta',  
  35. 'order_item_type' => 'coupon',  
  36. 'function' => 'SUM',  
  37. 'name' => 'discount_amount',  
  38. ),  
  39. ),  
  40. 'where' => array( 
  41. array( 
  42. 'key' => 'order_item_type',  
  43. 'value' => 'coupon',  
  44. 'operator' => '=',  
  45. ),  
  46. ),  
  47. 'query_type' => 'get_var',  
  48. 'filter_range' => true,  
  49. 'order_types' => wc_get_order_types( 'order-count' ),  
  50. ); 
  51.  
  52. $total_coupons_query = array( 
  53. 'data' => array( 
  54. 'order_item_id' => array( 
  55. 'type' => 'order_item',  
  56. 'order_item_type' => 'coupon',  
  57. 'function' => 'COUNT',  
  58. 'name' => 'order_coupon_count',  
  59. ),  
  60. ),  
  61. 'where' => array( 
  62. array( 
  63. 'key' => 'order_item_type',  
  64. 'value' => 'coupon',  
  65. 'operator' => '=',  
  66. ),  
  67. ),  
  68. 'query_type' => 'get_var',  
  69. 'filter_range' => true,  
  70. 'order_types' => wc_get_order_types( 'order-count' ),  
  71. ); 
  72.  
  73. if ( ! empty( $this->coupon_codes ) ) { 
  74. $coupon_code_query = array( 
  75. 'type' => 'order_item',  
  76. 'key' => 'order_item_name',  
  77. 'value' => $this->coupon_codes,  
  78. 'operator' => 'IN',  
  79. ); 
  80.  
  81. $total_discount_query['where'][] = $coupon_code_query; 
  82. $total_coupons_query['where'][] = $coupon_code_query; 
  83.  
  84. $total_discount = $this->get_order_report_data( $total_discount_query ); 
  85. $total_coupons = absint( $this->get_order_report_data( $total_coupons_query ) ); 
  86.  
  87. $legend[] = array( 
  88. /** translators: %s: discount amount */ 
  89. 'title' => sprintf( __( '%s discounts in total', 'woocommerce' ), '<strong>' . wc_price( $total_discount ) . '</strong>' ),  
  90. 'color' => $this->chart_colours['discount_amount'],  
  91. 'highlight_series' => 1,  
  92. ); 
  93.  
  94. $legend[] = array( 
  95. /** translators: %s: coupons amount */ 
  96. 'title' => sprintf( __( '%s coupons used in total', 'woocommerce' ), '<strong>' . $total_coupons . '</strong>' ),  
  97. 'color' => $this->chart_colours['coupon_count'],  
  98. 'highlight_series' => 0,  
  99. ); 
  100.  
  101. return $legend; 
  102.  
  103. /** 
  104. * Output the report. 
  105. */ 
  106. public function output_report() { 
  107.  
  108. $ranges = array( 
  109. 'year' => __( 'Year', 'woocommerce' ),  
  110. 'last_month' => __( 'Last month', 'woocommerce' ),  
  111. 'month' => __( 'This month', 'woocommerce' ),  
  112. '7day' => __( 'Last 7 days', 'woocommerce' ),  
  113. ); 
  114.  
  115. $this->chart_colours = array( 
  116. 'discount_amount' => '#3498db',  
  117. 'coupon_count' => '#d4d9dc',  
  118. ); 
  119.  
  120. $current_range = ! empty( $_GET['range'] ) ? sanitize_text_field( $_GET['range'] ) : '7day'; 
  121.  
  122. if ( ! in_array( $current_range, array( 'custom', 'year', 'last_month', 'month', '7day' ) ) ) { 
  123. $current_range = '7day'; 
  124.  
  125. $this->check_current_range_nonce( $current_range ); 
  126. $this->calculate_current_range( $current_range ); 
  127.  
  128. include( WC()->plugin_path() . '/includes/admin/views/html-report-by-date.php' ); 
  129.  
  130. /** 
  131. * Get chart widgets. 
  132. * @return array 
  133. */ 
  134. public function get_chart_widgets() { 
  135. $widgets = array(); 
  136.  
  137. $widgets[] = array( 
  138. 'title' => '',  
  139. 'callback' => array( $this, 'coupons_widget' ),  
  140. ); 
  141.  
  142. return $widgets; 
  143.  
  144. /** 
  145. * Output coupons widget. 
  146. */ 
  147. public function coupons_widget() { 
  148. ?> 
  149. <h4 class="section_title"><span><?php _e( 'Filter by coupon', 'woocommerce' ); ?></span></h4> 
  150. <div class="section"> 
  151. <form method="GET"> 
  152. <div> 
  153. <?php 
  154. $used_coupons = $this->get_order_report_data( array( 
  155. 'data' => array( 
  156. 'order_item_name' => array( 
  157. 'type' => 'order_item',  
  158. 'order_item_type' => 'coupon',  
  159. 'function' => '',  
  160. 'distinct' => true,  
  161. 'name' => 'order_item_name',  
  162. ),  
  163. ),  
  164. 'where' => array( 
  165. array( 
  166. 'key' => 'order_item_type',  
  167. 'value' => 'coupon',  
  168. 'operator' => '=',  
  169. ),  
  170. ),  
  171. 'query_type' => 'get_col',  
  172. 'filter_range' => false,  
  173. ) ); 
  174.  
  175. if ( ! empty( $used_coupons ) && is_array( $used_coupons ) ) : 
  176. ?> 
  177. <select id="coupon_codes" name="coupon_codes" class="wc-enhanced-select" data-placeholder="<?php esc_attr_e( 'Choose coupons…', 'woocommerce' ); ?>" style="width:100%;"> 
  178. <option value=""><?php _e( 'All coupons', 'woocommerce' ); ?></option> 
  179. <?php 
  180. foreach ( $used_coupons as $coupon ) { 
  181. echo '<option value="' . esc_attr( $coupon ) . '" ' . selected( in_array( $coupon, $this->coupon_codes ), true, false ) . '>' . $coupon . '</option>'; 
  182. ?> 
  183. </select> 
  184. <input type="submit" class="submit button" value="<?php esc_attr_e( 'Show', 'woocommerce' ); ?>" /> 
  185. <input type="hidden" name="range" value="<?php if ( ! empty( $_GET['range'] ) ) echo esc_attr( $_GET['range'] ) ?>" /> 
  186. <input type="hidden" name="start_date" value="<?php if ( ! empty( $_GET['start_date'] ) ) echo esc_attr( $_GET['start_date'] ) ?>" /> 
  187. <input type="hidden" name="end_date" value="<?php if ( ! empty( $_GET['end_date'] ) ) echo esc_attr( $_GET['end_date'] ) ?>" /> 
  188. <input type="hidden" name="page" value="<?php if ( ! empty( $_GET['page'] ) ) echo esc_attr( $_GET['page'] ) ?>" /> 
  189. <input type="hidden" name="tab" value="<?php if ( ! empty( $_GET['tab'] ) ) echo esc_attr( $_GET['tab'] ) ?>" /> 
  190. <input type="hidden" name="report" value="<?php if ( ! empty( $_GET['report'] ) ) echo esc_attr( $_GET['report'] ) ?>" /> 
  191. <?php else : ?> 
  192. <span><?php _e( 'No used coupons found', 'woocommerce' ); ?></span> 
  193. <?php endif; ?> 
  194. </div> 
  195. </form> 
  196. </div> 
  197. <h4 class="section_title"><span><?php _e( 'Most popular', 'woocommerce' ); ?></span></h4> 
  198. <div class="section"> 
  199. <table cellspacing="0"> 
  200. <?php 
  201. $most_popular = $this->get_order_report_data( array( 
  202. 'data' => array( 
  203. 'order_item_name' => array( 
  204. 'type' => 'order_item',  
  205. 'order_item_type' => 'coupon',  
  206. 'function' => '',  
  207. 'name' => 'coupon_code',  
  208. ),  
  209. 'order_item_id' => array( 
  210. 'type' => 'order_item',  
  211. 'order_item_type' => 'coupon',  
  212. 'function' => 'COUNT',  
  213. 'name' => 'coupon_count',  
  214. ),  
  215. ),  
  216. 'where' => array( 
  217. array( 
  218. 'type' => 'order_item',  
  219. 'key' => 'order_item_type',  
  220. 'value' => 'coupon',  
  221. 'operator' => '=',  
  222. ),  
  223. ),  
  224. 'order_by' => 'coupon_count DESC',  
  225. 'group_by' => 'order_item_name',  
  226. 'limit' => 12,  
  227. 'query_type' => 'get_results',  
  228. 'filter_range' => true,  
  229. ) ); 
  230.  
  231. if ( ! empty( $most_popular ) && is_array( $most_popular ) ) { 
  232. foreach ( $most_popular as $coupon ) { 
  233. echo '<tr class="' . ( in_array( $coupon->coupon_code, $this->coupon_codes ) ? 'active' : '' ) . '"> 
  234. <td class="count" width="1%">' . $coupon->coupon_count . '</td> 
  235. <td class="name"><a href="' . esc_url( add_query_arg( 'coupon_codes', $coupon->coupon_code ) ) . '">' . $coupon->coupon_code . '</a></td> 
  236. </tr>'; 
  237. } else { 
  238. echo '<tr><td colspan="2">' . __( 'No coupons found in range', 'woocommerce' ) . '</td></tr>'; 
  239. ?> 
  240. </table> 
  241. </div> 
  242. <h4 class="section_title"><span><?php _e( 'Most discount', 'woocommerce' ); ?></span></h4> 
  243. <div class="section"> 
  244. <table cellspacing="0"> 
  245. <?php 
  246. $most_discount = $this->get_order_report_data( array( 
  247. 'data' => array( 
  248. 'order_item_name' => array( 
  249. 'type' => 'order_item',  
  250. 'order_item_type' => 'coupon',  
  251. 'function' => '',  
  252. 'name' => 'coupon_code',  
  253. ),  
  254. 'discount_amount' => array( 
  255. 'type' => 'order_item_meta',  
  256. 'order_item_type' => 'coupon',  
  257. 'function' => 'SUM',  
  258. 'name' => 'discount_amount',  
  259. ),  
  260. ),  
  261. 'where' => array( 
  262. array( 
  263. 'type' => 'order_item',  
  264. 'key' => 'order_item_type',  
  265. 'value' => 'coupon',  
  266. 'operator' => '=',  
  267. ),  
  268. ),  
  269. 'order_by' => 'discount_amount DESC',  
  270. 'group_by' => 'order_item_name',  
  271. 'limit' => 12,  
  272. 'query_type' => 'get_results',  
  273. 'filter_range' => true,  
  274. ) ); 
  275.  
  276. if ( ! empty( $most_discount ) && is_array( $most_discount ) ) { 
  277. foreach ( $most_discount as $coupon ) { 
  278. echo '<tr class="' . ( in_array( $coupon->coupon_code, $this->coupon_codes ) ? 'active' : '' ) . '"> 
  279. <td class="count" width="1%">' . wc_price( $coupon->discount_amount ) . '</td> 
  280. <td class="name"><a href="' . esc_url( add_query_arg( 'coupon_codes', $coupon->coupon_code ) ) . '">' . $coupon->coupon_code . '</a></td> 
  281. </tr>'; 
  282. } else { 
  283. echo '<tr><td colspan="3">' . __( 'No coupons found in range', 'woocommerce' ) . '</td></tr>'; 
  284. ?> 
  285. </table> 
  286. </div> 
  287. <script type="text/javascript"> 
  288. jQuery('.section_title').click(function() { 
  289. var next_section = jQuery(this).next('.section'); 
  290.  
  291. if ( jQuery(next_section).is(':visible') ) 
  292. return false; 
  293.  
  294. jQuery('.section:visible').slideUp(); 
  295. jQuery('.section_title').removeClass('open'); 
  296. jQuery(this).addClass('open').next('.section').slideDown(); 
  297.  
  298. return false; 
  299. }); 
  300. jQuery('.section').slideUp( 100, function() { 
  301. <?php if ( empty( $this->coupon_codes ) ) : ?> 
  302. jQuery('.section_title:eq(1)').click(); 
  303. <?php else : ?> 
  304. jQuery('.section_title:eq(0)').click(); 
  305. <?php endif; ?> 
  306. }); 
  307. </script> 
  308. <?php 
  309.  
  310. /** 
  311. * Output an export link. 
  312. */ 
  313. public function get_export_button() { 
  314. $current_range = ! empty( $_GET['range'] ) ? sanitize_text_field( $_GET['range'] ) : '7day'; 
  315. ?> 
  316. <a 
  317. href="#" 
  318. download="report-<?php echo esc_attr( $current_range ); ?>-<?php echo date_i18n( 'Y-m-d', current_time( 'timestamp' ) ); ?>.csv" 
  319. class="export_csv" 
  320. data-export="chart" 
  321. data-xaxes="<?php esc_attr_e( 'Date', 'woocommerce' ); ?>" 
  322. data-groupby="<?php echo $this->chart_groupby; ?>" 
  323. <?php _e( 'Export CSV', 'woocommerce' ); ?> 
  324. </a> 
  325. <?php 
  326.  
  327. /** 
  328. * Get the main chart. 
  329. * @return string 
  330. */ 
  331. public function get_main_chart() { 
  332. global $wp_locale; 
  333.  
  334. // Get orders and dates in range - we want the SUM of order totals, COUNT of order items, COUNT of orders, and the date 
  335. $order_coupon_counts_query = array( 
  336. 'data' => array( 
  337. 'order_item_name' => array( 
  338. 'type' => 'order_item',  
  339. 'order_item_type' => 'coupon',  
  340. 'function' => 'COUNT',  
  341. 'name' => 'order_coupon_count',  
  342. ),  
  343. 'post_date' => array( 
  344. 'type' => 'post_data',  
  345. 'function' => '',  
  346. 'name' => 'post_date',  
  347. ),  
  348. ),  
  349. 'where' => array( 
  350. array( 
  351. 'key' => 'order_item_type',  
  352. 'value' => 'coupon',  
  353. 'operator' => '=',  
  354. ),  
  355. ),  
  356. 'group_by' => $this->group_by_query,  
  357. 'order_by' => 'post_date ASC',  
  358. 'query_type' => 'get_results',  
  359. 'filter_range' => true,  
  360. 'order_types' => wc_get_order_types( 'order-count' ),  
  361. ); 
  362.  
  363. $order_discount_amounts_query = array( 
  364. 'data' => array( 
  365. 'discount_amount' => array( 
  366. 'type' => 'order_item_meta',  
  367. 'order_item_type' => 'coupon',  
  368. 'function' => 'SUM',  
  369. 'name' => 'discount_amount',  
  370. ),  
  371. 'post_date' => array( 
  372. 'type' => 'post_data',  
  373. 'function' => '',  
  374. 'name' => 'post_date',  
  375. ),  
  376. ),  
  377. 'where' => array( 
  378. array( 
  379. 'key' => 'order_item_type',  
  380. 'value' => 'coupon',  
  381. 'operator' => '=',  
  382. ),  
  383. ),  
  384. 'group_by' => $this->group_by_query . ', order_item_name',  
  385. 'order_by' => 'post_date ASC',  
  386. 'query_type' => 'get_results',  
  387. 'filter_range' => true,  
  388. 'order_types' => wc_get_order_types( 'order-count' ),  
  389. ); 
  390.  
  391. if ( ! empty( $this->coupon_codes ) ) { 
  392. $coupon_code_query = array( 
  393. 'type' => 'order_item',  
  394. 'key' => 'order_item_name',  
  395. 'value' => $this->coupon_codes,  
  396. 'operator' => 'IN',  
  397. ); 
  398.  
  399. $order_coupon_counts_query['where'][] = $coupon_code_query; 
  400. $order_discount_amounts_query['where'][] = $coupon_code_query; 
  401.  
  402. $order_coupon_counts = $this->get_order_report_data( $order_coupon_counts_query ); 
  403. $order_discount_amounts = $this->get_order_report_data( $order_discount_amounts_query ); 
  404.  
  405. // Prepare data for report 
  406. $order_coupon_counts = $this->prepare_chart_data( $order_coupon_counts, 'post_date', 'order_coupon_count' , $this->chart_interval, $this->start_date, $this->chart_groupby ); 
  407. $order_discount_amounts = $this->prepare_chart_data( $order_discount_amounts, 'post_date', 'discount_amount', $this->chart_interval, $this->start_date, $this->chart_groupby ); 
  408.  
  409. // Encode in json format 
  410. $chart_data = json_encode( array( 
  411. 'order_coupon_counts' => array_values( $order_coupon_counts ),  
  412. 'order_discount_amounts' => array_values( $order_discount_amounts ),  
  413. ) ); 
  414. ?> 
  415. <div class="chart-container"> 
  416. <div class="chart-placeholder main"></div> 
  417. </div> 
  418. <script type="text/javascript"> 
  419. var main_chart; 
  420.  
  421. jQuery(function() { 
  422. var order_data = jQuery.parseJSON( '<?php echo $chart_data; ?>' ); 
  423.  
  424. var drawGraph = function( highlight ) { 
  425. var series = [ 
  426. label: "<?php echo esc_js( __( 'Number of coupons used', 'woocommerce' ) ) ?>",  
  427. data: order_data.order_coupon_counts,  
  428. color: '<?php echo $this->chart_colours['coupon_count']; ?>',  
  429. bars: { fillColor: '<?php echo $this->chart_colours['coupon_count']; ?>', fill: true, show: true, lineWidth: 0, barWidth: <?php echo $this->barwidth; ?> * 0.5, align: 'center' },  
  430. shadowSize: 0,  
  431. hoverable: false 
  432. },  
  433. label: "<?php echo esc_js( __( 'Discount amount', 'woocommerce' ) ) ?>",  
  434. data: order_data.order_discount_amounts,  
  435. yaxis: 2,  
  436. color: '<?php echo $this->chart_colours['discount_amount']; ?>',  
  437. points: { show: true, radius: 5, lineWidth: 3, fillColor: '#fff', fill: true },  
  438. lines: { show: true, lineWidth: 4, fill: false },  
  439. shadowSize: 0,  
  440. <?php echo $this->get_currency_tooltip(); ?> 
  441. ]; 
  442.  
  443. if ( highlight !== 'undefined' && series[ highlight ] ) { 
  444. highlight_series = series[ highlight ]; 
  445.  
  446. highlight_series.color = '#9c5d90'; 
  447.  
  448. if ( highlight_series.bars ) 
  449. highlight_series.bars.fillColor = '#9c5d90'; 
  450.  
  451. if ( highlight_series.lines ) { 
  452. highlight_series.lines.lineWidth = 5; 
  453.  
  454. main_chart = jQuery.plot( 
  455. jQuery('.chart-placeholder.main'),  
  456. series,  
  457. legend: { 
  458. show: false 
  459. },  
  460. grid: { 
  461. color: '#aaa',  
  462. borderColor: 'transparent',  
  463. borderWidth: 0,  
  464. hoverable: true 
  465. },  
  466. xaxes: [ { 
  467. color: '#aaa',  
  468. position: "bottom",  
  469. tickColor: 'transparent',  
  470. mode: "time",  
  471. timeformat: "<?php echo ( 'day' === $this->chart_groupby ) ? '%d %b' : '%b'; ?>",  
  472. monthNames: <?php echo json_encode( array_values( $wp_locale->month_abbrev ) ) ?>,  
  473. tickLength: 1,  
  474. minTickSize: [1, "<?php echo $this->chart_groupby; ?>"],  
  475. font: { 
  476. color: "#aaa" 
  477. } ],  
  478. yaxes: [ 
  479. min: 0,  
  480. minTickSize: 1,  
  481. tickDecimals: 0,  
  482. color: '#ecf0f1',  
  483. font: { color: "#aaa" } 
  484. },  
  485. position: "right",  
  486. min: 0,  
  487. tickDecimals: 2,  
  488. alignTicksWithAxis: 1,  
  489. color: 'transparent',  
  490. font: { color: "#aaa" } 
  491. ],  
  492. ); 
  493.  
  494. jQuery('.chart-placeholder').resize(); 
  495.  
  496. drawGraph(); 
  497.  
  498. jQuery('.highlight_series').hover( 
  499. function() { 
  500. drawGraph( jQuery(this).data('series') ); 
  501. },  
  502. function() { 
  503. drawGraph(); 
  504. ); 
  505. }); 
  506. </script> 
  507. <?php