Add a List of Context-Sensitive Brands to Your Sidebar in Datafeedr
Adding a plain brand list to Datafeedr is fairly straight forward.
In Datafeedr, there is 1 main table that is being used for the shop – depending on your WordPress setup the name for this may change, but generally speaking it is called: wp_dfr_shop_products
This table contains one row per product and has tons of other useful columns include name, brand, merchant, network, description, tags, specs, image and more.
Below, I show you how to add a context sensitive list of brands to your sidebar. This brand list will change, depending on which category page the user views or depending on what search term the user has entered.
In this example, I’ve presumed the search term in use is ‘Word’ but quite often it makes sense to use the ‘?name=xyz’ parameter instead for searches as it makes your search results more accurate, although with the unfortunate side-effect of dropping some less relevant results completely. (I’ll cover improving the search results for Datafeedr in a separate article later, depending on demand)
Create a new widget
First, create a new widget from the Datafeedr factory and call it ‘Context Brand Filter’. Add a ‘Text block’ and paste the following code:
[level-member]
Filter By Brand
id, $awdword);
?>
[/level-member]
Notice the function call above to ‘awd_getbrands’ – this is what is going to construct our filtered brand list.
The brand list is going to be filtered based on the current category and the current search term. Note: this brand list cannot be used on the search results page as it stands as there would be no categoryid available, so this is currently for use with the category pages only.
To add the function awd_getbrands you need to edit your functions.php file. You’ll find this using FTP (I recommend FileZilla) – look in the root folder of your active theme – that’s where functions.php normally exists.
Add this function to the end of functions.php.
function awd_getbrands($catid, $word) {
global $wpdb;
$catid = mysql_real_escape_string($catid);
$word = mysql_real_escape_string($word);
$params = "";
if (@$_GET['word']) {
$params = "?word=".@$_GET['word']."&"; // when a user clicks to filter by brand the 'WORD' parameter should stay on if already used
}
else {
$params = "?";
}
$brands = $wpdb->get_results("select if(brand = '', merchant, brand) as brand, if(brand = '', 0, 1) as brandused,
count(*) as numproducts
from wp_dfr_shop_products as p
inner join wp_dfr_shop_p2c as p2c
on p.id = p2c.id
where p2c.cat_id = $catid
and (
name like '%{$word}%'
or description like '%{$word}%'
or tags like '%{$word}%'
)
group by if(brand = '', merchant, brand), if(brand = '', 0, 1)
order by brand asc;
");
$html = "";
foreach ($brands as $brands) {
if ($brands->brandused == 1) {
$html .= '
Once you have added the code, save your changes and ensure they have uploaded to your server.
Log in to your WordPress admin area, go to Widgets and add the Datafeedr widget and change the drop down option to ‘Context Brand Filter’. Once your widget has been added, go test it out.
- How to create a tree of categories from a custom taxonomy - September 22, 2014
- Managing postcodes and locations with WPAllImport - September 22, 2014
- Why do affiliate merchants reject your website? - August 20, 2014
November 25, 2012 @ 4:11 am
i need professional help with my website and datafeedr are your services for hire..
thank you,
frank
November 27, 2012 @ 11:07 am
Hi Frank – yes, our services are available and we are highly rated by our customers.
To get started, please fill out the form here: http://www.affiliatewebdesigners.com/hire-affiliate-web-designers/
March 20, 2013 @ 4:58 am
I added the code to my functions.php file and it said the theme has been corrupted and website crashed. I took the code away and the website is ok again.
March 21, 2013 @ 4:55 pm
Hi – I’ve emailed you some info – can you send me a copy of your completed functions.php file and I’ll check it for errors?
September 27, 2013 @ 8:43 pm
This won’t work if you have another prefix than wp_ in the database. Would use $wpdb->prefix instead of hardcoding wp_.
Just my 2 cents, otherwise good job 🙂
October 9, 2013 @ 3:09 pm
Thanks – yeah, that’s just pure laziness on my part, but well noticed and thanks for the useful feedback.
November 18, 2013 @ 6:51 pm
Do you have some demos from your plugins/code?