1

我正在使用 woocommerce 订阅,并且正在编写一个插件来更新外部系统,如果用户升级或降级他的订阅,它会显示为带有其 ID 的新订单,但我无法获得订阅 ID(它是恒定的) 与订单 ID 相关,我查看了文档但找不到解决方案。

4

3 回答 3

2

我能够做到这样。

$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
}
于 2020-08-17T07:49:22.767 回答
0

从订单中获取订阅 ID。您可以使用 wcs_get_subscriptions_for_order woocommerce 函数获取具有与该订单相关的订阅的对象,并从那里获取订阅 ID。

$subscriptions = wcs_get_subscriptions_for_order($order_id);

var_dump($subscriptions);

于 2018-12-10T21:21:08.183 回答
-1

您可以通过挂钩processed_subscription_paymentWooCommerce 订阅提供的操作来访问此数据:

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;
于 2017-03-28T20:51:54.317 回答