0

have added a new taxonomy called "collection" to Woocommerce with the code below. I want to insert this new taxonomy between the Category and Tags labels that appear on a single product page. I have tried several examples on StackExchange but they seem not to work for me.

add_action( 'init', 'custom_taxonomy_Item' );

// Register Custom Taxonomy
function custom_taxonomy_Item() {

$labels = array(
'name' => 'Collections',
'singular_name' => 'Collection',
'menu_name' => 'Collections',
'all_items' => 'All Collections',
'parent_item' => 'Parent Collection',
'parent_item_colon' => 'Parent Collection:',
'new_item_name' => 'New Collection Name',
'add_new_item' => 'Add New Collection',
'edit_item' => 'Edit Collection',
'update_item' => 'Update Collection',
'separate_items_with_commas' => 'Separate Collection with commas',
'search_items' => 'Search Collection',
'add_or_remove_items' => 'Add or remove Collection',
'choose_from_most_used' => 'Choose from the most popular Collection',
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'exclude_from_search'   => false,
'supports'              => array( 'title', 'editor', 'custom-fields', 'thumbnail', 'excerpt', 'revisions', 'page-attributes' ) 
);
register_taxonomy( 'collection', 'product', $args );
}

1 Answer 1

0

Disclaimer: I don't have enough rep on this site to add comments asking for more details so I'll try to add a proper answer without ...

Assuming your custom taxonomy DOES work, and you have added at least 1 of them to your products...

The next step is to edit your meta template for the single product page.

  1. Copy/paste the /wp-content/plugins/woocommerce/templates/single-product/meta.php to /wp-content/*your_theme*/woocommerce/single-product/meta.php

Note: If you're using a child theme.. Then this goes into your child-theme folder. Not the parent theme.

For reference on everything going on in that page see your Single product's template file at woocommerce/templates/content-single-product.php. You can overwrite this one too if needed.

  1. Add this to your functions.php file:
function custom_product_meta_tax() {
    global $product;

    $taxonomy = 'collection'; // <== Here set your custom taxonomy

    if( ! taxonomy_exists( string $taxonomy ) ) 
        return; // exit
    
    $term_ids = wp_get_post_terms( $product->get_id(), $taxonomy, array('fields' => 'ids') );

    if ( ! empty($term_ids) ) {
        echo get_the_term_list( $product->get_id(), 'collection', '<span class="posted_in">' . _n( 'Collection:', 'Collections:', count( $term_ids ), 'woocommerce' ) . ' ', ', ', '</span>' );
    }
}
  1. In your meta.php (step 1), add this between the categories and the tags (line 35ish):
<?php custom_product_meta_tax(); ?>

That should do the trick. I did not test this manually but should work. If it doesn't, let me know and please add your code to these other files so I can see where the issue may be.

3
  • Thanks for code Cato. Unfortunately, it does not work. I received the error below after pasting the code in option 2. Here is the error. '"Unable to communicate back with site to check for fatal errors, so the PHP change was reverted. You will need to upload your PHP file change by some other means, such as by using SFTP.""
    – Rostilupt
    Dec 10, 2021 at 12:34
  • Oh wow ok. So, are you trying to do this change in the theme editor in the dashboard panel? I would assume that's the case. Are you doing this on a production site? If that is the case, do you have SFTP / cpanel access to your website? Just let me know if you do, and I will test the proposed solution for you before you make any changes to a production site. I could use the points ha
    – Cato
    Dec 10, 2021 at 23:55
  • Yes. I'm using the child theme PHP functions to make the changes from the admin dashboard. I also have SFTP access to my website.
    – Rostilupt
    Dec 13, 2021 at 7:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.