Inspired by Make tag show as dropdown in woocommerce
This allowed me to add a dropdown to WooCommerce to show all my record labels artists in it. I modified it a little bit.
I have already found to have the "VIEW ALL" option show up as the top results by move the show all option to before the foreach loop that creates the options for the different labels.
My question: Is it possible to add a "VIEW ALL A-Z" to order all results alphabetically to the list? I haven't found a code that would actually work on my site to order by tags so that's why I'm trying to achieve this via this code.
This is my code already slightly modified:
function displayArtistDropdown() {
$html = '';
$html .= '<form class="artist-select" method="post">';
$html .= '<select id="artist-selector" name="artist" class="artist">';
$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 ARTIST</option>';
$html .= '<option value="' . $constructed_url . '">View All Artists</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'])) { /// 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 .= '<option value="' . $value_url . '">' . $cleanedup . '</option>';
}
$html .= '</select>';
$html .= '</form>';
echo $html;
}
add_action('woocommerce_before_shop_loop', 'displayArtistDropdown', 40);
add_action('wp_head', 'addDisplayArtistScript');
function addDisplayArtistScript() {
$script = '';
$script .= '<script type="text/javascript">';
$script .= 'jQuery(document).ready(function() {';
$script .= ' jQuery("#artist-selector").change(function() {';
$script .= ' location = jQuery("#artist-selector option:selected").val();';
$script .= ' });';
$script .= '});';
$script .= '</script>';
echo $script;
}