Home > Blockchain >  WooCommerce v6.4.1 - How do I get product id/name associated with a subscription id?
WooCommerce v6.4.1 - How do I get product id/name associated with a subscription id?

Time:08-22

We operate a subscription service with three subscription levels. I'll call them Silver, Gold, and Platinum. The subscriptions are associated with variable products. So, you can buy a Silver subscription at $11-a-month, $22-a-month, $44-a-month, and up it goes to $1000-a-year. These all have descriptive names like "Six Month Gold Member", etc.

Before posting this question, I've tried many, many answers I've found to very similar questions, but I think they must not work any more with WC 6.n. Or, maybe it has something to do with our products or settings or environment.

My flow looks like this:

  • Query WC for this user's active subscriptions. (this part works)
  • Use resulting subscription ids to query WC for the product they purchased. (help!)
  • Compare the user's role to their purchased products. The three general products, Silver, Gold, and Platinum are ranked in order of priority, and I want to match the active subscription of some product name to the user's current role, to safeguard against inadvertent role changes caused by other plugins (I'm looking at you, Ultimate Member - WooCommerce integration!).

It's that second step I'm stuck on.

The code I use to get the current user's active subscriptions works great:

$subscriptions = wcs_get_subscriptions( //query WC for this user's active subscriptions.
        array(
            'customer_id' => $user_id,
            'subscription_status' => 'active',
            'subscriptions_per_page' => - 1
        )
);

// iterate through each subscription to extract the subscription id.
foreach($subscriptions as $subscription){ 
    $subscription_id = $subscription->get_id();
    echo $subscription_id . "<br>";
    $subscription_products = $subscription->get_items();
    $order_key = $subscription->get_order_key(); // I do get an order_key
    $order_id = $subscription->get_parent_id(); // I do get an order_id
    //echo $order_id . "<br>"; // 
    $order = new WC_Order( $subscription_id ); // No errors here.

    // This doesn't produce anything.
    foreach($order->get_items() as $item) {
        $product_name = $item['name'];
        echo $product_name . "<br>";
    }
    //print_r($order);
}

I've scoured SO and elsewhere for a method to retrieve the product name from a member's subscription. Even Subscription Details on the My Account page never show exactly what package I bought.

A few answers to my question can be found on the forum dating up to 2017, but I think WC has made quite a few changes, with deprecated functions that no longer work.

If I try this:

$subscription_products = $subscription->get_items();
$order_key = $subscription->get_order_key();
$order_id = $subscription->get_parent_id();
echo $order_id . "<br>";
$order = new WC_Order( $subscription_id );
        
print_r($order->get_items()); // returns an empty array

...it returns an empty array.

Finally, I notice that in the actual WP->WooCommerce->Subscriptions->Edit a subscription panel, I get the same information as my code does, and nowhere in the WooCommerce Subscriptions dashboard does it show the actual product name a user bought.

CodePudding user response:

Here's the code that should work for you:

$subscriptions = wcs_get_subscriptions(
        array(
            'customer_id' => $user_id,
            'subscription_status' => 'active',
            'subscriptions_per_page' => - 1
        )
);

// iterate through each subscription to extract the subscription id.
foreach ($subscriptions as $subscription) { 
    $related_orders_ids_array = $subscription->get_related_orders();

    foreach ( $related_orders_ids_array as $order_id ) {

        $order = new WC_Order( $order_id );
        //$items = $order->get_items(); 
        $items = $order->get_items( apply_filters( 'woocommerce_purchase_order_item_types', 'line_item' ) );

        //This will get items in order
        foreach ( $items as $product ) {
            print_r( $product ); //this will give you product object
        }
    }
}

Let me know if this works for you or you face any issues with this code.

  • Related