• Resolved sannin

    (@sannin)


    Hello, thank you for this plugin!

    I have tried to index the products of my woocommerce shop using the filters in your documentantion

    
    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' => 'string' ],
                    [ 'name' => 'permalink', 'type' => 'string' ],
                    [ 'name' => 'image', 'type' => 'string' ],
                    [ 'name' => 'ean', 'type' => 'int64' ],
                    [ 'name' => 'sku', 'type' => 'string' ],
                    [ 'name' => 'popularity', 'type' => 'int32' ],
                    [ 'name' => 'product_cost', 'type' => 'int32' ],
                    [ 'name' => 'price', 'type' => 'float', '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' => 'date_modified',
            ];
        }
    
        return $schema;
    }
    
    add_filter( 'cm_typesense_schema', 'cm_typesense_add_product_schema', 10, 2 );
    

    The indexing fails with this error:

    comment_count has been declared as a default sorting field, but is not found in the document

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author CodeManas

    (@codemanas)

    Hi @sannin ,

    The documentation needs to be update a bit thanks for bringing this to our attention. Can you please check your schema on typesense.

    If you have removed comment_count as the default_sorting_field and added date_modified.

    Then – the code to format the data should be changed.

    Thread Starter sannin

    (@sannin)

    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 );
    

    Thank you for the code @sannin I added the code to the functions.php now Products option appears in the “Select Posts Types”

    I indexed the product post. It says “All entries for “product” have been successfully Imported” however nothing shows up when searching.

    I checked the cluster on cloud.typesense.org and “product” has zero results. It says “No results found”

    @codemanas can you help me with that?

    Update: If I set thumbnail (product image) then posts appear in the cluster but still do not shows up in when searching in the site.

    • This reply was modified 3 years, 2 months ago by Ahmet.
    • This reply was modified 3 years, 2 months ago by Ahmet.
    Thread Starter sannin

    (@sannin)

    The fields i use are customized to the custom fields and attributes in my shop. Maybe if you remove the relevant code for supplier, brand, ean and product_cost it could work.

    Thread Starter sannin

    (@sannin)

    I changed the field names of my code to be more compatible to the ones already used in the plugin. I got instant search working, but not autocomplete.

    Some documentation and more customization options on the templates would be nice!

    Thread Starter sannin

    (@sannin)

    You could have told us, that you were planning a premium plugin, it could save me several hours of trying to do it by myself! ??

    For support issues / enhancements should i contact you directly? Because i think that some things can be done in the core plugin.

    • This reply was modified 3 years ago by sannin. Reason: Solved
    Plugin Contributor digamberpradhan

    (@digamberpradhan)

    Hi @sannin ,

    Sorry about that, the premium plugin was planned along side the core version but launch date was not specified so we omitted it.

    Yes, if you have some more suggestions or enhancements you can reach us via
    https://www.codemanas.com/hire-or-customize/

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Woocommerce Support’ is closed to new replies.