Avoing string to int comparison and add the id clause only when available (#40030)

This commit is contained in:
Vedanshu Jain 2023-09-05 12:47:17 +05:30 committed by GitHub
commit 0f7e6898db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 2 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Avoid string<>int comparison in products bought query to avoid results with customer_id = 0.

View File

@ -362,6 +362,10 @@ function wc_customer_bought_product( $customer_email, $user_id, $product_id ) {
$statuses
);
$order_table = OrdersTableDataStore::get_orders_table_name();
$user_id_clause = '';
if ( $user_id ) {
$user_id_clause = 'OR o.customer_id = ' . absint( $user_id );
}
$sql = "
SELECT im.meta_value FROM $order_table AS o
INNER JOIN {$wpdb->prefix}woocommerce_order_items AS i ON o.id = i.order_id
@ -369,8 +373,7 @@ INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS im ON i.order_item_id =
WHERE o.status IN ('" . implode( "','", $statuses ) . "')
AND im.meta_key IN ('_product_id', '_variation_id' )
AND im.meta_value != 0
AND ( o.customer_id IN ('" . implode( "','", $customer_data ) . "') OR o.billing_email IN ('" . implode( "','", $customer_data ) . "') )
AND ( o.billing_email IN ('" . implode( "','", $customer_data ) . "') $user_id_clause )
";
$result = $wpdb->get_col( $sql );
} else {

View File

@ -51,6 +51,10 @@ class WC_User_Functions_Tests extends WC_Unit_Test_Case {
$order_3->set_billing_email( 'test@example.com' );
$order_3->set_status( 'pending' );
$order_3->save();
$order_4 = wc_create_order();
$order_4->add_product( $product_1 );
$order_4->set_status( 'completed' );
$order_4->save();
$this->assertTrue( wc_customer_bought_product( 'test@example.com', $customer_id_1, $product_id_1 ) );
$this->assertTrue( wc_customer_bought_product( '', $customer_id_1, $product_id_1 ) );