WooCommerce Subscriptions - Get a Product of a Specific Subscription

Is there any way to get from $productfrom $subscription?

Thanks to this post , I know from which $subscriptionI can get $order:

$order = $subscription->order;

Is it possible?

$product = $subscription->product;

Or:

$product = $order->product;
0
source share
2 answers

You can get products either from a subscription, or using the appropriate order, since the subscription object is an extension of the class WC_Order.

$subscription_products = $subscription->get_items();
$order_products = $subscription->order->get_items();

In most situations when you provide subscription services, both of them will be equivalent or $order_productsshould contain at least all the products found in $subscription_products.

; , $subscription->order WC_Order.

+1

, " ", :

$subscription = new WC_Subscription( $the_id );

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

, , :

    foreach ( $items as $product ) {

        var_dump( $product );

    }

}
0

Source: https://habr.com/ru/post/1672276/


All Articles