You are here:  » List categories under a certain merchant


List categories under a certain merchant

Submitted by johan_norlund on Sun, 2006-05-21 21:34 in

Hello David and thanks SO much for putting this amazing script together!

This is what I want to do: I want to be able to list all the categories of a certain merchant. Is there any way to accomplish this?

Like: display all categories WHERE merchant = [entered value] ;)

I searched the forums for an answer to this, but didn't find anything relevant.

Thanks heaps!
Johan

Submitted by support on Sun, 2006-05-21 21:52

Hi Johan,

The SQL for the category list would be:

SELECT DISTINCT(category) as category FROM products WHERE merchant = 'some merchant' ORDER BY category"

Probably the easiest way to implement this would be to modify categories.php to accept a merchant name on the URL, for example:

/categories.php?merchant=Some+Merchant

And then at the top of the code, you would base the query on whether or not $_GET["merchant"] is set:

<?php
if ($_GET["merchant"])
{
  
$sql "SELECT DISTINCT(category) as category FROM
    `"
.$config_databaseTablePrefix."products`
    WHERE merchant = '"
.database_safe($_GET["merchant"])."'
    ORDER BY category"
;
}
else
{
  
$sql "SELECT DISTINCT(category) as category FROM
    `"
.$config_databaseTablePrefix."products`
    ORDER BY category"
;
}
// rest of code
?>

Let me know if you need a hand with creating a script to generate links to that page for each merchant. It would be a bit tricky to integrate it in the existing Merchant A-Z as it uses the common atoz.php script - so the easiest thing to do may be to modify merchants.php to use an exclusive copy of atoz.php that also includes a link to the merchant specific category page...

Hope this helps!
Cheers,
David.

Submitted by johan_norlund on Sun, 2006-05-21 22:25

THAT is what I call a quick reply!

Right now, the code you supplied is everything I need, but I'll post again if I need any further assistance.

Perhaps in a future release you could implement this feature (showing "View this merchant's categories"-links on the merchant A-Z page) and make it possible to turn it on/off in the admin area? Just a suggestion:)

Anyway, thank you again for the wonderful support!
Best regards
Johan

Submitted by johan_norlund on Mon, 2006-06-26 16:19

Hello again!

I've been working on my site for quite a while and have (among other things) integrated the above code to be able to list the categories of a specified merchant. Everything works like a treat!

Now I wonder if this is possible: When I click one of the merchant-specific categories (for example, "DVD players") I'd like to list ONLY the products of that very merchant. If two of my merchants both have the category "DVD players" the search results otherwise would show the products from both merchants. What do you think? My first idea was to modify search.php myself, but I'm quite a PHP newbie so perhaps you can give me some ideas?

Thanks heaps!
Johan

Submitted by support on Tue, 2006-06-27 07:56

Hi Johan,

There's 2 steps here. First, you need to propagate the merchant name through from the category index to the search page; and then modify the search script to limit the query by merchant if one has been indicated.

In categories.php (as modified as per previous instructions), you would need to make changes to the section of code that constructs the link to search.php, which currently looks like this:

<?php
  
if ($config_useRewrite)
  {
    
$item["href"] = tapestry_hyphenate($product["category"])."/";
  }
  else
  {
    
$item["href"] = "search.php?q=category:".urlencode($product["category"]).":";
  }
?>

The first change is that if you want to propagate the merchant name into the search script then you cannot use the search engine friendly URL, and so a test for $_GET["merchant"] should be added to the test for $config_useRewrite. The second change is that the merchant name needs to be included in the URL constructed for $item["href"]...

<?php
  
if ($config_useRewrite && !$_GET["merchant"])
  {
    
$item["href"] = tapestry_hyphenate($product["category"])."/";
  }
  else
  {
    
$item["href"] = "search.php?q=category:".urlencode($product["category"]).":&merchant=".urlencode($_GET["merchant"]);
  }
?>

With that in place, you can now test for $_GET["merchant"] in search.php at the point where the query is constructed for a "category:" search. Find the following code in search.php:

<?php
  
case "merchant":
    
// pass through to category
  
case "category":
    
// pass through to brand
  
case "brand":
    
$where " ".$parts[0]."='".database_safe($parts[1])."' ";
?>

..and change the order around, with some additional code in the category: case as follows:

<?php
  
case "category":
    if (
$_GET["merchant"]) $where " merchant = '".database_safe($_GET["merchant"])."' AND ";
    
// pass through
  
case "merchant":
    
// pass through
  
case "brand":
    
$where " ".$parts[0]."='".database_safe($parts[1])."' ";
?>

That should do the trick....

Cheers,
David.

Submitted by johan_norlund on Wed, 2006-06-28 10:38

Thanks for the detailed reply! ..Though the code mod seems to have no effect on the search results. I tried loading a free product feed from Animal Den into price tapestry, and searching with this URL:

http://localhost/search.php?q=category:BirdsCrane:&merchant=animalden

This lists all products in category "BirdsCrane". But when I try changing the merchant name in the URL to (for instance):

http://localhost/search.php?q=category:BirdsCrane:&merchant=aaanimalden

...the search results page still lists all the products in that category. In this case, shouldn't the script return a "no results found" since there are no merchant with the name "aaanimalden" ?

Cheers
Johan

Submitted by support on Wed, 2006-06-28 12:40

Oooops....

The last line should have been $where .= to append the where clause; so it was having no effect.... Here's the corrected version:

<?php
  
case "category":
    if (
$_GET["merchant"]) $where " merchant = '".database_safe($_GET["merchant"])."' AND ";
    
// pass through
  
case "merchant":
    
// pass through
  
case "brand":
    
$where .= " ".$parts[0]."='".database_safe($parts[1])."' ";
?>

Cheers,
David.

Submitted by johan_norlund on Wed, 2006-06-28 14:45

Perfect! One last thing: When search results return more than one page, and I click to go to the next page, the "merchant=some+merchant"-string disappears from the URL, thus giving me "Undefined index: merchant" and "Undefined variable: where" error messages.

I guess a modification of navigation.php is needed, right?

Submitted by support on Wed, 2006-06-28 19:33

Yes, the merchant name will need to be added to any follow up URLs that are constructed on that page. These exist in html/navigation.php and also the re-order links that are constructed in search.php

I think the easiest way to do this, rather than posts lots of modifications; you can do a search and replace using your text editor. In both of the above files, if you search for:

q=".urlencode($q)."

and replace with:

q=".urlencode($q)."&amp;merchant=".urlencode($_GET["merchant"])."

...this should have the effect of fixing up each link type in the appropraite way.

Hope this helps!
David.

Submitted by badger on Mon, 2006-10-02 22:26

What if instead of

display all categories WHERE merchant = [entered value]

we wanted to

display all results WHERE price

Submitted by James Warren on Wed, 2006-10-25 14:16

Hi i love the sound of this mod. I have read through and just can't follow what to change and where!
Would someone be able to write this in a step by step Guide?
I hope i'm not asking too much?

Regards

James Warren