Back to Woocommerce

Data

plugins/woocommerce/client/admin/docs/data.md

10.8.12.8 KB
Original Source

Data

WooCommerce Admin data stores implement the SqlQuery class.

SqlQuery Class

The SqlQuery class is a SQL Query statement object. Its properties consist of

  • A context string identifying the context of the query.
  • SQL clause (type) string arrays used to construct the SQL statement:
    • select
    • from
    • right_join
    • join
    • left_join
    • where
    • where_time
    • group_by
    • having
    • order_by
    • limit

Reports Data Stores

The base DataStore Automattic\WooCommerce\Admin\API\Reports\DataStore extends the SqlQuery class. There is StatsDataStoreTrait that adds Interval & Total Queries. The implementation data store classes use the following SqlQuery instances:

Data StoreContextClass QuerySub QueryInterval QueryTotal Query
CategoriescategoriesYesYes--
CouponscouponsYesYes--
Coupon Statscoupon_statsYes-YesYes
CustomerscustomersYesYes--
Customer Statscustomer_statsYes-YesYes
DownloadsdownloadsYesYes--
Download Statsdownload_statsYes-YesYes
OrdersordersYesYes--
Order Statsorder_statsYes-YesYes
ProductsproductsYesYes--
Product Statsproduct_statsYes-YesYes
TaxestaxesYesYes--
Tax Statstax_statsYes-YesYes
VariationsvariationsYesYes--
StatsDataStoreTraitn/an/a-YesYes

Query contexts are named as follows:

  • Class Query = Class Context
  • Sub Query = Class Context + _subquery
  • Interval Query = Class Context + _interval
  • Total Query = Class Context + _total

Filters

When getting the full statement the clause arrays are passed through two filters where $context is the query object context and $type is:

  • select
  • from
  • join = right_join + join + left_join
  • where = where + where_time
  • group_by
  • having
  • order_by
  • limit

The filters are:

  • apply_filters( "woocommerce_analytics_clauses_{$type}", $clauses, $context );
  • apply_filters( "woocommerce_analytics_clauses_{$type}_{$context}", $clauses );

Example usage

php
add_filter( 'woocommerce_analytics_clauses_product_stats_select_total', 'my_custom_product_stats' );
/**
 * Add sample data to product stats totals.
 *
 * @param array $clauses array of SELECT clauses.
 * @return array
 */
function my_custom_product_stats( $clauses ) {
	$clauses[] = ', SUM( sample_column ) as sample_total';
	return $clauses;
}