Follow us on Twitter X-Cart on Facebook Wiki
Shopping cart software Solutions for online shops and malls

Front end (Customer) product search logic and search results - worse than X-Cart 4?

 
Reply
   X-Cart forums > X-Cart 5 > Dev Questions (X-Cart 5)
 
Thread Tools Search this Thread
  #1  
Old 08-18-2022, 04:07 AM
 
xcartdev2 xcartdev2 is offline
 

Advanced Member
  
Join Date: Nov 2011
Posts: 43
 

Default Front end (Customer) product search logic and search results - worse than X-Cart 4?

Just having a tinker with X-Cart 5.4 code. As far as I see in the code for customer searches:
  • to start with the page loads with only an item count.
  • then there is another 'getData' function call to get a list of products without any of their associated details from other tables (like title and description)
  • then this is looped through to get things like name and brief description only within the displayed product list
I have a client where the search results are re-ordered so products where the search substring in in the Title are returned before products where the substring is in the description/SKU.


I've hacked it like this:
PHP Code:
protected function getData(\XLite\Core\CommonCell $cnd$countOnly false)
    {

        
$orig_countOnly $countOnly;
        
$countOnly false;
        
        
$return1 = \XLite\Core\Database::getRepo('\XLite\Model\Product')->search(
            
$this->prepareCnd($cnd),
            
$countOnly
        
);
        
        
// find products with search string in title
         
$return2 = array();
         
$cnd->byTitle "Y";

        
$return2 = \XLite\Core\Database::getRepo('\XLite\Model\Product')->search(
            
$this->prepareCnd($cnd),
            
$countOnly
        
);

            foreach(
$return2 as $i => $i_value) {
                
                foreach (
$return1 as $t => $t_value) {
                    if (
$t_value->product_id == $i_value->product_id) { 
                        unset(
$return1[$t]);
                        }
                }

            } 
            
$return array_merge($return2$return1);

        if (
$orig_countOnly=="1") { // count only
            
$return count($return);
            }

        return 
$return;
    } 


But that's two calls to the database instead of one.



In X-Cart 4 the basic query called everything and then you could manipulate or get data from the array very easily. So if I wanted to have a page title that said:



Search for 'apple': cheapest product is Apple Ipod, costs $800


That was pretty easy. In X-Cart 5 it looks like a lot of coding will be needed or have I missed something?
__________________
Verion 4.7.12
Reply With Quote
  #2  
Old 08-18-2022, 05:30 AM
  cflsystems's Avatar 
cflsystems cflsystems is offline
 

Veteran
  
Join Date: Apr 2007
Posts: 14,190
 

Default Re: Front end (Customer) product search logic and search results - worse than X-Cart 4?

You shouldn't be doing this. Find the code that calls getData - this will pass to the function the conditions ($cnd variable). Find the function that sets conditions - there is one, I don't remember the name of it though. Decorate the function doing this in your module to get parent and add to conditions your new condition. This way getData will receive the adjusted $cnd variable and do the search as needed and run one query - you do not have to decorate the getData function..
__________________
Steve Stoyanov
CFLSystems.com
Web Development
Reply With Quote
  #3  
Old 08-18-2022, 09:52 PM
 
xcartdev2 xcartdev2 is offline
 

Advanced Member
  
Join Date: Nov 2011
Posts: 43
 

Default Re: Front end (Customer) product search logic and search results - worse than X-Cart 4?

Yes - I agree with you. It should be a module (and it will be) but just tinkering.



However this would not change the overall outcome. I would need to call GetData with byTitle set to 'Y' and then call it again with byTitle set to 'NULL'. That's two database calls.


Better would be one call which also collects all product info such as the name, description, attributes, etc. Then manipulate this array as needed.



The other issue is that getData is only called after the <head> area has been executed (it is called by getPageData). That's too late if you want to include specific info about a product (say the cheapest one) in the <title> or meta description.


So I'm thinking: how to call getPageData at the beginning (in the controller) and then change the twig so it only calls getPageData if a particular array (this.getPageData()) is empty (i.e. the function has not already been called)
__________________
Verion 4.7.12
Reply With Quote
  #4  
Old 08-19-2022, 04:14 AM
  cflsystems's Avatar 
cflsystems cflsystems is offline
 

Veteran
  
Join Date: Apr 2007
Posts: 14,190
 

Default Re: Front end (Customer) product search logic and search results - worse than X-Cart 4?

Im not sure what you are trying to do there and why do you have to call the query twice - with value and null.
You can do a search where "term" is in product name or product description. Then loop through the result and sort as needed. No need of 2 queries.
__________________
Steve Stoyanov
CFLSystems.com
Web Development
Reply With Quote
  #5  
Old 08-19-2022, 06:20 AM
 
xcartdev2 xcartdev2 is offline
 

Advanced Member
  
Join Date: Nov 2011
Posts: 43
 

Default Re: Front end (Customer) product search logic and search results - worse than X-Cart 4?

So what I am trying to do is for any search term:
- list products where the substring is in the product title first
- list products where the substring is elsewhere (like in the description) after the above.





If I do a getData (using the default code) it returns a list of products but that list does not contain the name, description, page title, meta description, etc.


As such I can't loop through these results to get places where the substring appears in the product title (in order to list those first) and then tag the left overs onto that array.


... or can I?
__________________
Verion 4.7.12
Reply With Quote
Reply
   X-Cart forums > X-Cart 5 > Dev Questions (X-Cart 5)


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -8. The time now is 12:41 PM.

   

 
X-Cart forums © 2001-2020