WooCommerce订阅-获取特定订阅的相关订单ID [英] WooCommerce Subscriptions - Get related orders Ids for a specific subscription
问题描述
我在此官方文档中找到了 Subscription Function&物业参考:
I have found in this official documentation Subscription Function & Property Reference:
WC_Subscription::get_related_orders( $return_fields, $order_type );
但这似乎不是针对特定的订阅吗?
But this does not seem to be for specific subscription?
每当我尝试运行它时,都会出现致命错误,无论传入的内容是什么:
When ever I try to run it I get a fatal error no mater what I pass in:
致命错误:未捕获错误:在不在对象上下文中时使用$ this C:\ xampp \ htdocs \ mysite.com \ wp-content \ plugins \ woocommerce-subscriptions \ includes \ class-wc-subscription.php:1413
Fatal error: Uncaught Error: Using $this when not in object context in 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
"表,但是它们都不提供获取用户购买的订阅的相关订单的方法...
I am making my own plugin and I select all subscriptions where post status = wc-active
from post table. I have looked in the "woocommerce_order_items
", "woocommerce_order_itemmeta
" and "postmeta
" tables but neither of them provide way to get related orders for user bought subscription...
如果我只知道用户购买的订阅及其相关订单的关系在哪里,那么我可以编写一些sql,但我不知道,google也不会产生任何结果.
If I only knew where are the relation for user bought subscriptions and its related orders, then i could write some sql but I have no idea and google does not yield any results either.
有什么想法吗?
我的设置:
- php版本7.0.4
- wordpress版本4.7.3
- woocommerce 2.6.8
- woocommerce订阅:2.0.18
推荐答案
更新:添加了WooCommerce版本3+兼容性
从订阅对象中获取订单ID非常容易.我将像您一样从发布表中选择所有订阅,其中 'post status' = 'wc-active'
.
It's very easy to get the order ID from subscription object. I am going to select, just like you, all subscriptions where 'post status' = 'wc-active'
from post table.
// 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'
数组行以获取一位客户的订阅…此代码已经过测试并且有效
You can also uncomment in the post query the 'meta_key'
and 'meta_value'
array lines to get the subscriptions for one customer… This code is tested and works
这里最重要的是:
The most important thing here is:
$subscription = new WC_Subscription($customer_subscription->ID);
…因为您将获得WC_Subscription对象,您可以在其中应用所有WC_Subscription方法而不会出错,例如:
…as you will get the WC_Subscription object in which you can apply all WC_Subscription methods without getting errors, with for example:
$subscription = new WC_Subscription($post_id);
$relared_orders_ids_array = $subscription->get_related_orders();
这篇关于WooCommerce订阅-获取特定订阅的相关订单ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!