Automattic / woocommerce-subscriptions-core

Subscriptions core package for WooCommerce
Other
87 stars 33 forks source link

4.7.0: HPOS `wcs_get_orders_with_meta_query()` busted `wcs_get_subscriptions()` with a `meta_query` for non-HPOS stores #346

Closed lkraav closed 1 year ago

lkraav commented 1 year ago

Describe the bug

After 4.7.0, one of our queries got busted:

        $subscription_query_args = [
            'customer_id'            => $user_id,
            'subscription_status'    => $status,
            'subscriptions_per_page' => -1,
            // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
            'meta_query'             => [
                [
                    'key'     => '_schedule_trial_end',
                    'compare' => '!=',
                    'value'   => 0,
                ],
            ],
        ];

        return wcs_get_subscriptions( $subscription_query_args );

Problem is likely the combination with customer_id, and whatever else \WC_Order_Data_Store_CPT::get_wp_query_args() might map into meta_query.

In #171 a new meta filter callback was introduced where it just clobbers any WP_Query converted meta with original $subscription_query_args['meta_query']

https://github.com/Automattic/woocommerce-subscriptions-core/blob/5.1.0/includes/wcs-order-functions.php#L384-L399 with `$query['meta_query'] = $meta;

To Reproduce

Run wcs_get_subscriptions() with customer_id and a meta_query.

Expected behavior

Meta_query must not get clobbered.

Actual behavior

Meta_query is clobbered.

Product impact

Additional context

Seems to get fixed by array_merge() instead of assignment but is that correct?

// $query['meta_query'] = $meta;
$query['meta_query'] = array_merge( $query['meta_query'], $meta );