| 1 |
<?php |
| 2 |
|
| 3 |
namespace Sellkit\Admin\Components\Analytics\Coupon; |
| 4 |
|
| 5 |
use Sellkit\Admin\Components\Analytics; |
| 6 |
use Sellkit\Admin\Components\Analytics\Analytics_Base; |
| 7 |
|
| 8 |
defined( 'ABSPATH' ) || die(); |
| 9 |
|
| 10 |
/** |
| 11 |
* Components class. |
| 12 |
* |
| 13 |
* @since 1.1.0 |
| 14 |
*/ |
| 15 |
class Created_Coupon extends Analytics_Base { |
| 16 |
|
| 17 |
/** |
| 18 |
* Class constructor. |
| 19 |
* |
| 20 |
* @since 1.1.0 |
| 21 |
* @param int $rule_id Discount id. |
| 22 |
*/ |
| 23 |
public function __construct( $rule_id ) { |
| 24 |
$this->target_id = $rule_id; |
| 25 |
|
| 26 |
$this->set_coupons_details(); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Set discount details. |
| 31 |
* |
| 32 |
* @since 1.1.0 |
| 33 |
*/ |
| 34 |
public function set_coupons_details() { |
| 35 |
global $wpdb; |
| 36 |
|
| 37 |
$start_date = date( 'Y-m-d H:i:s', time() - ( 60 * 60 * 24 * Analytics::$date_range ) ); |
| 38 |
$posts_table = "{$wpdb->prefix}posts "; |
| 39 |
$posts_meta_table = "{$wpdb->prefix}postmeta "; |
| 40 |
|
| 41 |
// phpcs:disable |
| 42 |
$coupons = $wpdb->get_results( |
| 43 |
$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 |
| 44 |
LEFT JOIN {$posts_meta_table} pm ON pm.post_id = p.id AND pm.meta_key = 'sellkit_personalised_coupon_rule' |
| 45 |
WHERE %s < p.post_date_gmt and p.post_type = 'shop_coupon' and pm.meta_value " . Analytics::target_id_condition( $this->target_id ) . " %s |
| 46 |
GROUP BY DAY(p.post_date_gmt)", $start_date, $this->target_id ), |
| 47 |
ARRAY_A ); |
| 48 |
// phpcs:enable |
| 49 |
|
| 50 |
$neat_data = []; |
| 51 |
|
| 52 |
foreach ( $coupons as $coupon ) { |
| 53 |
$neat_data[ $coupon['day'] ] = intval( $coupon['total'] ); |
| 54 |
} |
| 55 |
|
| 56 |
$this->output = $neat_data; |
| 57 |
} |
| 58 |
} |
| 59 |
|