Jump to content

Output the value from Search.php query to product-list.tpl in Prestashop


Recommended Posts

I need to output in product-list.tpl file the result of a query from within classes/Search.php in Prestashop. The query in classes/Search.php is:
$sql = 'SELECT *
     FROM `'._DB_PREFIX_.'category_group` cg
     INNER JOIN `'._DB_PREFIX_.'category_product` cp ON cp.`id_category` = cg.`id_category`
     INNER JOIN `'._DB_PREFIX_.'category` c ON cp.`id_category` = c.`id_category`
     INNER JOIN `'._DB_PREFIX_.'product` p ON cp.`id_product` = p.`id_product`
     INNER JOIN `'._DB_PREFIX_.'product_part_number` ON p. `id_product` = `ID`
     '.Shop::addSqlAssociation('product', 'p', false).'
     WHERE c.`active` = 1
     AND product_shop.`active` = 1
     AND product_shop.`visibility` IN ("both", "search")
     AND product_shop.indexed = 1
     AND cg.`id_group` '.(!$id_customer ?  '= 1' : 'IN (
     SELECT id_group FROM '._DB_PREFIX_.'customer_group 
     WHERE id_customer = '.(int)$id_customer.'
    
     )');
     $results = $db->executeS($sql);
    
 
 This is outputting a series of "GREKA" values in the `'._DB_PREFIX_.'product_part_number` table at the top of the search results page from the query above:
            $eligible_products = array();
        
         foreach ($results as $row){
         $eligible_products[] = $row['id_product'];
         echo $row['GREKA'];
         }
What I need it to do is output the "GREKA" value within the tpl file themes/product-list.tpl for that product. This tpl file is using Smarty to output the results of the product search, I need to know how to call the "GREKA" value from the query to the foreach loop. I imagine it would be something like this:

           {foreach from=$products item=product name=products}
                 {$GREKA}
           {/foreach}

I have tried to put this line of code inside the foreach on the product-list.tpl and it gives me no output , it gives me an error: 

 

          Code:

  • {$product.GREKA}
  • Error:

     Notice: Undefined index: GREKA

 

Link to comment
Share on other sites

Hello,

 

- This is a programming (development) related question, so I hope the moderators will move it to the appropriate place.

 

- At a first glance, the query doesn't seem to select anything from your product_part_number table, though I didn't run the query on my own. Could you please comfirm that $results already contains those "GREKA" values right in the Search::find() method, before being sent to the template? Can you see them when you add a print_r() or var_dump() on your Search override class? If so, the next step would probably be to check the variables passed to Smarty.

Link to comment
Share on other sites

If I understand your question well, below is the actual code from the search controller.

$this->context->smarty->assign(array(
'products' => $search['result'], // DEPRECATED (since to 1.4), not use this: conflict with block_cart module
'search_products' => $search['result'],
'nbProducts' => $search['total'],
'search_query' => $query,
'instant_search' => $this->instant_search,
'homeSize' => Image::getSize(ImageType::getFormatedName('home'))));
and the value for $search comes from the Search::find() method, a few lines upper, as below:
$search = Search::find($this->context->language->id, $query, 1, 10, 'position', 'desc');
  So, you will probably want to extend the find() method's result to cover your GREKA values, and then, pass it thru Smarty by adding another item to the array such as:

$this->context->smarty->assign(array(
'products' => $search['result'], // DEPRECATED (since to 1.4), not use this: conflict with block_cart module
'search_products' => $search['result'],
'nbProducts' => $search['total'],
'search_query' => $query,
'instant_search' => $this->instant_search,
'greka' => $search['greka'],
'homeSize' => Image::getSize(ImageType::getFormatedName('home'))));
...or so.
Edited by prestashopninja (see edit history)
Link to comment
Share on other sites

To be more clear:

- If you want, you can start first by trying to pass something static to Smarty. For example:

$this->context->smarty->assign(array(
'products' => $search['result'], // DEPRECATED (since to 1.4), not use this: conflict with block_cart module
'search_products' => $search['result'],
'nbProducts' => $search['total'],
'search_query' => $query,
'instant_search' => $this->instant_search,
'greka' => 'test value',
'homeSize' => Image::getSize(ImageType::getFormatedName('home'))));

and try to get it the greka value as 'test value' from within Smarty template.

 

Once you do it, next will be to get this greka value dynamic. To do that, return back to the Search class where you can manipulate the result. from:

return array('total' => $total,'result' => $result_properties);

to (for example) :

return array('total' => $total,'result' => $result_properties , 'grekaValues' => $grekaValues);

where $grekaValues should be populated using your query result.

This way, when you call

$search = Search::find($this->context->language->id, $query, 1, 10, 'position', 'desc');

You will now find the greka values as $search['grekaValues'] and you can pass them thru Smarty with ease.

 

PS: I assume you will be doing all these, not altering the core code, but overriding them. 

Edited by prestashopninja (see edit history)
Link to comment
Share on other sites

it wont even do it with a straight value ... im doing this on the product-list.tpl 

{$product.greka}

 

Following my example above, the static greka value should be accessed as {$greka}, not {$product.greka} .

You can see what's passed to Smarty by adding

 

{debug}

at the top of your .tpl file.

Once you can see the static value of greka, then you can itertate and add the relevant greka values to each $product and then acces them as {$product.greka}

Link to comment
Share on other sites

As I clearly stated above, this should be defined as follows:

 


 

Once you do it, next will be to get this greka value dynamic. To do that, return back to the Search class where you can manipulate the result. from:

return array('total' => $total,'result' => $result_properties);

to (for example) :

return array('total' => $total,'result' => $result_properties , 'grekaValues' => $grekaValues);

 

I think you now have everything to sort out this issue. Please feel free to ask anything if you won't already find the response within what is written above.

Link to comment
Share on other sites

now it is saying it is undefined on this line :

return array('total' => $total,'result' => $result_properties , 'grekaValues' => $grekaValues);

when i do this code it gives me the GREKA values at the top of the screen ... how would i set this to the return array ?

echo $row['GREKA'];
Link to comment
Share on other sites

I know the template file is working because it will get the static "test value" , but when we set it to the $search it is blank because grekaValues is undefined on line 337:

return array('total' => $total,'result' => $result_properties , 'grekaValues' => $grekaValues);
Link to comment
Share on other sites

×
×
  • Create New...