Woocommerce-Abonnement erhält ID von Bestell-ID mit API

Lesezeit: 2 Minuten

Benutzeravatar von Damiano Fontana
Damiano Fontana

Ist es möglich, die Abonnement-ID aus der WooCommerce-Bestell-ID mit der API von WooCommerce zu erhalten? Ich verwende PHP und damit kann ich alle Bestelldaten abrufen, aber nicht die Abonnement-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);

  • Haben Sie einen ähnlichen API-Endpunkt www.example.com/wp-json/wc/v3/subscriptions ausprobiert

    – mujuonly

    25. April 2021 um 23:34 Uhr

  • Ja, aber ich brauche die Abonnement-ID für diesen Endpunkt … aber ich muss die Abonnement-ID zuerst von der Woocommerce-Bestell-ID erhalten, die ich habe

    – Damiano Fontana

    26. April 2021 um 12:24 Uhr

add_filter( 'woocommerce_api_order_response', 'add_woo_field_order_api_response', 20, 4 );

function add_woo_field_order_api_response( $order_data, $order, $fields, $server ) {

    // Get the subscription id
    $subscriptions_ids  = wcs_get_subscriptions_for_order( $order->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 == $order_id )
            break; // Stop the loop
    $order_data[ 'subscription_id' ] = $subscription_id;
    return $order_data;
}

Fügen Sie dieses Code-Snippet in die function.php Ihres serveraktiven Designs ein. Dann enthält die Bestell-API-Antwort die subscription_id

  • Ich habe diesen Code ausprobiert, aber in example.com/wp-json/wc/v3/orders/”.$orderId endopoint Ich sehe kein Element subscription_id

    – Damiano Fontana

    28. April 2021 um 14:24 Uhr

  • @DamianoFontana Prüfe, ob der Code ausgeführt wird, wenn die Anfrage ausgelöst wird.

    – mujuonly

    28. April 2021 um 14:47 Uhr

  • Ich habe versucht, einen Testparameter hinzuzufügen, aber ich kann ihn nicht in der Endpunktantwort sehen … function add_woo_field_order_api_response( $order_data, $order, $fields, $server ) { $order_data[‘test-parameter’] = “Test Test Test”;

    – Damiano Fontana

    28. April 2021 um 14:54 Uhr

  • dominykasgel.com/modify-woocommerce-api-orders-response

    – mujuonly

    28. April 2021 um 15:02 Uhr

  • Sollte ich statt add_woo_field_order_api_response prefix_wc_rest_prepare_order_object verwenden?

    – Damiano Fontana

    28. April 2021 um 16:43 Uhr

Ich habe mit diesem Code in meiner Datei functions.php in WordPress gelöst:

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);

Danke an mujuonly für die Referenz und für das erste Snippet.

1434650cookie-checkWoocommerce-Abonnement erhält ID von Bestell-ID mit API

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy