View Single Post
  #21  
Old October 21st, 2014, 09:25 AM
Eric's Avatar
Eric Eric is offline
Datafeedr Team
 
Join Date: Feb 2008
Posts: 16,902
Default

Here's how to add a <select> dropdown menu for brands, merchants and networks which gets filtered by category This filters to 1 category level deep so it's not a perfect solution for filtering per category. If you are not comfortable editing PHP or HTML code, this tutorial might not be for you.

You don't have to use them all but you can if you want. Here's what it will look like:



1. Go here: The Factory > Your Store > VIEWS > WIDGETS > Add a new view

2. Name the view "Drop Down Filters"

3. Add a Text module to the view

4. Add the following code to the Text module:

HTML Code:
<form method="get">
  
	<!-- Brand drop down select menu -->
	<select name="brand">
		<?php echo dfr_get_brands($category->id); ?>
	</select>
	
	<!-- Merchant drop down select menu -->
	<select name="merchant_id">
		<?php echo dfr_get_merchants($category->id); ?>
	</select>
	
	<!-- Network drop down select menu -->  
	<select name="net_id">
		<?php echo dfr_get_networks($category->id); ?>
	</select>
	
	<input type="submit" value="Go" />
  
</form>
5. Save the view.

6. Open your theme's functions.php file (usually found here: /wp-content/themes/YOUR-THEME/functions.php)

7. Add the following PHP code to the bottom of the file:

PHP Code:
function dfr_get_brands$cat=false ) { 
    global 
$wpdb
       
$cat_query = ($cat) ? "WHERE p2c.cat_id = ".intval($cat)." " "";
    
$brands $wpdb->get_results("     
        SELECT brand, p2c.cat_id 
        FROM "
.$wpdb->prefix."dfr_shop_products AS p 
        INNER JOIN "
.$wpdb->prefix."dfr_shop_p2c AS p2c ON p.id = p2c.id 
        
$cat_query 
        GROUP BY brand 
    "
); 
    
$html '<option value="">Select a brand</option>'
    foreach (
$brands as $brands) { 
        if (
trim($brands->brand) != "") { 
            
$selected = (strip_tags(stripslashes($_GET['brand']))==$brands->brand) ? ' selected="selected"' ''
            
$html .= '<option value="'.$brands->brand.'"'.$selected.'>'.$brands->brand.'</option>'
        } 
    } 
    return 
$html
}

function 
dfr_get_merchants$cat=false ) {
    global 
$wpdb;
       
$cat_query = ($cat) ? "WHERE p2c.cat_id = ".intval($cat)." " "";
    
$merchants $wpdb->get_results("
        SELECT merchant, merchant_id, p2c.cat_id 
        FROM "
.$wpdb->prefix."dfr_shop_products AS p 
        INNER JOIN "
.$wpdb->prefix."dfr_shop_p2c AS p2c ON p.id = p2c.id 
        
$cat_query 
        GROUP BY merchant
    "
);
    
$html '<option value="">Select a merchant</option>';
    foreach (
$merchants as $k => $v) {
        if (
trim($v->merchant) != "") {
            
$selected = (strip_tags($_GET['merchant_id'])==$v->merchant_id) ? ' selected="selected"' '';
            
$html .= '<option value="'.$v->merchant_id.'"'.$selected.'>'.$v->merchant.'</option>';
        }
    }
    return 
$html;


function 
dfr_get_networks$cat=false ) {
    global 
$wpdb;
       
$cat_query = ($cat) ? "WHERE p2c.cat_id = ".intval($cat)." " "";
    
$networks $wpdb->get_results("
        SELECT network, net_id, p2c.cat_id 
        FROM "
.$wpdb->prefix."dfr_shop_products AS p 
        INNER JOIN "
.$wpdb->prefix."dfr_shop_p2c AS p2c ON p.id = p2c.id 
        
$cat_query 
        GROUP BY net_id
    "
);
    
$html '<option value="">Select a network</option>';
    foreach (
$networks as $k => $v) {
        if (
$v->net_id 0) {
            
$selected = (strip_tags($_GET['net_id'])==$v->net_id) ? ' selected="selected"' '';
            
$html .= '<option value="'.$v->net_id.'"'.$selected.'>'.$v->network.'</option>';
        }
    }
    return 
$html;

8. Save the functions.php file.

9. Add the new "Drop Down Filters" widget to your sidebar here: WordPress Admin Area > Appearance > Widgets

That's it.

Eric