如何获取Woo商务订阅的数量?

我目前正在开发一个WordPress项目,我正在使用Woo商务与Woo商业订阅插件为我的用户提供订阅。我需要有关如何在PHP中获取订阅数量的帮助。


我正在使用此代码获取订阅,但我无法检索数量:


$subscriptions = wcs_get_subscriptions( array(

    'customer_id'            => get_current_user_id(),

    'subscription_status'    => 'wc-active',

    'order_by'               => 'DESC',

    'subscriptions_per_page' => - 1

) );

当用户购买订阅时,用户可以选择订阅的数量。所以我需要得到这个字段的值:

http://img.mukewang.com/632eba5c000149cd05760240.jpg

交互式爱情
浏览 72回答 2
2回答

GCT1015

您的代码是正确的,是获得客户活跃订阅的正确和最佳方式。wcs_get_subscriptions()但是您在代码后错过了一些东西来获取客户订阅项目数量(代码注释)::// Get current customer active subscriptions$subscriptions = wcs_get_subscriptions( array(&nbsp; &nbsp; 'customer_id'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => get_current_user_id(),&nbsp; &nbsp; 'subscription_status'&nbsp; &nbsp; => 'wc-active',&nbsp; &nbsp; 'order_by'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=> 'DESC',&nbsp; &nbsp; 'subscriptions_per_page' => - 1) );if ( count( $subscriptions ) > 0 ) {&nbsp; &nbsp; // Loop through customer subscriptions&nbsp; &nbsp; foreach ( $subscriptions as $subscription ) {&nbsp; &nbsp; &nbsp; &nbsp; // Get the initial WC_Order object instance from the subscription&nbsp; &nbsp; &nbsp; &nbsp; $order = wc_get_order( $subscription->get_parent_id() );&nbsp; &nbsp; &nbsp; &nbsp; // Loop through order items&nbsp; &nbsp; &nbsp; &nbsp; foreach ( $order->get_items() as $item ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $product = $item->get_product(); // Get the product object instance&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Target only subscriptions products type&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( in_array( $product->get_type(), ['subscription', 'subscription_variation'] ) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $quantity = $item->get_quantity(); // Get the quantity&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<p>Quantity: ' . $quantity . '</p>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}经过测试并工作。

郎朗坤

这是我的工作代码试试这个$current_user_id = get_current_user_id();$customer_subscriptions = get_posts( array(&nbsp; &nbsp; 'numberposts' => -1,&nbsp; &nbsp; 'meta_key'&nbsp; &nbsp; => '_customer_user',&nbsp; &nbsp; 'meta_value'&nbsp; => get_current_user_id(), // Or $user_id&nbsp; &nbsp; 'post_type'&nbsp; &nbsp;=> 'shop_subscription', // WC orders post type&nbsp; &nbsp; 'post_status' => 'wc-active' // Only orders with status "completed") );如果您想获得所有post_status订阅,请使用此$customer_subscriptions_for_other_cases = get_posts( array(&nbsp; &nbsp; 'numberposts' => -1,&nbsp; &nbsp; 'meta_key'&nbsp; &nbsp; => '_customer_user',&nbsp; &nbsp; 'meta_value'&nbsp; => get_current_user_id(), // Or $user_id&nbsp; &nbsp; 'post_type'&nbsp; &nbsp;=> 'shop_subscription', // WC orders post type&nbsp; &nbsp; 'post_status' => array('wc-on-hold','wc-pending-cancel','wc-active') // Only orders with status "completed") );
打开App,查看更多内容
随时随地看视频慕课网APP