WooCommerce Subscriptions - Get product of a specific subscription

前端 未结 3 1655
南笙
南笙 2021-01-19 19:02

Is there a way to get from $product from $subscription?

Thanks to this post, I know that from $subscription I can get $o

3条回答
  •  无人及你
    2021-01-19 19:27

    Yes, if you have a "subscription ID" you can get an object like this:

    $subscription = new WC_Subscription( $the_id );
    

    When you already have $subscription object:

    $related_orders_ids_array = $subscription->get_related_orders();
    

    Then loop through array of related orders.

    foreach ( $related_orders_ids_array as $order_id ) {
    
        $order = new WC_Order( $order_id );
        $items = $order->get_items();
    

    then if you loop through order items, you can access product information:

        foreach ( $items as $product ) {
    
            var_dump( $product );
    
        }
    
    }
    

提交回复
热议问题