Ugi*_*gis 8 php wordpress orders woocommerce woocommerce-subscriptions
我在此官方文档中找到了订阅功能和属性参考:
WC_Subscription::get_related_orders( $return_fields, $order_type );
Run Code Online (Sandbox Code Playgroud)
但这似乎不适合特定订阅?
当我试图运行它时,我得到一个致命的错误,无论我传递的是什么:
致命错误:未捕获错误:在C:\ xampp\htdocs\mysite.com\wp-content\plugins\woocommerce-subscriptions\includes\class-wc-subscription.php:1413中不在对象上下文中时使用$ this
我正在制作自己的插件,并post status = wc-active
从post表中选择所有订阅.我查看了" woocommerce_order_items
"," woocommerce_order_itemmeta
"和" postmeta
"表格,但它们都没有提供获取用户购买订阅的相关订单的方法......
如果我只知道用户购买订阅及其相关订单的关系在哪里,那么我可以写一些sql,但我不知道,谷歌也不会产生任何结果.
有任何想法吗?
我的设置:
Loi*_*tec 12
更新: 添加了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>';
}
Run Code Online (Sandbox Code Playgroud)
您还可以在post查询'meta_key'
和'meta_value'
数组行中取消注释以获取一个客户的订阅...此代码已经过测试并且可以正常运行
这里最重要的是:
Run Code Online (Sandbox Code Playgroud)$subscription = new WC_Subscription($customer_subscription->ID);
...因为您将获得WC_Subscription对象,您可以在其中应用所有WC_Subscription方法而不会出现错误,例如:
Run Code Online (Sandbox Code Playgroud)$subscription = new WC_Subscription($post_id); $relared_orders_ids_array = $subscription->get_related_orders();
归档时间: |
|
查看次数: |
6727 次 |
最近记录: |