Standard WC Rest Api

Hi everyone ! :wave:

I am trying to use the standard Woo API in Frontity.
I realize that the Custom Post Type in Frontity (product in WC) require of type “type” field fixed to “product” and the “link” field must be included in the response.
both things can be done using the following snippet:

function pxe_wc_rest_prepare_product( $response, $post ) {
    $response->data['type'] = 'product';
    $response->data['link'] = get_permalink( $post->id );
    
    return $response;
}
add_filter( 'woocommerce_rest_prepare_product_object', 'pxe_wc_rest_prepare_product', 10, 2 );

Is there anything else that should be included in the response?

1 Like

That’s great! I think you don’t need anything else because the WooCommerce API already includes the id and the slug. Just be careful because I think that WC API use type field to store the product type, you may want to store this in other field if you need it.

Hi @SantosGuillamot :wave:

I think it’s not enough …
The data is not as it should be:

The endpoint is:
https://wp.laotramirada.com.uy/wp-json/wc/v3/products/

I think I found the solution ! :v:

In the standard Woocommerce endpoint there is a “name” field where the product title comes

That field is misinterpreted by Frontity as if the data belonged to an author.
(\node_modules@frontity\wp-source\src\libraries\schemas\index.ts)

The solution I found was to remove that field from the endpoint and add the product title into another field:

function pxe_wc_rest_prepare_product( $response, $post ) {
  $product = wc_get_product( $post->id );

  $response->data['type'] = 'product';
  $response->data['product_type'] = $product->get_type();

  unset($response->data['name']);
  $response->data['product_name'] = $product->get_name();

  $response->data['link'] = get_permalink( $post->id );

  return $response;
}
add_filter( 'woocommerce_rest_prepare_product_object', 'pxe_wc_rest_prepare_product', 10, 2 );

What do you think ?

You’re totally right! Right now @frontity/wp-source is assuming that if the field name exists in the REST API, it’s considered and author, that’s why you were having that problem.

If If that solution works for you it seems totally fine, I’m glad you made it work :blush: Btw, we are working on a new version of source in order to make easier and prevent these problems, we’ll keep you posted about that :slightly_smiling_face:.

Yes, this scenario will be handled in the new version :+1:

Now, the final part is the correct configuration of taxonomies, this is my settings:

postTypes: [
  {
    type: 'product',
    endpoint: '/wc/v3/products/',
    archive: '/products'
  }
],
taxonomies: [
  {
    taxonomy: 'product_cat',
    endpoint: '/wc/v3/products/categories/',
    postTypeEndpoint: 'product',
  }
],

I think I should modify the product categories endpoint, right?

I don’t know if that’s going to work because those options expect regular postTypes/taxonomies from /wp/v2. But let us know what you find out :slight_smile:

what are the fields that frontity expects?

In the same way that i can modify the product endpoint (woocommerce_rest_prepare_product_object), I can modify the taxonomies endpoint (woocommerce_rest_prepare_product_cat)

Thanks ! :slightly_smiling_face:

I wouldn’t mind on doing that to be honest because we are going to fix that in Source v2 and then you’ll be able to use any normalization that you want.