【问题标题】:How to get quantity of a WooCommerce subscription?如何获得 WooCommerce 订阅的数量?
【发布时间】:2020-06-01 08:44:27
【问题描述】:

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

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

$subscriptions = wcs_get_subscriptions( array(
    'customer_id'            => get_current_user_id(),
    'subscription_status'    => 'wc-active',
    'order_by'               => 'DESC',
    'subscriptions_per_page' => - 1
) );

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

【问题讨论】:

    标签: php wordpress woocommerce woocommerce-subscriptions product-quantity


    【解决方案1】:

    您的代码是正确的,wcs_get_subscriptions() 是让客户积极订阅的正确和最佳方式。

    但是你的代码获取客户订阅项目数量之后你错过了一些东西 (代码注释)

    // Get current customer active subscriptions
    $subscriptions = wcs_get_subscriptions( array(
        'customer_id'            => get_current_user_id(),
        'subscription_status'    => 'wc-active',
        'order_by'               => 'DESC',
        'subscriptions_per_page' => - 1
    ) );
    
    if ( count( $subscriptions ) > 0 ) {
        // Loop through customer subscriptions
        foreach ( $subscriptions as $subscription ) {
            // Get the initial WC_Order object instance from the subscription
            $order = wc_get_order( $subscription->get_parent_id() );
    
            // Loop through order items
            foreach ( $order->get_items() as $item ) {
                $product = $item->get_product(); // Get the product object instance
    
                // Target only subscriptions products type
                if( in_array( $product->get_type(), ['subscription', 'subscription_variation'] ) ) {
                    $quantity = $item->get_quantity(); // Get the quantity
                    echo '<p>Quantity: ' . $quantity . '</p>';
                }
            }
        }
    }
    

    经过测试并且有效。

    【讨论】:

      【解决方案2】:

      这是我的工作代码试试这个

      $current_user_id = get_current_user_id();
      $customer_subscriptions = get_posts( array(
          'numberposts' => -1,
          'meta_key'    => '_customer_user',
          'meta_value'  => get_current_user_id(), // Or $user_id
          'post_type'   => 'shop_subscription', // WC orders post type
          'post_status' => 'wc-active' // Only orders with status "completed"
      ) );
      

      如果您想获得所有 post_status 订阅,请使用此

      
      $customer_subscriptions_for_other_cases = get_posts( array(
          'numberposts' => -1,
          'meta_key'    => '_customer_user',
          'meta_value'  => get_current_user_id(), // Or $user_id
          'post_type'   => 'shop_subscription', // WC orders post type
          'post_status' => array('wc-on-hold','wc-pending-cancel','wc-active') // Only orders with status "completed"
      ) );
      

      谢谢

      【讨论】:

      • @LoicTheAztec 这就是我需要的。
      猜你喜欢
      • 2015-11-27
      • 2015-09-27
      • 1970-01-01
      • 2014-05-24
      • 2017-04-07
      • 2017-12-03
      • 2019-07-29
      • 1970-01-01
      • 2017-08-05
      相关资源
      最近更新 更多