我正在使用woocommerce订阅,我正在编写插件来更新外部系统,如果用户升级或降级他的订阅,它会显示为带有ID的新订单,但我可以'获得与订单ID相关的订阅ID(它是常数),我查看文档但我找不到解决方案。
答案 0 :(得分:0)
您可以通过挂钩WooCommerce订阅提供的processed_subscription_payment
操作来访问此数据:
add_action( 'processed_subscription_payment', 'se43079522_process_subscription', 10, 2 );
function se43079522_process_subscription($user_id, $subscription_key) {
// here you have access to the $subscription_key (ID) and the $user_id associated
}
请查看此链接:https://docs.woocommerce.com/document/subscriptions/develop/action-reference/,了解有关此插件的可用操作的更多信息。
或者,您可以像这样手动执行此操作:
阅读漫游的评论
global $woocommerce;
// Get the order ID and save as variable
$order_id = [ORDER_ID];
// Get the order object
$order = new WC_Order( $order_id );
// Loop through the subscription order
foreach ( WC_Subscriptions_Order::get_recurring_items( $order ) as $order_item ) {
// Get the subscription key
$subscription_key = WC_Subscriptions_Manager::get_subscription_key( $order->id, WC_Subscriptions_Order::get_items_product_id( $order_item ) );
}
// This is your subscription key (ID)
echo $subscription_key;
答案 1 :(得分:0)
从订单中获取订阅ID。您可以使用wcs_get_subscriptions_for_order woocommerce函数获取具有与该订单相关的订阅的对象,并从那里获取订阅ID。
$ subscriptions = wcs_get_subscriptions_for_order($ order_id);
var_dump($ subscriptions);
答案 2 :(得分:0)
我能够做到这样。
$subscriptions = wcs_get_subscriptions_for_order($order_id, array( 'order_type' => 'any' ));
foreach( $subscriptions as $subscription_id => $subscription_obj )
{
$current_subs_id = $subscription_obj->get_id(); // This is current subscription id
$parent_id = $subscription_obj->get_parent_id(); // This is subscription parent id
}