X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   Dev Questions (X-Cart 5) (https://forum.x-cart.com/forumdisplay.php?f=56)
-   -   Front end (Customer) product search logic and search results - worse than X-Cart 4? (https://forum.x-cart.com/showthread.php?t=78447)

xcartdev2 08-18-2022 04:07 AM

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?

cflsystems 08-18-2022 05:30 AM

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..

xcartdev2 08-18-2022 09:52 PM

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)

cflsystems 08-19-2022 04:14 AM

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.

xcartdev2 08-19-2022 06:20 AM

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?


All times are GMT -8. The time now is 11:38 AM.

Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.