【问题标题】:WooCommerce Subscriptions - Get related orders Ids for a specific subscriptionWooCommerce 订阅 - 获取特定订阅的相关订单 ID
【发布时间】:2017-08-05 02:09:57
【问题描述】:

是否有 woocommerce 功能可以为我返回用户拥有的特定订阅的所有相关订单(至少是订单 ID)?

我在这个官方文档中找到了Subscription Function & Property Reference

WC_Subscription::get_related_orders( $return_fields, $order_type );

但这似乎不是针对特定订阅的?

每当我尝试运行它时,我都会收到一个致命错误,无论我传入什么:

致命错误:未捕获的错误:当不在对象上下文中时使用 $this C:\xampp\htdocs\mysite.com\wp-content\plugins\woocommerce-subscriptions\includes\class-wc-subscription.php:1413

我正在制作自己的插件,并从帖子表中选择post status = wc-active 的所有订阅。我查看了“woocommerce_order_items”、“woocommerce_order_itemmeta”和“postmeta”表,但它们都没有提供获取用户购买订阅相关订单的方法...

如果我只知道用户购买的订阅及其相关订单的关系在哪里,那么我可以写一些 sql 但我不知道,google 也不会产生任何结果。

有什么想法吗?

我的设置:

  • php版本7.0.4
  • wordpress 4.7.3 版
  • woocommerce 2.6.8
  • woocommerce 订阅:2.0.18

【问题讨论】:

    标签: php wordpress woocommerce orders woocommerce-subscriptions


    【解决方案1】:

    更新: 添加了 WooCommerce 版本 3+ 兼容性

    从订阅对象中获取订单 ID 非常容易。我将像您一样选择所有订阅,其中 'post status' = 'wc-active' 从帖子表中。

    // Get all customers subscriptions
    $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"
    ) );
    
    // Iterating through each post subscription object
    foreach( $customer_subscriptions as $customer_subscription ){
        // The subscription ID
        $subscription_id = $customer_subscription->ID
    
        // IMPORTANT HERE: Get an instance of the WC_Subscription Object
        $subscription = new WC_Subscription( $subscription_id );
        // Or also you can use
        // wc_get_order( $subscription_id ); 
    
        // Getting the related Order ID (added WC 3+ comaptibility)
        $order_id = method_exists( $subscription, 'get_parent_id' ) ? $subscription->get_parent_id() : $subscription->order->id;
    
        // Getting an instance of the related WC_Order Object (added WC 3+ comaptibility)
        $order = method_exists( $subscription, 'get_parent' ) ? $subscription->get_parent() : $subscription->order;
    
        // Optional (uncomment below): Displaying the WC_Subscription object raw data
        // echo '<pre>';print_r($subscription);echo '</pre>';
    }
    

    您还可以在帖子查询中取消注释 'meta_key''meta_value' 数组行以获取一位客户的订阅...此代码经过测试并且有效

    这里最重要的是:

    $subscription = new WC_Subscription($customer_subscription->ID);
    

    ...因为您将获得 WC_Subscription 对象,您可以在其中应用所有 WC_Subscription 方法而不会出错,例如:

    $subscription = new WC_Subscription($post_id);
    $relared_orders_ids_array = $subscription->get_related_orders();
    

    【讨论】:

    • 我没有对我的 WooCommerce 问题采取太多行动。我可以请你们看看这个:stackoverflow.com/questions/44829634/…
    • 订单ID实际上是从$subscription-&gt;id这里提取的
    • @Amritpal 我已经更新了这个旧答案……它现在与 WooCommerce 版本 3+ 兼容……WC 3+ 中的订单 ID 提取自:$subscription-&gt;get_parent_id()
    猜你喜欢
    • 2017-12-03
    • 2017-04-07
    • 2015-11-27
    • 2017-08-26
    • 2019-07-29
    • 2017-09-17
    • 2019-01-03
    • 2015-01-11
    • 2022-10-15
    相关资源
    最近更新 更多