Reputation: 11
Trying to use the wcs_get_subscriptions
function to retrieve subscription information to create printable labels.
I have the plugin passing a comma delimited list of order id's within the querystring into the script, but I'm not sure how to pass the id string into the function.
$subscriptions = wcs_get_subscriptions(array( 'subscriptions_per_page' => -1,
'subscription_status' => array('active'),
'post_id' => array(123,456,789) ));
foreach($subscriptions as $sub){
echo $sub->get_shipping_city();
}
Upvotes: 1
Views: 3856
Reputation: 1125
In short, you can't with the wcs_get_subscriptions
function:
The wcs_get_subscriptions
function unfortunately does not currently allow an array for the order_id
parameter. Looking at the source for the function, it only takes a single numeric value ("The post ID of a shop_order post/WC_Order object which was used to create the subscription"), then uses that as the post_parent
in a get_posts
call that returns a list of IDs; it then runs wcs_get_subscription on each one to create the final array that's returned. It's somewhat limited in not allowing all of the arguments that get_posts does.
Source for the wcs_get_subscriptions
function is available here:
https://github.com/wp-premium/woocommerce-subscriptions/blob/master/wcs-functions.php
Alternative solution to do what you need:
You can use get_posts, matching other similar arguments that wcs_get_subscriptions
uses, with the post_parent__in
parameter:
'post_parent__in' (array) An array containing parent page IDs to query child pages from.
Here's an example:
/**
* Get an array of WooCommerce subscriptions in form of post_id => WC_Subscription.
* Basically returns what wcs_get_subcriptions does, but allows supplying
* additional arguments to get_posts.
* @param array $get_post_args Additional arguments for get_posts function in WordPress
* @return array Subscription details in post_id => WC_Subscription form.
*/
function get_wcs_subscription_posts($get_post_args){
// Find array of post IDs for WooCommerce Subscriptions.
$get_post_args = wp_parse_args( $get_post_args, array(
'post_type' => 'shop_subscription',
'post_status' => array('active'),
'posts_per_page' => -1,
'order' => 'DESC',
'fields' => 'ids',
'orderby' => 'date'
));
$subscription_post_ids = get_posts( $get_post_args );
// Create array of subscriptions in form of post_id => WC_Subscription
$subscriptions = array();
foreach ( $subscription_post_ids as $post_id ) {
$subscriptions[ $post_id ] = wcs_get_subscription( $post_id );
}
return $subscriptions;
}
get_wcs_subscription_posts(array(
'post_parent__in' => array(123, 456, 789)
));
If you had the Subscription IDs rather than the Order IDs, you could also use post__in
. Hope that helps.
Upvotes: 2