Custom WordPress and WooCommerce Development

Extending WooCommerce Product REST API Responses

WooCommerce supports many core properties when fetching a product via the REST API. In this tutorial, we’ll explore three code snippets that extend WooCommerce product responses, allowing for the retrieval and updating of custom fields and metadata.

1. Adding Custom Fields to Product Schema

The first snippet focuses on modifying the product schema, defining additional fields for product responses. This is achieved by using the woocommerce_rest_product_schema filter hook. Let’s break down the code:

function kia_filter_product_schema( $schema ) {
    return array_merge( $schema, array(
        'kia_custom_field_1' => array(
            'description' => esc_html__( 'This is an example of a field that is associated with custom setters/getters. It would be more typically used with a custom product type.', 'your-text-domain' ),
            'type'        => 'boolean',
            'context'     => array( 'view', 'edit' ),
        ),
        'kia_custom_field_2' => array(
            'description' => esc_html__( 'This is an example of a field that is associated with product meta data.', 'your-text-domain' ),
            'type'        => 'string',
            'context'     => array( 'view', 'edit' ),
        ),
    ));
}
add_filter( 'woocommerce_rest_product_schema', 'kia_filter_product_schema' );Code language: PHP (php)

This function hooks into woocommerce_rest_product_schema, extending the schema with two custom fields: kia_custom_field_1 and kia_custom_field_2, each defined with a description, type, and context.

2. Modifying Product Response Data

The second snippet modifies the data of a product response before it is returned. It utilizes the woocommerce_rest_prepare_product_object filter hook. Here’s a breakdown:

function kia_prepare_product_response( $response, $product ) {
    $data = $response->get_data();

    if ( is_callable( array( $product, 'your_custom_getter' ) ) ) {
        $data['kia_custom_field_1'] = $product->your_custom_getter();
    } 

    $data['kia_custom_field_2'] = $product->get_meta( 'kia_custom_field_2', true );

    $response->set_data( $data );

    return $response;
}
add_filter( 'woocommerce_rest_prepare_product_object', 'kia_prepare_product_response', 10, 3 );Code language: PHP (php)

This function intercepts the product response object, retrieves its data, and adds custom fields (kia_custom_field_1 and kia_custom_field_2) based on custom getters and product metadata.

3. Preparing Product Data for Insertion

The third snippet prepares product data before insertion via the REST API using the woocommerce_rest_pre_insert_product_object filter hook:

function kia_prepare_insert_product( $product, $request ) {
    if ( is_callable( array( $product, 'your_custom_setter' ) ) && isset( $request['kia_custom_field_1'] ) ) {
        $product->your_custom_setter( $request['kia_custom_field_1'] );
    }

    if ( isset( $request['kia_custom_field_2'] ) ) {
        $product->update_meta_data( 'kia_custom_field_2', $request['kia_custom_field_2'] );
    }

    return $product;
}
add_filter( 'woocommerce_rest_pre_insert_product_object', 'kia_prepare_insert_product', 10, 2 );Code language: PHP (php)

This function intercepts the product object before insertion and sets custom fields (kia_custom_field_1 and kia_custom_field_2) based on custom setters and provided request data.

By utilizing these snippets, developers can extend WooCommerce product responses with custom fields and metadata, enriching the API capabilities and enhancing the overall shopping experience for customers.


Comments

One response to “Extending WooCommerce Product REST API Responses”

  1. Good one! Thank you for sharing!