WC_Order_Data_Store_CPT::generate_total_queryprivateWC 1.0

Generate meta query for total filtering with operators.

Method of the class: WC_Order_Data_Store_CPT{}

No Hooks.

Returns

Array|false. Meta query array or false if invalid.

Usage

// private - for code of main (parent) class only
$result = $this->generate_total_query( $total_params );
$total_params(array) (required)
Total query parameters with value, operator.

WC_Order_Data_Store_CPT::generate_total_query() code WC 10.3.3

private function generate_total_query( array $total_params ) {
	if ( ! isset( $total_params['value'] ) ) {
		return false;
	}

	$operator            = $total_params['operator'] ?? '=';
	$value               = $total_params['value'];
	$supported_operators = array( '=', '!=', '>', '>=', '<', '<=', 'BETWEEN', 'NOT BETWEEN' );

	if ( ! in_array( $operator, $supported_operators, true ) ) {
		return false;
	}

	// Handle between operators.
	if ( 'BETWEEN' === $operator || 'NOT BETWEEN' === $operator ) {
		if ( ! is_array( $value ) || count( $value ) !== 2 ) {
			return false;
		}
		$value1 = wc_format_decimal( $value[0], wc_get_price_decimals() );
		$value2 = wc_format_decimal( $value[1], wc_get_price_decimals() );

		if ( 'BETWEEN' === $operator ) {
			return array(
				array(
					'key'     => '_order_total',
					'value'   => array( $value1, $value2 ),
					'compare' => 'BETWEEN',
					'type'    => 'DECIMAL(10,' . wc_get_price_decimals() . ')',
				),
			);
		} else {
			return array(
				array(
					'key'     => '_order_total',
					'value'   => array( $value1, $value2 ),
					'compare' => 'NOT BETWEEN',
					'type'    => 'DECIMAL(10,' . wc_get_price_decimals() . ')',
				),
			);
		}
	}

	// Handle other operators - value must be a single number.
	if ( ! is_numeric( $value ) ) {
		return false;
	}

	return array(
		'key'     => '_order_total',
		'value'   => wc_format_decimal( $value, wc_get_price_decimals() ),
		'compare' => $operator,
		'type'    => '=' === $operator ? 'CHAR' : 'DECIMAL(10,' . wc_get_price_decimals() . ')',
	);
}