Home Forums General Issues Getting custom field from woocommerce category in home page


Getting custom field from woocommerce category in home page

  • i have create custom field (textfield) in woocommerce category, which im trying to get in home where i have display woocommerce category with thumbnails and name in loop, which lists all woocommerce category in home page, but im unable to display acf custom field create in category within this loop so that is easily display acf custom field with this category loop.

    acf custom field for woocomerce category

    when i paste following code in woocommerce shop page (archive-product.php) i easily get acf custom field but when i paste same code in home page i get nothing.

    <?php $product_cat_object = get_queried_object(); ?>
    <?php the_field('custom_link', 'product_cat_' . $product_cat_object->term_id); ?>

    Category loop code which i used for getting woocommerce category with thumbnail and title:

        $prod_categories = get_terms( 'product_cat', array(
            'orderby'    => 'name',
            'order'      => 'ASC',
            'hide_empty' => 1
        foreach( $prod_categories as $prod_cat ) :
            $cat_thumb_id = get_woocommerce_term_meta( $prod_cat->term_id, 'thumbnail_id', true );
            $cat_thumb_url = wp_get_attachment_thumb_url( $cat_thumb_id );
            $term_link = get_term_link( $prod_cat, 'product_cat_' );
    		$term_id = get_queried_object()->term_id;
    	<img src="<?php echo $cat_thumb_url; ?>" alt="" class="w-100 d-block"/>
    	<h4><?php echo $prod_cat->name; ?></h4>
    <?php endforeach; wp_reset_query(); ?>

    I need to display acf custom-field create in woocommerce category in this loop..
    so, can anyone share the knowledge how can i do that……

  • HI.
    I solve a similar problem, only with tags.
    I searched for a solution for almost 2 hours (I didn’t find it), I didn’t find any examples, but I managed to solve my problem myself!
    If it helps:
    is_main_tag – field name
    product_tag_ – suffix (described at the bottom of the page
    $post_id = false; // current post
    $post_id = 1; // post ID = 1
    $post_id = “user_2”; // user ID = 2
    $post_id = “category_3”; // category term ID = 3
    By analogy, he suggested and tried his type (woocommerce) of taxonomy and helped!

    $ value = get_field (“is_main_tag”, ‘product_tag _’. $ id);

    In your case, it will most likely help:
    $ value = get_field (“name_of_your_field”, ‘product_cat_’. $ id);

    I hope this helps you!
    Good luck colleague!

  • thank for the reply,but i have already solve it next i post it…., your solution is bit similar, i just added following code in my loop and it did the magic.

        $category = get_term_by('slug', $prod_cat->name ,'product_cat'); $custom_field = get_field('your_custom_field', $category );
        echo $custom_field;
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Getting custom field from woocommerce category in home page’ is closed to new replies.