Refresh

This website jigoshop.com/forum-sub/kb/faq/adding-product-category-terms-in-a-dropdown-box-for-custom-post-meta/ is currently offline. Cloudflare's Always Online™ shows a snapshot of this web page from the Internet Archive's Wayback Machine. To check for the live version, click Refresh.

Please refer to TemplateMonster Support for any issues surrounding TemplateMonster/TemplateTuning Themes – thanks, Jigoshop Team

Adding Product Category Terms in a dropdown box for Custom Post Meta

Q: I am creating a small record store site which Jigoshop will work perfectly for but I am a little stumped on how to achieve one element the client wants. Under the store area, records will be listed by artist so I will use categories for each artist. They also want an artists page for each artist. For this I am going to create a custom post type of artists. Under the artists bio, the client wants an “All records by [artists name]” section which will pull in all the artists releases.

I know there is a shortcode to display products by category slug but my client isn’t the most savvy and I was hoping to avoid having them use this. I thought about adding a custom field where the client could simply enter the slug by text and then insert it into the necessary call to pull in the records in the template file. This should be farely simple if I tell them to just write the artist name in lowercase and use a hyphen instead of a space eg “the-beatles” or “the-flaming-lips”. Only trouble with this is if somehow the category name is added incorrectly or duplicated at some point so the slug is incorrect.

The best option would be to somehow pull in a dropdown box of all available product categories that the user can select the category (or artist) from in a custom field. This would then return the necessary category slug or id into the template file to pull in all products in the category.

The trouble is I have no idea how to do this…

A: The best way is to create your own function like below. It’s an exact dupe of jigoshop_product_dropdown_categories() with the subtle difference that it disables the term ordering set by jigoshop elsewhere:

    function mytheme_product_dropdown_categories( $show_counts = true, $hierarchal = true ) {
            global $wp_query;
            $r = array();
            $r['pad_counts'] = 1;
            $r['hierarchal'] = $hierarchal;
            $r['hide_empty'] = 1;
            $r['show_count'] = 1;
            $r['selected']   = isset( $wp_query->query['product_cat'] ) ? $wp_query->query['product_cat'] : '';
            $r['menu_order'] = false; // This is to enable the following order args
            $r['orderby'] = 'id';
            $r['order'] = 'ASC';
            $terms = get_terms( 'product_cat', $r );
            if ( ! $terms ) return;
            $output  = "<select name='product_cat' id='dropdown_product_cat'>";
            $output .= '<option value="" ' .  selected( isset( $_GET['product_cat'] ) ? esc_attr( $_GET['product_cat'] ) : '', '', false ) . '>'.__('Select a category', 'jigoshop').'</option>';
            $output .= jigoshop_walk_category_dropdown_tree( $terms, 0, $r );
            $output .="</select>";
            echo $output;
    }