skip to Main Content

I have nested categories in my website.
I created a custom Field in Woocommerce category and I tried to Add it in category loop. It shows only the term value of the current category page

add_action( 'woocommerce_after_subcategory_title', 'custom_add_cat_Min_price', 12);

function custom_add_cat_Min_price ($category) {
 $prix_min_catt = get_term_meta(get_queried_object_id(), 'prix_min_cat', true);
 $terms = get_the_terms( $post->ID, 'prix_min_cat' );
 foreach ($terms as $term){
 echo '<div class="prixminofcatg">'.$prix_min_catt.'</div>';
 }
}

2

Answers


  1. Chosen as BEST ANSWER

    Thanks its working without Foreach

    add_action( 'woocommerce_after_subcategory_title', 'custom_addd_cat_Min_price', 29);
    
    function custom_addd_cat_Min_price ($category) {
        $category_id = $category->term_id;
     $prix_min_cag = get_term_meta($category_id, 'prix_min_cat', true);
     $terms = get_term( $category_id, 'prix_min_cat' );
     echo '<div class="prixminofcatg">'.$prix_min_cag.'</div>';
    }
    

  2. I think the problem is the scope of your function. You’ve passed the $category to your function, but haven’t used it. This gives you the ID of your category:

    function custom_add_cat_Min_price ($category) {
        $category_id = $category->term_id;
    

    and from there, you should be able to extract the custom fields.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search