获取Woocommerce订阅的订阅产品作者

时间:2019-03-07 23:10:05

标签: php wordpress woocommerce author woocommerce-subscriptions

如何遍历所有当前的有效woo订阅并打印发布与每个有效的订阅(PHP)相关的产品的用户的用户ID?我认为类似这样的东西只会提供订阅:

$args = array( 'subscriptions_per_page' => -1, 'post_type'   => 'shop_subscription', // WC orders post type
                'post_status' => 'wc-active' );
            $subscriptions = wcs_get_subscriptions( $args );

1 个答案:

答案 0 :(得分:0)

以下代码将查询所有活动订阅以获取作者ID(发布了该活动订阅的产品):

// Get all active subscriptions
$subscriptions = wcs_get_subscriptions( array(
    'subscriptions_per_page' => -1,
    'subscription_status' => array('active') // Active subscriptions
) );

// 1) Loop through quieried active subscriptions
foreach($subscriptions as $subscription)
{
    // 2) Loop through subscription items
    foreach( $subscription->get_items() as $item_id => $item ) 
    {
        // Get the subscription product author
        $author_id = get_post_field ('post_author', $item->get_product_id());
        // Display
        echo $author_id . '<br>';
    }
}

经过测试可以正常工作。