PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, May 9, 2022

[FIXED] how to get current product category name in woocommerce

 May 09, 2022     php, product, taxonomy-terms, woocommerce, wordpress     No comments   

Issue

In WooCommerce single produce page I'm using some custom fields but one tab (see attachment picture right side tab 'Residential') I'd like to get the product category, which only one will be ticked. But I'm very unsure what code is needed to get that?

Image: https://ibb.co/XJ7FvmT

enter image description here

Any help to extract one product category name would be much appreciated.

The product (a property) will only have one category. Ie. Land, Residential, Commercial, Industry etc.

Thanks

Here is some code from image that's from the add_action in functions.php

    <!-- Advanced Custom Feilds-->            
<span class="property-contract-badge"><?php echo get_field('listing_status'); ?></span>
<span class="property-type-badge"><?php echo get_field('property_category'); ?></span>

<!-- Get Woocommerce product category -->        
<span class="property-type-badge"><?php echo $product->get_the_terms( $post->ID, 'product_cat'); ?></span>

Solution

Try the below code.

<span class="property-type-badge">
    <?php   
        $terms = wp_get_post_terms( get_the_id(), 'product_cat' );
        $term  = reset($terms);
        echo $term->name;
    ?>
</span>


Answered By - Bhautik
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing