简体   繁体   中英

Display a custom taxonomy in Woocommerce single product pages

I have added a new taxonomy called "Vendor" to Woocommerce with the following code:

// hook into the init action and call taxonomy when it fires

add_action( 'init', 'create_vendor_taxonomy', 0 );

// create and register vendor taxonomy (hierarchical)

function create_vendor_taxonomy() {

    $labels = array(
        'name'              => _x( 'Vendors', 'taxonomy general name', 'textdomain' ),
        'singular_name'     => _x( 'Vendor', 'taxonomy singular name', 'textdomain' ),
        'search_items'      => __( 'Search Vendors', 'textdomain' ),
        'all_items'         => __( 'All Vendors', 'textdomain' ),
        'parent_item'       => __( 'Parent Vendor', 'textdomain' ),
        'parent_item_colon' => __( 'Parent Vendor:', 'textdomain' ),
        'edit_item'         => __( 'Edit Vendor', 'textdomain' ),
        'update_item'       => __( 'Update Vendor', 'textdomain' ),
        'add_new_item'      => __( 'Add New Vendor', 'textdomain' ),
        'new_item_name'     => __( 'New Vendor Name', 'textdomain' ),
        'menu_name'         => __( 'Vendors', 'textdomain' ),
    );

    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'vendor' ),
    );

    register_taxonomy( 'vendor', array( 'product' ), $args );

}

I want to insert this new taxonomy between the Category and Tags labels that appear on a single product page.

I have a child theme and understand that I must create a woocommerce folder in the child and then add to that folder a copy of the woo template files that I must edit.

Can anyone help me?

  1. What woo template files must I edit?
  2. What code do I need to add to these files to insert my new taxonomy into the product page?

Thanks in advance for any kind assistance.

UPDATE: Upon further research it would appear that I do not need to edit the Woo template files.

There is a hook available just below the Category and Tags meta on the single product page. That will do the job.

So I can insert the Vendor taxonomy details with the following:

add_action( 'woocommerce_product_meta_end', 'insert_vendor_custom_action', 5 );

function insert_vendor_custom_action() {
    global $product;
    if [WHAT DO I NEED HERE?]
    echo [WHAT DO I NEED HERE?];
}

Thanks to anyone who can help me out.

To display the post terms from a custom taxonomy terms in meta section of Woocommerce single product page, you don't need to override any Woocommerce template.

Instead you can use the specific dedicated action hook this way:

add_action( 'woocommerce_product_meta_end', 'action_product_meta_end' );
function action_product_meta_end() {
    global $product;

    $taxonomy = 'vendor'; // <== 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(), 'vendor', '<span class="posted_in">' . _n( 'Vendor:', 'Vendors:', count( $term_ids ), 'woocommerce' ) . ' ', ', ', '</span>' );
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

It gives me an error:

FATAL ERROR syntax error, unexpected '$taxonomy' (T_VARIABLE), expecting ')' on line number 7

IN: if( ! taxonomy_exists( string $taxonomy ) )

How can I solve that? Custom taxonomies are not visible in the product_meta of my single product page

Thanks for the code. unfortunately for me, it did not solve my problem. here is the error code I received. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM