Cs-cart search by relevance

This post will explain how to incorporate Search by Relevance using MySQL full text search in Cs-cart.  This Doesn’t require any core file modifications,  add on code and sql has been used.
Cs-cart Sort by relevance search page

You can edit one of your existing addons.  If you don’t have an existing addon, do the changes in my_changes addon, just make sure you have it installed.  To illustrate I will do the changes in my_changes addon.

  1. Make sure you have the addon installed.  Verify from Administration->addons page.
    my_changes_installed
  2. I found it difficult to add a new product list sorting, so I will edit an existing one “No Sorting”.  I will make it behave like relevance.  You can see the list of available product list sorting from Settings->AppearanceAvailable product list sorting
  3. Modify sort_by_null_asc text variable to Sort by Relevance.
    Administration -> Languages -> Translations
    txt_sort_by_relevance
  4. Make Sort by Relevance available and default.
    Sort by Relevance available
  5. Add php addon code, create folder and files if not present.
    app\addons\my_changes\func.php

    
    <?php
    
    use Tygh\Registry;
    
    if (!defined('BOOTSTRAP')) { die('Access denied'); }
    
    function fn_my_changes_get_products(&$params, &$fields, &$sortings, &$condition, &$join, &$sorting, &$group_by, &$lang_code)
    {
    
     // set default sorting
     if (empty($params['sort_by']) || empty($sortings[$params['sort_by']])) {
     $params = array_merge($params, fn_get_default_products_sorting());
     if (empty($sortings[$params['sort_by']])) {
     $_products_sortings = fn_get_products_sorting(false);
     $params['sort_by'] = key($_products_sortings);
     }
     }
    
     // sort by relevance
     if($params['search_performed'] == "Y" && $params['q'] && $params['sort_by'] == "null"){
     $escaped_q = str_replace("'","\'",$params['q']) ;
     $fields[] = "MATCH (descr1.search_words,descr1.product) AGAINST ('".$escaped_q."') as score " ;
     // overwrite where cond
     $condition = " AND MATCH (descr1.product,descr1.search_words) AGAINST ('".$escaped_q."') AND companies.status = 'A' AND products.status IN ('A') AND (products.usergroup_ids = '' OR FIND_IN_SET(0, products.usergroup_ids) OR FIND_IN_SET(1, products.usergroup_ids)) AND products.status IN ('A') AND prices.usergroup_id IN (0, 0, 1) " ;
     $sortings['null'] = 'score' ;
     $params['sort_order'] = 'desc' ; // always sort in descending relevance
     }
    
    }
    
    ?>
    
    
  6. Add code hook so that the above function gets called.
    app\addons\my_changes\init.php

    <?php
    use Tygh\Registry;
    
    if (!defined('BOOTSTRAP')) { die('Access denied'); }
    
    fn_register_hooks(
     'get_products'
    );
    
    ?>
    
  7. Run the following sql in your database to create a full text index on product names and search words.
    ALTER TABLE `cscart_product_descriptions` ADD FULLTEXT `product_full_text` (`product`, `search_words`);
    
    
  8. That’s it, you should see Sort by Relevance now in your search page.
Posted in CS-Cart, CS-Cart Addons, MySql, PHP Tagged with: , , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*