WooCommerce 订阅 - 获取特定订阅的相关订单 ID
WooCommerce Subscriptions - Get related orders Ids for a specific subscription
是否有 woocommerce 功能可以 return 为用户购买的特定订阅提供所有相关订单(至少是订单 ID)?
我在这个官方文档中找到了Subscription Function & Property Reference:
WC_Subscription::get_related_orders( $return_fields, $order_type );
不过这个好像不是特定订阅的吧?
每当我尝试 运行 它时,我都会收到 致命错误 无论我传入什么:
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
我正在制作自己的插件,并且我 select 所有 post status = wc-active
来自 post table 的订阅。我查看了“woocommerce_order_items
”、“woocommerce_order_itemmeta
”和“postmeta
”table,但它们都没有提供获取用户购买订阅的相关订单的方式。 .
如果我只知道用户购买的订阅及其相关订单的关系在哪里,那么我可以写一些 sql 但我不知道 google 也不会产生任何结果。
有什么想法吗?
我的设置:
- php 版本 7.0.4
- wordpress 版本 4.7.3
- woocommerce 2.6.8
- woocommerce 订阅:2.0.18
Updated: Added WooCommerce version 3+ Compatibility
很容易从订阅对象中获取订单ID。我要去 select,就像你一样,所有订阅 其中 'post status' = 'wc-active'
来自 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>';
}
您也可以在 post 查询 'meta_key'
和 'meta_value'
数组行中取消注释以获取一个客户的订阅...此代码已经过测试并且有效
The most important thing here is:
$subscription = new WC_Subscription($customer_subscription->ID);
…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 功能可以 return 为用户购买的特定订阅提供所有相关订单(至少是订单 ID)?
我在这个官方文档中找到了Subscription Function & Property Reference:
WC_Subscription::get_related_orders( $return_fields, $order_type );
不过这个好像不是特定订阅的吧?
每当我尝试 运行 它时,我都会收到 致命错误 无论我传入什么:
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
我正在制作自己的插件,并且我 select 所有 post status = wc-active
来自 post table 的订阅。我查看了“woocommerce_order_items
”、“woocommerce_order_itemmeta
”和“postmeta
”table,但它们都没有提供获取用户购买订阅的相关订单的方式。 .
如果我只知道用户购买的订阅及其相关订单的关系在哪里,那么我可以写一些 sql 但我不知道 google 也不会产生任何结果。
有什么想法吗?
我的设置:
- php 版本 7.0.4
- wordpress 版本 4.7.3
- woocommerce 2.6.8
- woocommerce 订阅:2.0.18
Updated: Added WooCommerce version 3+ Compatibility
很容易从订阅对象中获取订单ID。我要去 select,就像你一样,所有订阅 其中 'post status' = 'wc-active'
来自 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>';
}
您也可以在 post 查询 'meta_key'
和 'meta_value'
数组行中取消注释以获取一个客户的订阅...此代码已经过测试并且有效
The most important thing here is:
$subscription = new WC_Subscription($customer_subscription->ID);
…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();