| 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 Summary { |
| 16 |
|
| 17 |
/** |
| 18 |
* All valid discounts. |
| 19 |
* |
| 20 |
* @var $data array Discounts. |
| 21 |
*/ |
| 22 |
public $data; |
| 23 |
|
| 24 |
/** |
| 25 |
* Class instance. |
| 26 |
* |
| 27 |
* @since 1.1.0 |
| 28 |
* @var Summary |
| 29 |
*/ |
| 30 |
private static $instance = null; |
| 31 |
|
| 32 |
/** |
| 33 |
* Gets class instance. |
| 34 |
* |
| 35 |
* @since 1.1.0 |
| 36 |
* @return Summary |
| 37 |
*/ |
| 38 |
public static function get_instance() { |
| 39 |
if ( null === self::$instance ) { |
| 40 |
self::$instance = new self(); |
| 41 |
} |
| 42 |
|
| 43 |
return self::$instance; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Class constructor. |
| 48 |
* |
| 49 |
* @since 1.1.0 |
| 50 |
*/ |
| 51 |
public function __construct() { |
| 52 |
$this->set_discounts_details(); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Set discount details. |
| 57 |
* |
| 58 |
* @since 1.1.0 |
| 59 |
*/ |
| 60 |
public function set_discounts_details() { |
| 61 |
global $wpdb; |
| 62 |
|
| 63 |
$rule_id = sellkit_htmlspecialchars( INPUT_GET, 'target_id' ); |
| 64 |
|
| 65 |
$start_date = time() - ( 60 * 60 * 24 * Analytics::$date_range ); |
| 66 |
|
| 67 |
$sellkit_prefix = Database::DATABASE_PREFIX; |
| 68 |
$discount_table = "{$wpdb->prefix}{$sellkit_prefix}applied_coupon"; |
| 69 |
$posts_table = "{$wpdb->prefix}posts "; |
| 70 |
$posts_meta_table = "{$wpdb->prefix}postmeta "; |
| 71 |
$coupon_start_date = gmdate( 'Y-m-d H:i:s', time() - ( 60 * 60 * 24 * Analytics::$date_range ) ); |
| 72 |
|
| 73 |
// phpcs:disable |
| 74 |
$coupons = $wpdb->get_results( |
| 75 |
$wpdb->prepare( "SELECT count(*) as created_coupon FROM {$posts_table} AS p |
| 76 |
LEFT JOIN {$posts_meta_table} pm ON pm.post_id = p.id AND pm.meta_key = 'sellkit_personalised_coupon_rule' |
| 77 |
WHERE %s < p.post_date_gmt and p.post_type = 'shop_coupon' and pm.meta_value " . Analytics::target_id_condition( $rule_id ) . " %s", $coupon_start_date, $rule_id ), |
| 78 |
ARRAY_A ); |
| 79 |
|
| 80 |
$this->data['created_coupon'] = ! empty( $coupons[0]['created_coupon'] ) ? $coupons[0]['created_coupon'] : 0; |
| 81 |
|
| 82 |
$query_result = $wpdb->get_results( |
| 83 |
$wpdb->prepare( "SELECT count(*) as used_coupon FROM {$discount_table} |
| 84 |
where applied_at > {$start_date} and rule_id " . Analytics::target_id_condition( $rule_id ) . " %d", $rule_id ), |
| 85 |
ARRAY_A ); |
| 86 |
|
| 87 |
$this->data['used_coupon'] = ! empty( $query_result[0]['used_coupon'] ) ? $query_result[0]['used_coupon'] : 0; |
| 88 |
|
| 89 |
$monetary_data = $wpdb->get_results( |
| 90 |
$wpdb->prepare( "SELECT SUM( revenue ) as revenue, SUM( total_discount ) as total_discount FROM {$discount_table} |
| 91 |
where applied_at > {$start_date} and rule_id " . Analytics::target_id_condition( $rule_id ) . " %d", $rule_id ), |
| 92 |
ARRAY_A ); |
| 93 |
|
| 94 |
$this->data['revenue'] = ! empty( $monetary_data[0]['revenue'] ) ? round( $monetary_data[0]['revenue'], 2 ) : 0; |
| 95 |
$this->data['total_discount'] = ! empty( $monetary_data[0]['total_discount'] ) ? round( $monetary_data[0]['total_discount'], 2 ) : 0; |
| 96 |
|
| 97 |
$customers = $wpdb->get_results( |
| 98 |
$wpdb->prepare( "SELECT count(DISTINCT(email)) as customers FROM {$discount_table} |
| 99 |
where applied_at > {$start_date} and rule_id " . Analytics::target_id_condition( $rule_id ) . " %d GROUP BY email; ", $rule_id ), |
| 100 |
ARRAY_A ); |
| 101 |
|
| 102 |
$this->data['customers'] = ! empty( $customers ) && is_array( $customers ) ? count( $customers ) : 0; |
| 103 |
// phpcs:enable |
| 104 |
|
| 105 |
if ( empty( $this->data['used_coupon'] ) ) { |
| 106 |
$this->data['conversion_rate'] = 0; |
| 107 |
} |
| 108 |
|
| 109 |
if ( ! empty( $this->data['used_coupon'] ) ) { |
| 110 |
$this->data['conversion_rate'] = ( $this->data['customers'] / $this->data['created_coupon'] ) * 100; |
| 111 |
$this->data['conversion_rate'] = floatval( number_format( $this->data['conversion_rate'], 2 ) ); |
| 112 |
} |
| 113 |
|
| 114 |
$this->data['target_title'] = sellkit_decode_entity_title( get_the_title( $rule_id ) ); |
| 115 |
} |
| 116 |
} |
| 117 |
|