How does Opencart 2 search work?

Opencart 2 Search feature gives the result based on the provided keywords using but the actual implementation may not fulfil your requirements. In this case, you could customize the search logic as per your requirements. Here is the explanation how the Opencart 2 search workflow works.

  1. When you press search, the URL become as http://localhost/onlinebooks/index.php?route=product/search&search=java
  2. Here “java” is the keyword we are going to search
  3. Here execution is handled by the controller “catalogue/controller/product/search.php”. Now in this controller, the follow things happens:

  4. “java” keyword is noted down in $search variable

  5. Also the same keyword is noted in $tag variable if the tag parameter is not set in search URL.

  6. Then “description” is checked in URL if it is there. It search URL contains the “description parameter” , its noted in $description variable.

  7. Other variables are set as well

  8. Then breadcrumb is prepared

  9. Then URL is prepared and the parameters received in search url is added as it is.

  10. Again breadcrumb is modified and url is added to breadcrumb

  11. Heading title value is set to search parameter, so that the page title will show the keyword you passed there.

  12. Other routine data is set to $data array

  13. Retrieve all categories and create the categories hierarchy and add this hierarchy in $data['categories'][]

  14. Then the filter data array is created there in which you could see the variables: [filter_name]="java", [filter_tag]="Java",[filter_description]="", [filter_category_id]="0", [filter_sub_category]="", [sort]="p.sort_order", [order]="ASC", [start]=0, [limit]="15".

  15. Now products total is received by invoking the getTotalProducts($fitler-data) which return the total of the product to be returned based on the current criteria
     
    The final query would be query to get total 

SELECT COUNT(DISTINCT p.product_id) AS total FROM oc_product p 
LEFT JOIN oc_product_description pd ON (p.product_id = pd.product_id) 
LEFT JOIN oc_product_to_store p2s ON (p.product_id = p2s.product_id) 
WHERE pd.language_id = '1' AND  p.status = '1' AND p.date_available <= NOW() 
AND p2s.store_id = '0' AND ( pd.name LIKE '%Java%' OR pd.tag LIKE '%java%' 
OR LCASE(p.model) = 'java' OR LCASE(p.sku) = 'java' OR LCASE(p.upc) = 'java' 
OR LCASE(p.ean) = 'java' OR LCASE(p.jan) = 'java' OR LCASE(p.isbn) = 'java' OR LCASE(p.mpn) = 'java')

 

16. Retrieve all products using the following statement

 

$results = $this->model_catalog_product->getProducts($filter_data);

 

Tags