PluginProbe
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster / 1.2.3
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster v1.2.3
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 / discount / conversion-rate.php

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

119 lines 2.8 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\Discount;
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 * All valid discoints.
19 *
20 * @var $discounts array Discounts.
21 */
22 public $discounts;
23
24 /**
25 * Abandoned discounts.
26 *
27 * @var $discounts array Abandoned discounts.
28 */
29 public $applied_discounts;
30
31 /**
32 * Discount id.
33 *
34 * @var $discount_id int discount_id.
35 */
36 public $discount_id;
37
38 /**
39 * Class constructor.
40 *
41 * @since 1.1.0
42 * @param int $discount_id Discount id.
43 */
44 public function __construct( $discount_id ) {
45 $this->discount_id = $discount_id;
46
47 $this->set_discounts_details();
48 }
49
50 /**
51 * Set discount details.
52 *
53 * @since 1.1.0
54 */
55 public function set_discounts_details() {
56 global $wpdb;
57
58 $start_date = time() - ( 60 * 60 * 24 * Analytics::$date_range );
59
60 $sellkit_prefix = Database::DATABASE_PREFIX;
61 $discount_table = "{$wpdb->prefix}{$sellkit_prefix}applied_discount";
62
63 // phpcs:disable
64 $applied_discounts = $wpdb->get_results(
65 $wpdb->prepare( "SELECT FROM_UNIXTIME(applied_at, '%%b_%%d') as `day`, count(*) as total FROM {$discount_table}
66 where applied_at > {$start_date} and `order_id` IS NOT NULL and discount_id " . Analytics::target_id_condition( $this->discount_id ) . " %d
67 GROUP BY `day` ORDER BY `day` ASC", $this->discount_id ),
68 ARRAY_A );
69
70 $discounts = $wpdb->get_results(
71 $wpdb->prepare( "SELECT FROM_UNIXTIME(applied_at, '%%b_%%d') as `day`, count(*) as total FROM {$discount_table}
72 where applied_at > {$start_date} and discount_id " . Analytics::target_id_condition( $this->discount_id ) . " %d
73 GROUP BY `day` ORDER BY `day` ASC", $this->discount_id ),
74 ARRAY_A );
75 // phpcs:enable
76
77 $prepared_applied_discount = [];
78 foreach ( $applied_discounts as $abandoned_discount ) {
79 $prepared_applied_discount[ $abandoned_discount['day'] ] = $abandoned_discount['total'];
80 }
81
82 $prepared_discount = [];
83 foreach ( $discounts as $discount ) {
84 $prepared_discount[ $discount['day'] ] = $discount['total'];
85 }
86
87 $this->applied_discounts = $prepared_applied_discount;
88 $this->discounts = $prepared_discount;
89 }
90
91 /**
92 * Getting chart data.
93 *
94 * @since 1.1.0
95 */
96 public function get_data() {
97 $chart_data = [];
98
99 for ( $i = Analytics::$date_range; $i >= 0; $i-- ) {
100 $time = strtotime( "-{$i} days" );
101 $date = date( 'M_d', $time );
102 $result = [
103 'date' => str_replace( '_', ' ', $date ),
104 'value' => 0,
105 ];
106
107 if ( $this->discounts[ $date ] && $this->applied_discounts[ $date ] ) {
108 $result['value'] = ( $this->applied_discounts[ $date ] / $this->discounts[ $date ] ) * 100;
109 $result['value'] = round( $result['value'], 2 );
110 }
111
112 $chart_data[] = $result;
113 }
114
115 return $chart_data;
116 }
117
118 }
119