Get Subscription Product author of a Woocommerce subscription

北战南征 提交于 2019-12-24 01:16:30

问题


How can I iterate through all current active woo subscriptions and print the user ID of the user who published the product related to each active subscription (PHP)? I think something like this will give just the subscriptions:

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

回答1:


The following code will query all active subscriptions to get the author Id (that published the product of the active subscription):

// 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>';
    }
}

Tested and works.



来源:https://stackoverflow.com/questions/55054276/get-subscription-product-author-of-a-woocommerce-subscription

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!