PluginProbe
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster / trunk
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster vtrunk
2.6.0 trunk 1.1.0 1.1.4 1.2.1 1.2.2 1.2.3 1.2.5 1.2.9 1.3.1 1.3.2 1.5.0 1.5.1 1.5.4 1.5.7 1.5.8 1.5.9 1.6.2 1.6.5 1.6.8 1.7.2 1.7.4 1.7.5 1.7.9 1.8.1 All 42 releases
sellkit / includes / admin / components / analytics / coupon / conversion-rate.php

conversion-rate.php in SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster trunk, at includes/admin/components/analytics/coupon/conversion-rate.php

121 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Sellkit\Admin\Components\Analytics\Coupon;
4
5 use Sellkit\Admin\Components\Analytics;
6 use Sellkit\Database;
7
8 defined( 'ABSPATH' ) || die();
9
10 /**
11 * Components class.
12 *
13 * @since 1.1.0
14 */
15 class Conversion_Rate {
16
17 /**
18 * Output data.
19 *
20 * @var $coupons array Discounts.
21 */
22 public $coupons;
23
24 /**
25 * Abandoned coupons.
26 *
27 * @var $coupons array Abandoned coupons.
28 */
29 public $applied_coupons;
30
31 /**
32 * Coupon id.
33 *
34 * @var $coupon_id int coupon_id.
35 */
36 public $coupon_id;
37
38 /**
39 * Class constructor.
40 *
41 * @since 1.1.0
42 * @param int $coupon_id Discount id.
43 */
44 public function __construct( $coupon_id ) {
45 $this->coupon_id = $coupon_id;
46
47 $this->set_coupons_details();
48 }
49
50 /**
51 * Set discount details.
52 *
53 * @since 1.1.0
54 */
55 public function set_coupons_details() {
56 global $wpdb;
57
58 $start_date = time() - ( 60 * 60 * 24 * Analytics::$date_range );
59
60 $sellkit_prefix = Database::DATABASE_PREFIX;
61 $coupon_table = "{$wpdb->prefix}{$sellkit_prefix}applied_coupon";
62 $posts_table = "{$wpdb->prefix}posts ";
63 $posts_meta_table = "{$wpdb->prefix}postmeta ";
64
65 // phpcs:disable
66 $applied_coupons = $wpdb->get_results(
67 $wpdb->prepare( "SELECT FROM_UNIXTIME(applied_at, '%%b_%%d') as `day`, count(*) as total FROM {$coupon_table}
68 where applied_at > {$start_date} and rule_id = %d
69 GROUP BY `day` ORDER BY `day` ASC", $this->coupon_id ),
70 ARRAY_A );
71
72 $coupons = $wpdb->get_results(
73 $wpdb->prepare( "SELECT count(p.id) as total, pm.meta_key, DATE_FORMAT( p.post_date_gmt, '%%b_%%d') as day FROM {$posts_table} AS p
74 LEFT JOIN {$posts_meta_table} pm ON pm.post_id = p.id AND pm.meta_key = 'sellkit_personalised_coupon_rule'
75 WHERE %s < p.post_date_gmt and p.post_type = 'shop_coupon' and pm.meta_value = %s
76 GROUP BY DAY(p.post_date_gmt)", $start_date, $this->coupon_id ),
77 ARRAY_A );
78 // phpcs:enable
79
80 $prepared_applied_coupon = [];
81 foreach ( $applied_coupons as $abandoned_discount ) {
82 $prepared_applied_coupon[ $abandoned_discount['day'] ] = $abandoned_discount['total'];
83 }
84
85 $prepared_coupon = [];
86 foreach ( $coupons as $discount ) {
87 $prepared_coupon[ $discount['day'] ] = $discount['total'];
88 }
89
90 $this->applied_coupons = $prepared_applied_coupon;
91 $this->coupons = $prepared_coupon;
92 }
93
94 /**
95 * Getting chart data.
96 *
97 * @since 1.1.0
98 */
99 public function get_data() {
100 $chart_data = [];
101
102 for ( $i = Analytics::$date_range; $i >= 0; $i-- ) {
103 $time = strtotime( "-{$i} days" );
104 $date = gmdate( 'M_d', $time );
105 $result = [
106 'date' => str_replace( '_', ' ', $date ),
107 'value' => 0,
108 ];
109
110 if ( $this->coupons[ $date ] && $this->applied_coupons[ $date ] ) {
111 $result['value'] = number_format( ( $this->applied_coupons[ $date ] / $this->coupons[ $date ] ) * 100, 2 );
112 $result['value'] = floatval( $result['value'] );
113 }
114
115 $chart_data[] = $result;
116 }
117
118 return $chart_data;
119 }
120 }
121