Automattic\WooCommerce\Internal\DataStores\Orders
OrdersTableQuery::generate_customer_query
Generate SQL conditions for the 'customer' query.
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_customer_query( $values, $relation ): string;
- $values(array) (required)
- List of customer ids or emails.
- $relation(string)
- 'OR' or 'AND' relation used to build the customer query.
Default: 'OR'
OrdersTableQuery::generate_customer_query() OrdersTableQuery::generate customer query code WC 10.3.6
private function generate_customer_query( $values, string $relation = 'OR' ): string {
$values = is_array( $values ) ? $values : array( $values );
$ids = array();
$emails = array();
$pieces = array();
foreach ( $values as $value ) {
if ( is_array( $value ) ) {
$sql = $this->generate_customer_query( $value, 'AND' );
$pieces[] = $sql ? '(' . $sql . ')' : '';
} elseif ( is_numeric( $value ) ) {
$ids[] = absint( $value );
} elseif ( is_string( $value ) && is_email( $value ) ) {
$emails[] = sanitize_email( $value );
} else {
// Invalid query.
$pieces[] = '1=0';
}
}
if ( $ids ) {
$pieces[] = $this->where( $this->tables['orders'], 'customer_id', '=', $ids, 'int' );
}
if ( $emails ) {
$pieces[] = $this->where( $this->tables['orders'], 'billing_email', '=', $emails, 'string' );
}
return $pieces ? implode( " $relation ", $pieces ) : '';
}