API Response Reference

Filter

surecart/request/response

This filter allows developers to intercept and modify the response returned from SureCart REST API requests. It provides a powerful way to inspect, transform, or enhance response data before it is delivered to the front-end or consuming application.

Usage

You can hook into this filter using add_filter() to implement your custom logic based on the request arguments and endpoint.

Here’s an example that modifies the response from the checkouts/ endpoint - It replace the Custom Amount text for ad-hoc price amount.

add_filter( 'surecart/request/response', function( $response, $args, $endpoint ) {
    // Only target checkout endpoints.
    if ( ! str_starts_with( $endpoint, 'checkouts/' ) ) {
        return $response;
    }

    // Check if we have line items in the response.
    if ( isset( $response->line_items ) && isset( $response->line_items->data ) && is_array( $response->line_items->data ) ) {
        foreach ( $response->line_items->data as $key => $line_item ) {
            // Only modify if this is an ad_hoc price.
            if ( isset( $line_item->price ) && ! empty( $line_item->price->ad_hoc ) ) {
                if ( isset( $line_item->total_amount ) ) {
                    $response->line_items->data[ $key ]->ad_hoc_amount = $line_item->total_amount;
                }
            }
        }
    }

    return $response;
}, 10, 3 );

Parameters

  • $response (object)
    The API response object returned by the SureCart endpoint. You can inspect and modify its structure or contents.
  • $args (array)
    The arguments passed with the request. This may include method type, request body, or any custom parameters.
  • $endpoint (string)
    The name of the SureCart endpoint being accessed (e.g., checkouts/[checkout-id], products, etc.).

Notes

  • This filter is global, meaning it runs for all SureCart API responses.
  • You should always scope your logic to specific endpoints (e.g., checkouts/) to avoid unintentionally affecting other data structures.
  • Helpful for response post-processing, debugging, or injecting additional custom data.

Would you like this merged directly into your original doc as a full unified version?