global $product;
$product->get_id();
$id = 42;
if( $term = get_term_by( 'id', $id, 'product_cat' ) ){
echo $term->name;
}
$terms = get_the_terms( $product_id, 'product_cat' );
foreach ($terms as $term) {
$product_cat_id[] = $term->term_id;
}
global $post;
$link = '';
$terms = get_the_terms( $post->ID, 'product_cat' );
if(!empty($terms[0])){
$link = get_term_link( $terms[0]->term_id, 'product_cat' );
}
$p_array=['12','16', '21', '17'];
$prod_array= array();
foreach( $p_array as $key=>$value) {
$term_object = get_term( $value );
$cat_name= $term_object->slug;
//echo $cat_name;
$all_ids = get_posts( array(
'post_type' => 'product',
'numberposts' => -1,
'post_status' => 'publish',
'fields' => 'ids',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $cat_name,
'operator' => 'IN',
)
),
) );
foreach ( $all_ids as $id ) {
array_push($prods_array, $id);
}
}
print_r($prod_array);
// Testing that $_GET['product-cato'] has been requested and that the product category exists.
if( isset( $_GET['product-cato'] ) && term_exists( intval($_GET['product-cato']), 'product_cat' ) ){
// Get the corresponding WP_Term object
$term = get_term( intval($_GET['product-cato']), 'product_cat' );
//Display the term name for the product category
echo '<p>' . $term->name . '</p>';
}