Skip to content
This repository has been archived by the owner on Jun 15, 2022. It is now read-only.

Calling wc_get_orders with a custom query parameter causes a DB error #125

Closed
bswatson opened this issue Jul 12, 2019 · 0 comments · Fixed by #126
Closed

Calling wc_get_orders with a custom query parameter causes a DB error #125

bswatson opened this issue Jul 12, 2019 · 0 comments · Fixed by #126
Assignees
Labels
Milestone

Comments

@bswatson
Copy link
Contributor

Describe the bug
An error is thrown when wc_get_orders is called with a custom query parameter which uses the woocommerce_order_data_store_cpt_get_orders_query filter to define additional meta_query` items.

To Reproduce
Steps to reproduce the behavior:

  1. Create an order and assign meta data to it.
$order->update_meta_data( '_custom_meta_key', 'value' );
  1. Create a filter to handle the incoming query var:
add_filter('woocommerce_order_data_store_cpt_get_orders_query', function ( $query, $query_vars ) {
	if ( ! empty( $query_vars['_custom_meta_key'] ) ) {
		$query['meta_query'][] = array(
			'key' => '_custom_meta_key',
			'value' => esc_attr( $query_vars['_custom_meta_key'] ),
		);
	}

	return $query;
}, 100, 2);
  1. Execute wc_get_orders with the custom query var.
$orders = wc_get_orders( array( '_custom_meta_key' => 'value' ) );
  1. Error is thrown and logged.
WordPress database error Unknown column 'wptests_postmeta.meta_key' in 'where clause' for query SELECT   wptests_posts.ID FROM wptests_posts  LEFT JOIN wptests_woocommerce_orders ON ( wptests_posts.ID = wptests_woocommerce_orders.order_id )  WHERE 1=1  AND ( 
  ( wptests_postmeta.meta_key = '_custom_meta_key' AND wptests_postmeta.meta_value = 'value' )
) AND wptests_posts.post_type IN ('shop_order', 'shop_order_refund') AND ((wptests_posts.post_status = 'wc-pending' OR wptests_posts.post_status = 'wc-processing' OR wptests_posts.post_status = 'wc-on-hold' OR wptests_posts.post_status = 'wc-completed' OR wptests_posts.post_status = 'wc-cancelled' OR wptests_posts.post_status = 'wc-refunded' OR wptests_posts.post_status = 'wc-failed')) GROUP BY wptests_posts.ID ORDER BY wptests_posts.post_date DESC LIMIT 0, 10

Expected behavior
$orders returns as an array with the expected order.

Versions

  • WordPress version: 5.2.2
  • WooCommerce version: 3.6.5
  • Plugin version: develop @ 587ce29
  • PHP version: 7.2

Additional context
Add any other context about the problem here.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant