I'm having some issue's trying to display the term from a custom taxonomy in WooCommerce.
Currently, I already utilise WooCommerce Product Categories, so I created a custom taxonomy called sale_type
function add_custom_taxonomies() {
// Add new "Sale Type" taxonomy to Posts
register_taxonomy('sale_type', 'product', array(
// Hierarchical taxonomy (like categories)
'hierarchical' => true,
// This array of options controls the labels displayed in the WordPress Admin UI
'labels' => array(
'name' => _x( 'Sale Type', 'taxonomy general name' ),
'singular_name' => _x( 'Sale Type', 'taxonomy singular name' ),
'search_items' => __( 'Search Sale Type' ),
'all_items' => __( 'All Sale Type' ),
'parent_item' => __( 'Parent Sale Type' ),
'parent_item_colon' => __( 'Parent Sale Type:' ),
'edit_item' => __( 'Edit Sale Type' ),
'update_item' => __( 'Update Sale Type' ),
'add_new_item' => __( 'Add New Sale Type' ),
'new_item_name' => __( 'New Sale Type Name' ),
'menu_name' => __( 'Sale Type' ),
),
// Control the slugs used for this taxonomy
'rewrite' => array(
'slug' => 'sale-type', // This controls the base slug that will display before each term
'with_front' => false, // Don't display the category base before "/locations/"
'hierarchical' => true // This will allow URL's like "/locations/boston/cambridge/"
),
));
}
add_action( 'init', 'add_custom_taxonomies', 0 );
Then, In my failed attempt to display the term name of a custom taxonomysale_type
on the single product page, I used the wp_get_post_terms function similarly to how I would do with 'product_cat'.
Here's an example:
<span class="badge">
<?php
$terms = wp_get_post_terms(get_the_id(), 'sale_type');
$term = reset($terms);
echo $term->name;
?>
</span>
<span class="badge"><?php $terms=wp_get_post_terms(get_the_id(),'product_cat');$term=reset($terms);echo $term->name; ?></span>
The output was nothing. So first, I done the obvious and checked for the 'sale_type' custom taxonomy metabox and verify that a term is selected. Then I attempted to use get_the_terms instead of wp_get_post_terms - and then also done a var dump which Debugging Output = array(0) { }
Debugging post meta for the product, I can see all the various custom fields, but I don't see the 'sale_type' taxonomy in this output.
I have never really used custom taxonomy before, so I'm now curious if that 'sale_type' might be stored differently or not stored at all in the post meta. So, is there a different approach to get the custom taxonomy terms directly?
I have tried your WooCommerce product custom taxonomy and it works… Now I have made some little changes to your displaying code. Try the following:
$term_names = wp_get_post_terms( get_the_ID(), 'sale_type', array('fields' => 'names') );
if ( ! empty($term_names) ) {
printf('<span class="badge">%s</span>', implode(' ', $term_names));
}
The code display correctly the term name(s) if they are set on a product…
get_the_id()
to get_the_ID()
array('fields' => 'names')
as argument to wp_get_post_terms()
to get directly the term names array,implode()
instead of reset()
,IF
statement to avoid the display if there are no term names set for a product…For display testing, I have used the following for single product pages (on the meta section):
add_action( 'woocommerce_product_meta_end', 'single_pproduct_display_sale_type_test', 10 );
function single_pproduct_display_sale_type_test(){
global $product, $post;
$term_names = wp_get_post_terms( get_the_ID(), 'sale_type', array('fields' => 'names') );
if ( ! empty($term_names) ) {
printf('<p class="sale_type">%s: <strong>%s</strong></p>', get_taxonomy('sale_type')->labels->name, implode(' ', $term_names));
}
}
Code goes in functions.php file of your child theme (or in a plugin). Tested and works.