Paste this code to functions.php end echo anywhere you want. You can also make shortcode with: add_shortcode( 'NAME TO DISPLAY', 'displayLocationDropdown' )'.
Please also look at comments.
';
$html .= '';
$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 .= '--Select Location-- ';
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'])) { /// I am not sure if it is necessery
$value_url = $constructed_url . '?product_tag=' . $tag_slug;
} else {
$value_url = $page_url . '?product_tag=' . $tag_slug;
}
/// (this prevent duplicate in URL in my case) $value_url = site_url() . '?product_tag=' . $tag_slug;
$html .= '' . $cleanedup . ' ';
}
$html .= 'View All Locations ';
$html .= ' ';
$html .= '';
echo $html;
}
add_action('woocommerce_before_shop_loop', 'displayLocationDropdown', 40);
add_action('wp_head', 'addDisplayLocationScript');
function addDisplayLocationScript() {
$script = '';
$script .= '';
echo $script;
}