尝试使用wcs_get_subscriptions
函数检索订阅信息以创建可打印的标签。
我让插件将查询字符串中以逗号分隔的订单 id 列表传递到脚本中,但我不确定如何将 id 字符串传递到函数中。
$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();
}
简而言之,你不能使用wcs_get_subscriptions
函数:
不幸的是,wcs_get_subscriptions
函数当前不允许order_id
参数的数组。查看函数的源代码,它只需要一个数值(">用于创建订阅的shop_order post/WC_Order 对象的 postID"(,然后将其用作返回 ID 列表的get_posts
调用中的post_parent
;然后,它会在每个数组上运行wcs_get_subscription以创建返回的最终数组。它在某种程度上限制了不允许get_posts所做的所有论点。
wcs_get_subscriptions
函数的来源可在此处获得:
https://github.com/wp-premium/woocommerce-subscriptions/blob/master/wcs-functions.php
满足您需求的替代解决方案:
您可以使用 get_posts,匹配wcs_get_subscriptions
使用的其他类似参数,其中包含post_parent__in
参数:
"post_parent__in"(数组(包含要查询的父页面 ID 的数组 子页面来自。
下面是一个示例:
/**
* 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)
));
如果您有订阅 ID 而不是订单 ID,则还可以使用post__in
。希望有帮助。