Php Woocommerce订阅使用API从订单id获取id

Php Woocommerce订阅使用API从订单id获取id,php,wordpress,woocommerce,wordpress-rest-api,woocommerce-subscriptions,Php,Wordpress,Woocommerce,Wordpress Rest Api,Woocommerce Subscriptions,是否可以使用Woocommerce的API从Woocommerce订单id获取订阅id? 我正在使用PHP,通过它我可以获得所有订单数据,但不能获得订阅id: $curl = curl_init(); curl_setopt_array($curl, [ CURLOPT_RETURNTRANSFER => 1, CURLOPT_URL => "https://www.example.com/wp-json/wc/v3/orders/".$order

是否可以使用Woocommerce的API从Woocommerce订单id获取订阅id? 我正在使用PHP,通过它我可以获得所有订单数据,但不能获得订阅id:

$curl = curl_init();
curl_setopt_array($curl, [
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => "https://www.example.com/wp-json/wc/v3/orders/".$orderId,
    CURLOPT_USERPWD => 'code:code',
    CURLOPT_HTTPHEADER => array(
         "accept: application/json"
    )
]);
$woocommerceOrder = curl_exec($curl);

在服务器active theme function.php中添加此代码段。然后order API响应将包含订阅id,我在Wordpress的functions.php文件中用以下代码解决了该问题:

function prefix_wc_rest_prepare_order_object($response, $object, $request){
    // Get the subscription id
    $subscriptions_ids = wcs_get_subscriptions_for_order($object->get_id(), array('order_type' => 'any'));
    // We get all related subscriptions for this order
    foreach($subscriptions_ids as $subscription_id => $subscription_obj){
        if($subscription_obj->order->id == $object->get_id()){
            break; // Stop the loop
        }
    }
    $response->data['subscription_id'] = $subscription_id;
    return $response;
}
add_filter('woocommerce_rest_prepare_shop_order_object', 'prefix_wc_rest_prepare_order_object', 10, 3);

感谢mujuonly提供的参考和初始代码片段。

您是否尝试过类似的API端点www.example.com/wp-json/wc/v3/subscriptions是的,但我需要该端点的订阅id…但我需要首先从我尝试过的woocommerce订单id获取订阅id,但在Endoint中,我没有看到任何元素订阅_id@DamianoFontana检查触发请求时代码是否执行。我已尝试添加测试参数,但在端点响应中看不到它。。。函数add_-woo_-field_-order_-api_-response($order_-data,$order,$fields,$server){$order_-data['test-parameter']=“test-test-test-test”;我应该使用前缀_-wc_-rest_-prepare_-order_-api_-response来代替add_-woo_-field_-api_-response吗?
function prefix_wc_rest_prepare_order_object($response, $object, $request){
    // Get the subscription id
    $subscriptions_ids = wcs_get_subscriptions_for_order($object->get_id(), array('order_type' => 'any'));
    // We get all related subscriptions for this order
    foreach($subscriptions_ids as $subscription_id => $subscription_obj){
        if($subscription_obj->order->id == $object->get_id()){
            break; // Stop the loop
        }
    }
    $response->data['subscription_id'] = $subscription_id;
    return $response;
}
add_filter('woocommerce_rest_prepare_shop_order_object', 'prefix_wc_rest_prepare_order_object', 10, 3);