Automattic\WooCommerce\Internal\DataStores\Orders
OrdersTableQuery::generate_total_query
Generate SQL conditions for the 'total' query with operators.
Method of the class: OrdersTableQuery{}
No Hooks.
Returns
String. SQL to be used in a WHERE clause.
Usage
// private - for code of main (parent) class only $result = $this->generate_total_query( $total_params ): string;
- $total_params(array) (required)
- Total query parameters with value, operator.
OrdersTableQuery::generate_total_query() OrdersTableQuery::generate total query code WC 10.3.6
private function generate_total_query( array $total_params ): string {
if ( ! isset( $total_params['value'] ) ) {
return '';
}
$operator = $total_params['operator'] ?? '=';
$value = $total_params['value'];
$supported_operators = array( '=', '!=', '>', '>=', '<', '<=', 'BETWEEN', 'NOT BETWEEN' );
if ( ! in_array( $operator, $supported_operators, true ) ) {
return '';
}
// Handle between operators.
if ( 'BETWEEN' === $operator || 'NOT BETWEEN' === $operator ) {
if ( ! is_array( $value ) || count( $value ) !== 2 ) {
return '';
}
$value1 = wc_format_decimal( $value[0], wc_get_price_decimals() );
$value2 = wc_format_decimal( $value[1], wc_get_price_decimals() );
if ( 'BETWEEN' === $operator ) {
return $this->where( $this->tables['orders'], 'total_amount', '>=', $value1, 'decimal' ) . ' AND ' . $this->where( $this->tables['orders'], 'total_amount', '<=', $value2, 'decimal' );
} else {
return '(' . $this->where( $this->tables['orders'], 'total_amount', '<', $value1, 'decimal' ) . ' OR ' . $this->where( $this->tables['orders'], 'total_amount', '>', $value2, 'decimal' ) . ')';
}
}
// Handle other operators - value must be a single number.
if ( ! is_numeric( $value ) ) {
return '';
}
return $this->where( $this->tables['orders'], 'total_amount', $operator, wc_format_decimal( $value, wc_get_price_decimals() ), 'decimal' );
}