Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Examples and use cases for ExtendSchema (#11245)
Browse files Browse the repository at this point in the history
* Examples and use cases

* Address feedback

* Revert addition to main file
  • Loading branch information
mikejolley authored Oct 16, 2023
1 parent 9cdea40 commit 45b0ca8
Showing 1 changed file with 168 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,174 @@

## Table of Contents <!-- omit in toc -->

- [`wc/store/checkout`](#wcstorecheckout)
- [Passed Parameters](#passed-parameters)
- [Key](#key)
- [`wc/store/cart`](#wcstorecart)
- [Passed Parameters](#passed-parameters-1)
- [Key](#key-1)
- [`wc/store/cart/items`](#wcstorecartitems)
- [Passed Parameters](#passed-parameters-2)
- [Key](#key-2)
- [`wc/store/products`](#wcstoreproducts)
- [Passed Parameters](#passed-parameters-3)
- [Key](#key-3)

To see how to add your data to Store API using ExtendSchema, [check this document](./extend-rest-api-add-data.md). If you want to add a new endpoint, [check this document](../../../internal-developers/rest-api/extend-rest-api-new-endpoint.md).

This is a list of available endpoints that you can extend. For other endpoints, [see here](./../../../../src/StoreApi/README.md).

## `wc/store/checkout`

The checkout endpoint is extensible via ExtendSchema. The data is available via the `extensions` key in the response.

### Passed Parameters

- `data_callback`: none.
- `schema_callback`: none.

### Key

- `CheckoutSchema::IDENTIFIER`

## `wc/store/cart`

The main cart endpoint is extensible via ExtendSchema. The data is available via the `extensions` key in the response.

### Passed Parameters

- `data_callback`: none.
- `schema_callback`: none.

### Key

- `CartSchema::IDENTIFIER`

## `wc/store/cart/items`

The items endpoint, which is also available on `wc/store/cart` inside the `items` key. The data would be available inside each item of the `items` array.

### Passed Parameters

- `data_callback`: `$cart_item`.
- `schema_callback` none.

### Key

- `CartItemSchema::IDENTIFIER`

## `wc/store/products`

The main products endpoint is extensible via ExtendSchema. The data is available via the `extensions` key for each `product` in the response array.

### Passed Parameters

- `data_callback`: `$product`.
- `schema_callback` none.

### Key

- `ProductSchema::IDENTIFIER`
- [Products](#products)
- [Use Cases](#use-cases)
- [Example](#example)
- [Cart](#cart)
- [Use Cases](#use-cases-1)
- [Example](#example-1)
- [Cart Items](#cart-items)
- [Use Cases](#use-cases-2)
- [Example](#example-2)
- [Checkout](#checkout)
- [Use Cases](#use-cases-3)
- [Example](#example-3)

Some endpoints of the Store API are extensible via a class called `ExtendSchema`. This allows you to customise the data (including the schema) that is returned by the Store API so that it can be consumed by your application or plugin.

For more information about extending the Store API, you may also be interested in:

- [How to add your data to Store API using `ExtendSchema`](./extend-rest-api-add-data.md)
- [How to add a new endpoint](../../../internal-developers/rest-api/extend-rest-api-new-endpoint.md)

Below is a list of available endpoints that you can extend using `ExtendSchema`, as well as some example use-cases.

## Products

The main `wc/store/products` endpoint is extensible via ExtendSchema. The data is available via the `extensions` key for each `product` in the response array.

This endpoint can be extended using the `ProductSchema::IDENTIFIER` key. For this endpoint, your `data_callback` callback function is passed `$product` as a parameter. Your `schema_callback` function is passed no additional parameters; all products should share the same schema.

### Use Cases

This endpoint is useful for adding additional data about individual products. This could be some meta data, additional pricing, or anything else to support custom blocks or components on the products page.

### Example

```php
woocommerce_store_api_register_endpoint_data(
array(
'endpoint' => ProductSchema::IDENTIFIER,
'namespace' => 'my_plugin_namespace',
'data_callback' => function( $product ) {
return array(
'my_meta_data' => get_post_meta( $product->get_id(), 'my_meta_data', true ),
);
},
'schema_callback' => function() {
return array(
'properties' => array(
'my_meta_data' => array(
'type' => 'string',
),
),
);
},
'schema_type' => ARRAY_A,
)
);
```

## Cart

The main `wc/store/cart` endpoint is extensible via ExtendSchema. The data is available via the `extensions` key in the response.

This endpoint can be extended using the `CartSchema::IDENTIFIER` key. For this endpoint, your `data_callback` and `schema_callback` functions are passed no additional parameters.

### Use Cases

This endpoint is useful for adding additional data to the cart page, for example, extra data about the cart items, or anything else needed to support custom blocks displayed on the cart page.

### Example

```php
woocommerce_store_api_register_endpoint_data(
array(
'endpoint' => CartSchema::IDENTIFIER,
'namespace' => 'my_plugin_namespace',
'data_callback' => function() {
return array(
'foo' => 'bar',
);
},
'schema_callback' => function() {
return array(
'properties' => array(
'foo' => array(
'type' => 'string',
),
),
);
},
'schema_type' => ARRAY_A,
)
);
```

## Cart Items

The `wc/store/cart/items` endpoint, which is also available on `wc/store/cart` inside the `items` key. The data would be available inside each item of the `items` array.

This endpoint can be extended using the `CartItemSchema::IDENTIFIER` key. For this endpoint, your `data_callback` callback function is passed `$cart_item` as a parameter. Your `schema_callback` function is passed no additional parameters; all cart items should share the same schema.

### Use Cases

This endpoint is useful for adding additional data about individual cart items. This could be some meta data, additional pricing, or anything else to support custom blocks or components on the cart page.

### Example

```php
woocommerce_store_api_register_endpoint_data(
array(
'endpoint' => CartItemSchema::IDENTIFIER,
'namespace' => 'my_plugin_namespace',
'data_callback' => function( $cart_item ) {
$product = $cart_item['data'];
return array(
'my_meta_data' => get_post_meta( $product->get_id(), 'my_meta_data', true ),
);
},
'schema_callback' => function() {
return array(
'properties' => array(
'my_meta_data' => array(
'type' => 'string',
),
),
);
},
'schema_type' => ARRAY_A,
)
);
```

## Checkout

The `wc/store/checkout` endpoint is extensible via ExtendSchema. Additional data is available via the `extensions` key in the response.

This endpoint can be extended using the `CheckoutSchema::IDENTIFIER` key. For this endpoint, your `data_callback` and `schema_callback` functions are passed no additional parameters.

### Use Cases

This endpoint is useful for adding additional data to the checkout page, such as a custom payment method which requires additional data to be collected from the user or server.

**Important: Do **not** reveal any sensitive data in this endpoint, as it is publicly accessible. This includes private keys for payment services.**

### Example

```php
woocommerce_store_api_register_endpoint_data(
array(
'endpoint' => CheckoutSchema::IDENTIFIER,
'namespace' => 'my_plugin_namespace',
'data_callback' => function() {
return array(
'foo' => 'bar',
);
},
'schema_callback' => function() {
return array(
'properties' => array(
'foo' => array(
'type' => 'string',
),
),
);
},
'schema_type' => ARRAY_A,
)
);
```

<!-- FEEDBACK -->

Expand All @@ -80,4 +180,3 @@ The main products endpoint is extensible via ExtendSchema. The data is available
🐞 Found a mistake, or have a suggestion? [Leave feedback about this document here.](https://github.com/woocommerce/woocommerce-blocks/issues/new?assignees=&labels=type%3A+documentation&template=--doc-feedback.md&title=Feedback%20on%20./docs/third-party-developers/extensibility/rest-api/available-endpoints-to-extend.md)

<!-- /FEEDBACK -->

0 comments on commit 45b0ca8

Please sign in to comment.