Skip to content

Instantly share code, notes, and snippets.

@dustyf
Created June 25, 2013 20:26
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save dustyf/5862035 to your computer and use it in GitHub Desktop.
Save dustyf/5862035 to your computer and use it in GitHub Desktop.
Woo Commerce Dropdown to filter product tag within an archive page.
<?php
function displayLocationDropdown() {
$html = '';
$html .= '<form class="location-select" method="post">';
$html .= '<select id="location-selector" name="location" class="location">';
$tag = wp_tag_cloud( array(
'format' => 'array',
'taxonomy' => 'product_tag'
) );
$page_path = $_SERVER["REQUEST_URI"];
$page_url = site_url() . $page_path;
$url_parts = parse_url($page_url);
$constructed_url = $url_parts['scheme'] . '://' . $url_parts['host'] . (isset($url_parts['path'])?$url_parts['path']:'');
$html .= '<option value="#">--Select Location--</option>';
foreach($tag as $tagkey => $tagvalue)
{
$cleanedup = strip_tags($tagvalue);
$tag_info = get_term_by('name', $cleanedup, 'product_tag', 'ARRAY_A');
$tag_slug = $tag_info['slug'];
if(isset($_GET['product_tag'])) {
$value_url = $constructed_url . '?product_tag=' . $tag_slug;
} else {
$value_url = $page_url . '?product_tag=' . $tag_slug;
}
$html .= '<option value="' . $value_url . '">' . $cleanedup . '</option>';
}
$html .= '<option value="' . $constructed_url . '">View All Locations</option>';
$html .= '</select>';
$html .= '</form>';
echo $html;
}
add_action('woocommerce_before_shop_loop', 'displayLocationDropdown', 40);
add_action('wp_head', 'addDisplayLocationScript');
function addDisplayLocationScript() {
$script = '';
$script .= '<script type="text/javascript">';
$script .= 'jQuery(document).ready(function() {';
$script .= ' jQuery("#location-selector").change(function() {';
$script .= ' location = jQuery("#location-selector option:selected").val();';
$script .= ' });';
$script .= '});';
$script .= '</script>';
echo $script;
}
@WebCOLT
Copy link

WebCOLT commented Oct 14, 2014

Hi buddy, how can I use your script?

@autumned
Copy link

@WebCOLT Add it to function.php in your theme folder.

@hardikdashora
Copy link

hardikdashora commented Jul 25, 2016

Hi,
Great code. I wanted to know how can I convert this code into shortcode so that I can place it wherever I want.

Thanks.

@mrkkr
Copy link

mrkkr commented Apr 26, 2018

This is nice solution! But in #30 line I replaced $value_url to site_url(), because I had duplicate in URL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment