<?php
global $wp_query;
$current_cat_name = $wp_query->get_queried_object()->slug;
if (is_product()) {
echo '<input type="hidden" name="product_cat" value="'. $current_cat_name .'" />';
} elseif (is_product_category()) {
echo '<input type="hidden" name="product_cat" value="'. $current_cat_name .'" />';
} else {
echo '';
}
?>