Hey again,
I found the problem, the schema in typesense was from an earlier attempt. I manually deleted the schema and reindexed the products. After some tries, i successfully indexed the products. I had some issues with field types, so i tried to use string on most of them. Any suggestions are welcome, this is my full code:
/*** Adds the post type book under available post_types ***/
function cm_typesense_add_available_post_types( $available_post_types ) {
$available_post_types['product'] = [ 'label' => 'Products', 'value' => 'product' ];
return $available_post_types;
}
add_filter( 'cm_typesense_available_post_types', 'cm_typesense_add_available_post_types');
//only necessary if the default post schema is not necessary
function cm_typesense_add_product_schema( $schema, $name ) {
if ( $name == 'product' ) {
$schema = [
'name' => 'product',
'fields' => [
[ 'name' => 'description', 'type' => 'string' ],
[ 'name' => 'name', 'type' => 'string' ],
[ 'name' => 'short_description', 'type' => 'string' ],
[ 'name' => 'date_modified', 'type' => 'string' ],
[ 'name' => 'id', 'type' => 'int64' ],
[ 'name' => 'permalink', 'type' => 'string' ],
[ 'name' => 'image', 'type' => 'string' ],
[ 'name' => 'ean', 'type' => 'string' ],
[ 'name' => 'sku', 'type' => 'string' ],
[ 'name' => 'popularity', 'type' => 'int32' ],
[ 'name' => 'product_cost', 'type' => 'string' ],
[ 'name' => 'price', 'type' => 'string', 'facet' => true ],
[ 'name' => 'supplier', 'type' => 'string' ],
[ 'name' => 'categories', 'type' => 'string[]', 'facet' => true ],
[ 'name' => 'tags', 'type' => 'string[]', 'facet' => true ],
[ 'name' => 'brand', 'type' => 'string', 'facet' => true ],
//[ 'name' => 'stock_status', 'type' => 'string', 'facet' => true ],
[ 'name' => 'date_modified', 'type' => 'string' ],
],
'default_sorting_field' => 'popularity',
];
}
return $schema;
}
add_filter( 'cm_typesense_schema', 'cm_typesense_add_product_schema', 10, 2 );
//you can modify the code as needed
//if your schema is simillar to the default post type then you should only change what fields you need to add
//example here we are only adding genre
function cm_typesense_format_product_data ( $formatted_data, $raw_data, $object_id, $schema_name ) {
if ( $schema_name == 'product' ) {
$product = wc_get_product( $object_id );
if ( ! $product ) {
return $formatted_data;
}
$formatted_data = array();
$tags = get_the_terms( $object_id, 'product_tag' );
$product_tags = array();
foreach ( $tags as $tag ) {
$product_tags[] = $tag->name;
}
$cats = get_the_terms( $object_id, 'product_cat' );
$product_cats = array();
foreach ( $cats as $cat ) {
$product_cats[] = $cat->name;
}
$formatted_data[ 'name' ] = $product->get_title();
$formatted_data[ 'description' ] = $product->get_description();
$formatted_data[ 'short_description' ] = $product->get_short_description();
$formatted_data[ 'id' ] = (string) $object_id;
$formatted_data[ 'permalink' ] = $product->get_permalink();
$formatted_data[ 'date_modified' ] = $product->get_stock_status();
$formatted_data[ 'image' ] = wp_get_attachment_url( $product->get_image_id() );
$formatted_data[ 'ean' ] = $product->get_meta('we_skroutzxml_ean_barcode');
$formatted_data[ 'sku' ] = $product->get_sku();
$formatted_data[ 'popularity' ] = (int) $product->get_meta('total_sales');
$formatted_data[ 'price' ] = $product->get_price();
$formatted_data[ 'product_cost' ] = $product->get_meta('_wc_cog_cost');
$formatted_data[ 'categories' ] = $product_cats;
$formatted_data[ 'tags' ] = $product_tags;
$formatted_data[ 'supplier' ] = $product->get_attribute('pa_supplier');
$formatted_data[ 'brand' ] = $product->get_attribute('pa_manufacturer');
//$formatted_data[ 'stock_status' ] = $product->get_stock_status();
$formatted_data[ 'date_modified' ] = (string) $product->get_date_modified();
}
return $formatted_data;
}
add_filter( 'cm_typesense_data_before_entry', 'cm_typesense_format_product_data', 10, 4 );