Besart
Besart

Reputation: 399

How to get quantity of a WooCommerce subscription?

I am currently developing a WordPress project and I am using WooCommerce with WooCommerce Subscriptions plugin to offer subscriptions to my users. I need help on how to get the quantity of a subscription in PHP.

I am using this code to get subscription but I can not retrieve quantity:

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

When a user purchases a subscription, the user can select the quantity of subscriptions. So I need to get the value of this field:

enter image description here

Upvotes: 1

Views: 1605

Answers (2)

LoicTheAztec
LoicTheAztec

Reputation: 253784

Your code is correct and wcs_get_subscriptions() is the right and best way to get customer active subscriptions.

But you have missed a something after your code to get the customer subscription item quantity (code commented):

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

Tested and works.

Upvotes: 1

rajat.gite
rajat.gite

Reputation: 446

Here is my working code try this

$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"
) );

And if you want get all post_status subscription then use this


$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"
) );

Thanks

Upvotes: 0

Related Questions