Automattic / woocommerce-subscriptions-core

Subscriptions core package for WooCommerce
Other
81 stars 29 forks source link

[HPOS] Fix wcs_get_subscription_orders() returning empty list when querying parent orders #474

Closed mattallan closed 11 months ago

mattallan commented 11 months ago

Fixes #422

Description

With HPOS enabled & data synchronization disabled, calling wcs_get_subscription_orders( 'ids', 'parent' ) will return an empty array due to this function querying for post_parent from the posts table.

In this PR I'm replacing the following query with code that alters the query based on whether HPOS is enabled.

$order_ids = array_merge( $order_ids, $wpdb->get_col(
    "SELECT DISTINCT post_parent FROM {$wpdb->posts}
     WHERE post_type = 'shop_subscription'
     AND post_parent <> 0"
) );

Note - This function not working as expected doesn't impact WC Subscriptions as we don't use this with HPOS enabled.

How to test this PR

  1. Enable HPOS on your store and turn off data syncing
  2. Purchase a subscription
  3. With WP Console run the following snippet:
    echo var_export( wcs_get_subscription_orders( 'ids', 'parent' ), true );
  4. While on trunk, this will return an empty array.
  5. On this branch, the query will return all subscription parent orders.

Product impact