Skip to content

Instantly share code, notes, and snippets.

@dustyf
Created June 25, 2013 20:26

Revisions

  1. dustyf created this gist Jun 25, 2013.
    54 changes: 54 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    <?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;
    }