-
-
Save mrkkr/9e0b4076067a273eb8754b0787734a50 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>'; ///site_url() instead $constructed_url is probably better | |
$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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment