| 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 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 |
$discount_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_discount"; |
| 69 |
|
| 70 |
// phpcs:disable |
| 71 |
$query_result = $wpdb->get_results( |
| 72 |
$wpdb->prepare( "SELECT count(*) as applied_discount FROM {$discount_table} |
| 73 |
where applied_at > {$start_date} and discount_id " . Analytics::target_id_condition( $discount_id ) . " %d", $discount_id), |
| 74 |
ARRAY_A ); |
| 75 |
|
| 76 |
$this->data['applied_discount'] = ! empty( $query_result[0]['applied_discount'] ) ? $query_result[0]['applied_discount'] : 0; |
| 77 |
|
| 78 |
$query_result = $wpdb->get_results( |
| 79 |
$wpdb->prepare( "SELECT count(*) as converted FROM {$discount_table} |
| 80 |
where applied_at > {$start_date} and order_id IS NOT NULL and discount_id " . Analytics::target_id_condition( $discount_id ) . " %d", $discount_id ), |
| 81 |
ARRAY_A ); |
| 82 |
|
| 83 |
$this->data['converted'] = ! empty( $query_result[0]['converted'] ) ? $query_result[0]['converted'] : 0; |
| 84 |
|
| 85 |
$revenues = $wpdb->get_results( |
| 86 |
$wpdb->prepare( "SELECT SUM( order_total ) as revenue FROM {$discount_table} |
| 87 |
where applied_at > {$start_date} and order_id IS NOT NULL and discount_id " . Analytics::target_id_condition( $discount_id ) . " %d", $discount_id ), |
| 88 |
ARRAY_A ); |
| 89 |
|
| 90 |
$this->data['revenue'] = ! empty( $revenues[0]['revenue'] ) ? round( $revenues[0]['revenue'], 2 ) : 0; |
| 91 |
|
| 92 |
|
| 93 |
$discounts = $wpdb->get_results( |
| 94 |
$wpdb->prepare( "SELECT SUM( total_amount ) as total_discount FROM {$discount_table} |
| 95 |
where applied_at > {$start_date} and order_id IS NOT NULL and discount_id " . Analytics::target_id_condition( $discount_id ) . " %d", $discount_id ), |
| 96 |
ARRAY_A ); |
| 97 |
|
| 98 |
$this->data['total_discount'] = ! empty( $discounts[0]['total_discount'] ) ? round( $discounts[0]['total_discount'], 2 ) : 0; |
| 99 |
|
| 100 |
$customers = $wpdb->get_results( |
| 101 |
$wpdb->prepare( "SELECT count(DISTINCT(email)) as customers FROM {$discount_table} |
| 102 |
where applied_at > {$start_date} and discount_id " . Analytics::target_id_condition( $discount_id ) . " %d GROUP BY email; ", $discount_id ), |
| 103 |
ARRAY_A ); |
| 104 |
|
| 105 |
$this->data['customers'] = ! empty( $customers ) && is_array( $customers ) ? count( $customers ) : 0; |
| 106 |
// phpcs:enable |
| 107 |
|
| 108 |
if ( empty( $this->data['converted'] ) ) { |
| 109 |
$this->data['conversion_rate'] = 0; |
| 110 |
} |
| 111 |
|
| 112 |
if ( ! empty( $this->data['converted'] ) ) { |
| 113 |
$this->data['conversion_rate'] = ( $this->data['converted'] / $this->data['applied_discount'] ) * 100; |
| 114 |
$this->data['conversion_rate'] = round( $this->data['conversion_rate'], 2 ); |
| 115 |
} |
| 116 |
|
| 117 |
$this->data['target_title'] = sellkit_decode_entity_title( get_the_title( $discount_id ) ); |
| 118 |
} |
| 119 |
} |
| 120 |
|