Created
June 25, 2013 20:26
-
-
Save dustyf/5862035 to your computer and use it in GitHub Desktop.
Woo Commerce Dropdown to filter product tag within an archive page.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 Add it to function.php in your theme folder.
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.
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
Hi buddy, how can I use your script?