Jump to content

[SOLVED]Question about how to change the default product category on link.


m33ts4k0z

Recommended Posts

Hello,

 

 

I'm using prestashop for quite some time now and I'm really happy with it. Recently I came accross a limitation that frustrates me a bit.

 

I'm having many categories that I made by myself. In these categories I have created like 80 products that are different with each other but will be exactly the same under each of those categories. That was the fastest solution since otherwise I would have to create each of those 80 products seperately for every category and it would take ages (the categories are about 100).

 

Now the thing is that on each order of those products, I need to know which category they were bought from. I achieved that by adding the name of the actual category in the product name by editing the orderdetails.php. That part works fine. However now we come to the interesting part.

 

I have modified the search.php so now I can also search for products using category names and that works fine too. The problem is that the results show the default category instead of the category the name of which i searched for. As you understand the problem is that if the customer orders the product, I wont know from which category he got it from and thats really important to me.

 

I'm using prestashop 1.5.5

 

Any solution, suggestions or workarounds? Everything is welcome.

 

Thanks in advance

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

Well answering my own question. I found a solution to this that satisfied me a lot.

 

What I simply did was to use session variables that I kept the category name and then I injected it into the product name inside the orderdetail.php

 

I have also modified the ajax script to include products by typing the first letters of the category name and that works great too.

 

Also I modified the find() function so it would return products that exist in the search category and finally with some tpl editing I can fake the product name by adding the category next to it.

 

This is partucullary helpful for anyone who wants to have category name but arent able to since prestashop only uses the default category in product links.

 

Below are the most significant snippets of my code in case anyone is interested:

 

on classes/search.php on line 218, add after:

 


$category_search = 'SELECT id_product
FROM '._DB_PREFIX_.'category_product  
WHERE id_category IN (SELECT id_category FROM '._DB_PREFIX_.'category_lang WHERE name LIKE 
'.($word[0] == '-'
? ' \''.pSQL(Tools::substr($word, 1, PS_SEARCH_MAX_WORD_LENGTH)).'%\''
: '\''.pSQL(Tools::substr($word, 0, PS_SEARCH_MAX_WORD_LENGTH)).'%\''
).')';
$category_name_search = 'SELECT name FROM '._DB_PREFIX_.'category_lang WHERE name LIKE '.($word[0] == '-'
? ' \''.pSQL(Tools::substr($word, 1, PS_SEARCH_MAX_WORD_LENGTH)).'%\''
: '\''.pSQL(Tools::substr($word, 0, PS_SEARCH_MAX_WORD_LENGTH)).'%\''
);
$sql_category = 'SELECT id_category FROM '._DB_PREFIX_.'category_lang WHERE name LIKE'.($word[0] == '-'
? ' \''.pSQL(Tools::substr($word, 1, PS_SEARCH_MAX_WORD_LENGTH)).'%\''
: '\''.pSQL(Tools::substr($word, 0, PS_SEARCH_MAX_WORD_LENGTH)).'%\''
);

search for:

foreach ($intersect_array as $query)

replace the whole foreach loop with the following code:

 

foreach ($intersect_array as $query)
{
$eligible_products2 = array();
if(count($db->executeS($query)) > 0)
{
foreach ($db->executeS($query) as $row)
$eligible_products2[] = $row['id_product'];
}
else
{
foreach ($db->executeS($category_search) as $row)
$eligible_products2[] = $row['id_product'];
session_start(); 
$_SESSION['actual_category_search'] = $db->getValue($category_name_search);
$_SESSION['is_search'] = true;
}


$eligible_products = array_intersect($eligible_products, $eligible_products2);
if (!count($eligible_products))
return ($ajax ? array() : array('total' => 0, 'result' => array()));
}

search for:

if ($ajax)

replace the whole if statement with:

 

if ($ajax)
{
$sql = 'SELECT DISTINCT p.id_product, pl.name pname, cl.name cname,
cl.link_rewrite crewrite, pl.link_rewrite prewrite '.$score.'
FROM '._DB_PREFIX_.'product p
INNER JOIN `'._DB_PREFIX_.'product_lang` pl ON (
p.`id_product` = pl.`id_product`
AND pl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('pl').'
)
'.Shop::addSqlAssociation('product', 'p').'
INNER JOIN `'._DB_PREFIX_.'category_lang` cl ON (
cl.`id_category` IN ('.$sql_category.')
AND cl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('cl').'
)
WHERE p.`id_product` '.$product_pool.'
ORDER BY position DESC LIMIT 10';
return $db->executeS($sql);
}

By making these modifications you achieved that both the ajax search and the normal search work by typing the category name. The results will display products that are under the category typed. Also you created session variables that will keep the category name until you destroy,unset or replace them.

 

Hope this helps someone.

Edited by m33ts4k0z (see edit history)
  • Like 1
Link to comment
Share on other sites

  • 6 months later...

Hello!!

 

Thanks for sharing! This works perfect, just a few things:

  • Is there a way to modify this in override instead of modifying directly?
  • Is it possible to use this code to show in search results page the different categories with their descriptions?

By the way, I understood you have product with the same name but in different categories? How are you doing SEO in your page?

 

Regards :)

Link to comment
Share on other sites

Hi Alvarosc,

 

You can make it an override by creating an empty file in override/classes/Search.php and create an overriding class:

<?php
class Search extends SearchCore
{
// add modified function(s) here...

}

and add the modified function to this file.

 

(Don't forget to throw away the file: cache/class_index.php and then reload the front page, otherwise the overridden class is not seen)

 

 

pascal

  • Like 1
Link to comment
Share on other sites

Hi Alvarosc,

 

You can make it an override by creating an empty file in override/classes/Search.php and create an overriding class:

<?php
class Search extends SearchCore
{
// add modified function(s) here...

}

and add the modified function to this file.

 

(Don't forget to throw away the file: cache/class_index.php and then reload the front page, otherwise the overridden class is not seen)

 

 

pascal

 

So thanks!! I'll try this, probably I won't solve it, but I'll try it  :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...