2018-12-05 12:16:46 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2019-01-15 01:53:02 +00:00
|
|
|
* WC_Admin_Reports_Coupons_Data_Store class file.
|
2018-12-05 12:16:46 +00:00
|
|
|
*
|
|
|
|
* @package WooCommerce Admin/Classes
|
|
|
|
*/
|
|
|
|
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WC_Admin_Reports_Coupons_Data_Store.
|
|
|
|
*/
|
|
|
|
class WC_Admin_Reports_Coupons_Data_Store extends WC_Admin_Reports_Data_Store implements WC_Admin_Reports_Data_Store_Interface {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Table used to get the data.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
const TABLE_NAME = 'wc_order_coupon_lookup';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mapping columns to data type to return correct response types.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $column_types = array(
|
2018-12-14 11:40:49 +00:00
|
|
|
'coupon_id' => 'intval',
|
|
|
|
'amount' => 'floatval',
|
|
|
|
'orders_count' => 'intval',
|
2018-12-05 12:16:46 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SQL columns to select in the db query and their mapping to SQL code.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $report_columns = array(
|
2018-12-14 11:40:49 +00:00
|
|
|
'coupon_id' => 'coupon_id',
|
|
|
|
'amount' => 'SUM(discount_amount) as amount',
|
|
|
|
'orders_count' => 'COUNT(DISTINCT order_id) as orders_count',
|
2018-12-05 12:16:46 +00:00
|
|
|
);
|
|
|
|
|
2019-01-16 02:23:00 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
|
|
|
public function __construct() {
|
|
|
|
global $wpdb;
|
|
|
|
$table_name = $wpdb->prefix . self::TABLE_NAME;
|
|
|
|
// Avoid ambigious column order_id in SQL query.
|
|
|
|
$this->report_columns['orders_count'] = str_replace( 'order_id', $table_name . '.order_id', $this->report_columns['orders_count'] );
|
|
|
|
}
|
|
|
|
|
2019-02-05 18:15:39 +00:00
|
|
|
/**
|
|
|
|
* Set up all the hooks for maintaining and populating table data.
|
|
|
|
*/
|
|
|
|
public static function init() {
|
|
|
|
add_action( 'woocommerce_reports_delete_order_stats', array( __CLASS__, 'sync_on_order_delete' ), 5 );
|
|
|
|
}
|
|
|
|
|
2018-12-05 12:16:46 +00:00
|
|
|
/**
|
|
|
|
* Returns comma separated ids of included coupons, based on query arguments from the user.
|
|
|
|
*
|
|
|
|
* @param array $query_args Parameters supplied by the user.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function get_included_coupons( $query_args ) {
|
|
|
|
$included_coupons_str = '';
|
|
|
|
|
2018-12-19 12:37:54 +00:00
|
|
|
if ( isset( $query_args['coupons'] ) && is_array( $query_args['coupons'] ) && count( $query_args['coupons'] ) > 0 ) {
|
|
|
|
$included_coupons_str = implode( ',', $query_args['coupons'] );
|
2018-12-05 12:16:46 +00:00
|
|
|
}
|
|
|
|
return $included_coupons_str;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the database query with parameters used for Products report: categories and order status.
|
|
|
|
*
|
|
|
|
* @param array $query_args Query arguments supplied by the user.
|
|
|
|
* @return array Array of parameters used for SQL query.
|
|
|
|
*/
|
|
|
|
protected function get_sql_query_params( $query_args ) {
|
|
|
|
global $wpdb;
|
|
|
|
$order_coupon_lookup_table = $wpdb->prefix . self::TABLE_NAME;
|
|
|
|
|
|
|
|
$sql_query_params = $this->get_time_period_sql_params( $query_args, $order_coupon_lookup_table );
|
|
|
|
$sql_query_params = array_merge( $sql_query_params, $this->get_limit_sql_params( $query_args ) );
|
|
|
|
$sql_query_params = array_merge( $sql_query_params, $this->get_order_by_sql_params( $query_args ) );
|
|
|
|
|
|
|
|
$included_coupons = $this->get_included_coupons( $query_args );
|
|
|
|
if ( $included_coupons ) {
|
|
|
|
$sql_query_params['where_clause'] .= " AND {$order_coupon_lookup_table}.coupon_id IN ({$included_coupons})";
|
|
|
|
}
|
|
|
|
|
|
|
|
$order_status_filter = $this->get_status_subquery( $query_args );
|
|
|
|
if ( $order_status_filter ) {
|
2019-01-16 02:23:00 +00:00
|
|
|
$sql_query_params['from_clause'] .= " JOIN {$wpdb->prefix}wc_order_stats ON {$order_coupon_lookup_table}.order_id = {$wpdb->prefix}wc_order_stats.order_id";
|
2018-12-05 12:16:46 +00:00
|
|
|
$sql_query_params['where_clause'] .= " AND ( {$order_status_filter} )";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $sql_query_params;
|
|
|
|
}
|
|
|
|
|
2019-01-10 17:10:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fills ORDER BY clause of SQL request based on user supplied parameters.
|
|
|
|
*
|
|
|
|
* @param array $query_args Parameters supplied by the user.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function get_order_by_sql_params( $query_args ) {
|
|
|
|
global $wpdb;
|
|
|
|
$lookup_table = $wpdb->prefix . self::TABLE_NAME;
|
|
|
|
$sql_query = array();
|
|
|
|
$sql_query['from_clause'] = '';
|
|
|
|
$sql_query['order_by_clause'] = '';
|
|
|
|
if ( isset( $query_args['orderby'] ) ) {
|
|
|
|
$sql_query['order_by_clause'] = $this->normalize_order_by( $query_args['orderby'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( false !== strpos( $sql_query['order_by_clause'], '_coupons' ) ) {
|
|
|
|
$sql_query['from_clause'] .= " JOIN {$wpdb->prefix}posts AS _coupons ON {$lookup_table}.coupon_id = _coupons.ID";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( isset( $query_args['order'] ) ) {
|
|
|
|
$sql_query['order_by_clause'] .= ' ' . $query_args['order'];
|
|
|
|
} else {
|
|
|
|
$sql_query['order_by_clause'] .= ' DESC';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $sql_query;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maps ordering specified by the user to columns in the database/fields in the data.
|
|
|
|
*
|
|
|
|
* @param string $order_by Sorting criterion.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function normalize_order_by( $order_by ) {
|
|
|
|
if ( 'date' === $order_by ) {
|
|
|
|
return 'time_interval';
|
|
|
|
}
|
|
|
|
if ( 'code' === $order_by ) {
|
|
|
|
return '_coupons.post_title';
|
|
|
|
}
|
|
|
|
return $order_by;
|
|
|
|
}
|
|
|
|
|
2018-12-10 15:39:20 +00:00
|
|
|
/**
|
|
|
|
* Enriches the coupon data with extra attributes.
|
|
|
|
*
|
|
|
|
* @param array $coupon_data Coupon data.
|
|
|
|
* @param array $query_args Query parameters.
|
|
|
|
*/
|
|
|
|
protected function include_extended_info( &$coupon_data, $query_args ) {
|
2018-12-10 18:07:46 +00:00
|
|
|
foreach ( $coupon_data as $idx => $coupon_datum ) {
|
|
|
|
$extended_info = new ArrayObject();
|
|
|
|
if ( $query_args['extended_info'] ) {
|
2018-12-10 15:39:20 +00:00
|
|
|
$coupon_id = $coupon_datum['coupon_id'];
|
|
|
|
$coupon = new WC_Coupon( $coupon_id );
|
|
|
|
|
|
|
|
$gmt_timzone = new DateTimeZone( 'UTC' );
|
|
|
|
|
|
|
|
$date_expires = $coupon->get_date_expires();
|
|
|
|
if ( null === $date_expires ) {
|
|
|
|
$date_expires = '';
|
|
|
|
$date_expires_gmt = '';
|
|
|
|
} else {
|
|
|
|
$date_expires = $date_expires->format( WC_Admin_Reports_Interval::$iso_datetime_format );
|
|
|
|
$date_expires_gmt = new DateTime( $date_expires );
|
|
|
|
$date_expires_gmt->setTimezone( $gmt_timzone );
|
|
|
|
$date_expires_gmt = $date_expires_gmt->format( WC_Admin_Reports_Interval::$iso_datetime_format );
|
|
|
|
}
|
|
|
|
|
|
|
|
$date_created = $coupon->get_date_created();
|
|
|
|
if ( null === $date_created ) {
|
|
|
|
$date_created = '';
|
|
|
|
$date_created_gmt = '';
|
|
|
|
} else {
|
|
|
|
$date_created = $date_created->format( WC_Admin_Reports_Interval::$iso_datetime_format );
|
|
|
|
$date_created_gmt = new DateTime( $date_created );
|
|
|
|
$date_created_gmt->setTimezone( $gmt_timzone );
|
|
|
|
$date_created_gmt = $date_created_gmt->format( WC_Admin_Reports_Interval::$iso_datetime_format );
|
|
|
|
}
|
|
|
|
|
2018-12-10 18:07:46 +00:00
|
|
|
$extended_info = array(
|
2018-12-10 15:39:20 +00:00
|
|
|
'code' => $coupon->get_code(),
|
|
|
|
'date_created' => $date_created,
|
|
|
|
'date_created_gmt' => $date_created_gmt,
|
|
|
|
'date_expires' => $date_expires,
|
|
|
|
'date_expires_gmt' => $date_expires_gmt,
|
|
|
|
'discount_type' => $coupon->get_discount_type(),
|
|
|
|
);
|
|
|
|
}
|
2018-12-10 18:07:46 +00:00
|
|
|
$coupon_data[ $idx ]['extended_info'] = $extended_info;
|
2018-12-10 15:39:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 12:16:46 +00:00
|
|
|
/**
|
|
|
|
* Returns the report data based on parameters supplied by the user.
|
|
|
|
*
|
|
|
|
* @param array $query_args Query parameters.
|
|
|
|
* @return stdClass|WP_Error Data.
|
|
|
|
*/
|
|
|
|
public function get_data( $query_args ) {
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
$table_name = $wpdb->prefix . self::TABLE_NAME;
|
|
|
|
|
|
|
|
// These defaults are only partially applied when used via REST API, as that has its own defaults.
|
|
|
|
$defaults = array(
|
2018-12-10 15:39:20 +00:00
|
|
|
'per_page' => get_option( 'posts_per_page' ),
|
|
|
|
'page' => 1,
|
|
|
|
'order' => 'DESC',
|
2018-12-10 17:49:38 +00:00
|
|
|
'orderby' => 'coupon_id',
|
2019-02-10 16:07:45 +00:00
|
|
|
'before' => WC_Admin_Reports_Interval::default_before(),
|
|
|
|
'after' => WC_Admin_Reports_Interval::default_after(),
|
2018-12-10 15:39:20 +00:00
|
|
|
'fields' => '*',
|
2018-12-19 12:37:54 +00:00
|
|
|
'coupons' => array(),
|
2018-12-10 15:39:20 +00:00
|
|
|
'extended_info' => false,
|
2018-12-05 12:16:46 +00:00
|
|
|
);
|
|
|
|
$query_args = wp_parse_args( $query_args, $defaults );
|
2019-02-08 17:11:04 +00:00
|
|
|
$this->normalize_timezones( $query_args );
|
2018-12-05 12:16:46 +00:00
|
|
|
|
|
|
|
$cache_key = $this->get_cache_key( $query_args );
|
|
|
|
$data = wp_cache_get( $cache_key, $this->cache_group );
|
|
|
|
|
|
|
|
if ( false === $data ) {
|
|
|
|
$data = (object) array(
|
|
|
|
'data' => array(),
|
|
|
|
'total' => 0,
|
|
|
|
'pages' => 0,
|
|
|
|
'page_no' => 0,
|
|
|
|
);
|
|
|
|
|
|
|
|
$selections = $this->selected_columns( $query_args );
|
|
|
|
$sql_query_params = $this->get_sql_query_params( $query_args );
|
|
|
|
|
|
|
|
$db_records_count = (int) $wpdb->get_var(
|
|
|
|
"SELECT COUNT(*) FROM (
|
|
|
|
SELECT
|
|
|
|
coupon_id
|
|
|
|
FROM
|
|
|
|
{$table_name}
|
|
|
|
{$sql_query_params['from_clause']}
|
|
|
|
WHERE
|
|
|
|
1=1
|
|
|
|
{$sql_query_params['where_time_clause']}
|
|
|
|
{$sql_query_params['where_clause']}
|
|
|
|
GROUP BY
|
|
|
|
coupon_id
|
|
|
|
) AS tt"
|
|
|
|
); // WPCS: cache ok, DB call ok, unprepared SQL ok.
|
|
|
|
|
|
|
|
$total_pages = (int) ceil( $db_records_count / $sql_query_params['per_page'] );
|
|
|
|
if ( $query_args['page'] < 1 || $query_args['page'] > $total_pages ) {
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
$coupon_data = $wpdb->get_results(
|
|
|
|
"SELECT
|
|
|
|
{$selections}
|
|
|
|
FROM
|
|
|
|
{$table_name}
|
|
|
|
{$sql_query_params['from_clause']}
|
|
|
|
WHERE
|
|
|
|
1=1
|
|
|
|
{$sql_query_params['where_time_clause']}
|
|
|
|
{$sql_query_params['where_clause']}
|
|
|
|
GROUP BY
|
|
|
|
coupon_id
|
|
|
|
ORDER BY
|
|
|
|
{$sql_query_params['order_by_clause']}
|
|
|
|
{$sql_query_params['limit']}
|
|
|
|
",
|
|
|
|
ARRAY_A
|
|
|
|
); // WPCS: cache ok, DB call ok, unprepared SQL ok.
|
|
|
|
|
|
|
|
if ( null === $coupon_data ) {
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2018-12-10 15:39:20 +00:00
|
|
|
$this->include_extended_info( $coupon_data, $query_args );
|
|
|
|
|
2018-12-05 12:16:46 +00:00
|
|
|
$coupon_data = array_map( array( $this, 'cast_numbers' ), $coupon_data );
|
2019-02-05 17:42:34 +00:00
|
|
|
$data = (object) array(
|
2018-12-05 12:16:46 +00:00
|
|
|
'data' => $coupon_data,
|
|
|
|
'total' => $db_records_count,
|
|
|
|
'pages' => $total_pages,
|
|
|
|
'page_no' => (int) $query_args['page'],
|
|
|
|
);
|
|
|
|
|
|
|
|
wp_cache_set( $cache_key, $data, $this->cache_group );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns string to be used as cache key for the data.
|
|
|
|
*
|
|
|
|
* @param array $params Query parameters.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function get_cache_key( $params ) {
|
|
|
|
return 'woocommerce_' . self::TABLE_NAME . '_' . md5( wp_json_encode( $params ) );
|
|
|
|
}
|
|
|
|
|
2018-12-27 03:49:00 +00:00
|
|
|
/**
|
|
|
|
* Create or update an an entry in the wc_order_coupon_lookup table for an order.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
* @param int $order_id Order ID.
|
2019-01-29 21:36:02 +00:00
|
|
|
* @return int|bool Returns -1 if order won't be processed, or a boolean indicating processing success.
|
2018-12-27 03:49:00 +00:00
|
|
|
*/
|
|
|
|
public static function sync_order_coupons( $order_id ) {
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
$order = wc_get_order( $order_id );
|
|
|
|
if ( ! $order ) {
|
2019-01-29 21:36:02 +00:00
|
|
|
return -1;
|
2018-12-27 03:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$coupon_items = $order->get_items( 'coupon' );
|
2019-01-29 21:36:02 +00:00
|
|
|
$num_updated = 0;
|
|
|
|
|
2018-12-27 03:49:00 +00:00
|
|
|
foreach ( $coupon_items as $coupon_item ) {
|
2019-02-01 17:43:24 +00:00
|
|
|
$coupon_id = wc_get_coupon_id_by_code( $coupon_item->get_code() );
|
2019-02-05 17:42:34 +00:00
|
|
|
$result = $wpdb->replace(
|
2018-12-27 03:49:00 +00:00
|
|
|
$wpdb->prefix . self::TABLE_NAME,
|
|
|
|
array(
|
|
|
|
'order_id' => $order_id,
|
2019-02-01 17:43:24 +00:00
|
|
|
'coupon_id' => $coupon_id,
|
2018-12-27 03:49:00 +00:00
|
|
|
'discount_amount' => $coupon_item->get_discount(),
|
2019-02-08 17:01:44 +00:00
|
|
|
'date_created' => $order->get_date_created( 'edit' )->date( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
2018-12-27 03:49:00 +00:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
'%d',
|
|
|
|
'%d',
|
|
|
|
'%f',
|
|
|
|
'%s',
|
|
|
|
)
|
|
|
|
);
|
2019-02-01 17:43:24 +00:00
|
|
|
|
2019-02-01 19:02:15 +00:00
|
|
|
/**
|
|
|
|
* Fires when coupon's reports are updated.
|
|
|
|
*
|
|
|
|
* @param int $coupon_id Coupon ID.
|
|
|
|
* @param int $order_id Order ID.
|
|
|
|
*/
|
2019-02-04 17:58:37 +00:00
|
|
|
do_action( 'woocommerce_reports_update_coupon', $coupon_id, $order_id );
|
2019-02-05 17:42:34 +00:00
|
|
|
|
2019-01-29 21:36:02 +00:00
|
|
|
$num_updated += intval( $result );
|
2018-12-27 03:49:00 +00:00
|
|
|
}
|
2019-01-29 21:36:02 +00:00
|
|
|
|
|
|
|
return ( count( $coupon_items ) === $num_updated );
|
2018-12-27 03:49:00 +00:00
|
|
|
}
|
|
|
|
|
2019-01-29 21:13:06 +00:00
|
|
|
/**
|
|
|
|
* Clean coupons data when an order is deleted.
|
|
|
|
*
|
|
|
|
* @param int $order_id Order ID.
|
|
|
|
*/
|
|
|
|
public static function sync_on_order_delete( $order_id ) {
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
$table_name = $wpdb->prefix . self::TABLE_NAME;
|
|
|
|
|
|
|
|
$wpdb->query(
|
|
|
|
$wpdb->prepare(
|
|
|
|
"DELETE FROM ${table_name} WHERE order_id = %d",
|
|
|
|
$order_id
|
|
|
|
)
|
|
|
|
);
|
2019-02-01 18:29:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fires when coupon's reports are removed from database.
|
|
|
|
*
|
|
|
|
* @param int $coupon_id Coupon ID.
|
|
|
|
* @param int $order_id Order ID.
|
|
|
|
*/
|
2019-02-04 18:02:49 +00:00
|
|
|
do_action( 'woocommerce_reports_delete_coupon', 0, $order_id );
|
2019-01-29 21:13:06 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 12:16:46 +00:00
|
|
|
}
|