awesomemotive / easy-digital-downloads

Sell digital downloads through WordPress
https://easydigitaldownloads.com
GNU General Public License v2.0
863 stars 474 forks source link

Database error when performing an Order query containing a date_query and a product_id #9699

Open jkudish opened 7 months ago

jkudish commented 7 months ago

Bug Report

Expected behavior

Performing a Order query containing a date_query and a product_id should return results.

This bug is similar to the one reported in #7920 and fixed in #7951. However the solution that was applied there was specific to the exports page (which performs a custom SQL query) and did not fix the issue when performing a query using the EDD\Database\Queries\Order query object.

Actual behavior

Performing a Order query containing a date_query and a product_id produces the following database error: WordPress database error Column 'date_created' in where clause is ambiguous for query SELECT...

Steps to reproduce the behavior

Perform a query similar to this:

$query = array(
    'product_id' => 1
    'date_query' => array(
        array(
            'after'     => '2023-11-01 00:00:00',
            'before'    => '',
            'inclusive' => true,
        ),
    ),
);

// this will produce a database error:
// WordPress database error Column 'date_created' in where clause is ambiguous for query SELECT ... 
// this is due to the JOIN on the order items table
$orders = edd_get_orders( $query );

For ease of reproduction I've made a minimum viable reproduction plugin in the form of a WP_CLI command availalble here: https://gist.github.com/jkudish/e5cd9254abfa4e4903dd837c72513c8f

Information (if a specific version is affected):

PHP Version: any EDD Version (or branch): 3.0+ WordPress Version: any

I have submitted #9700 as an attempted fix for this bug