Create a Dropdown of Product Categories in Woocommerce

A recent project demanded that I create a drop-down list of Woocommerce product categories for an admin page in a plugin I was creating. Here is the PHP and HTML code that accomplished the task:

<form method=”post” action=””>
<?php $args1 = array(‘taxonomy’  => ‘product_cat’); ?>
  <?php wp_dropdown_categories( $args1 ); ?>
<input type=”submit” name=”cat_sel” value=”SELECT PRODUCT CATEGORY” />
</form><hr />

Retrieve Selected Category in Form Processing Script

Now to retrieve the value, you will use the post variable named cat like so:

$chosen_cat = $_POST[‘cat’];

…and that will return a product ID. If you need a product name, here is cool PHP function I found that returns the post name/slug when feeding the function the ID we just stored in $chosen_cat above:

function get_product_category_by_id( $category_id ) {
    $term = get_term_by( 'id', $category_id, 'product_cat', 'ARRAY_A' );
    return $term['name'];
$product_category = get_product_category_by_id( $your_category_id );


Leave a Reply

Your email address will not be published. Required fields are marked *