简体   繁体   English

在 Woocommerce 单个产品页面中显示自定义分类

[英]Display a custom taxonomy in Woocommerce single product pages

I have added a new taxonomy called "Vendor" to Woocommerce with the following code:我使用以下代码向 Woocommerce 添加了一个名为“供应商”的新分类法:

// 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.我有一个子主题,并且知道我必须在子主题中创建一个 woocommerce 文件夹,然后将我必须编辑的 woo 模板文件的副本添加到该文件夹​​中。

Can anyone help me?谁能帮我?

  1. What woo template files must I edit?我必须编辑哪些 woo 模板文件?
  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.更新:经过进一步研究,我似乎不需要编辑 Woo 模板文件。

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.要在 Woocommerce 单个产品页面的元部分中显示自定义分类术语中的帖子术语,您无需覆盖任何 Woocommerce 模板。

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).代码位于活动子主题(或活动主题)的 function.php 文件中。 Tested and works.测试和工作。

It gives me an error:它给了我一个错误:

FATAL ERROR syntax error, unexpected '$taxonomy' (T_VARIABLE), expecting ')' on line number 7致命错误语法错误,第 7 行出现意外的“$taxonomy”(T_VARIABLE),需要“)”

IN: if( ! taxonomy_exists( string $taxonomy ) )在:如果(!taxonomy_exists(字符串$taxonomy))

How can I solve that?我该如何解决? Custom taxonomies are not visible in the product_meta of my single product page自定义分类在我的单个产品页面的 product_meta 中不可见

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.无法与站点通信以检查致命错误,因此 PHP 更改已恢复。 You will need to upload your PHP file change by some other means, such as by using SFTP.您需要通过其他方式上传您的 PHP 文件更改,例如使用 SFTP。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 WooCommerce 产品存档页面上显示自定义分类术语 - Display custom taxonomy terms on WooCommerce product archive pages 在 Woocommerce 单个产品页面中显示特定产品标签的自定义内容 - Display custom content for specific product tags in Woocommerce single product pages 在 Woocommerce 的单个产品页面上显示特定的自定义产品属性 - Display specific custom product attributes on single product pages in Woocommerce 在Woocommerce单个产品页面中显示高于产品价格的自定义字段 - Display a custom field above product price in Woocommerce single product pages 显示自定义字段吹产品标题Woocommerce产品单页 - Display custom field blow product title Woocommerce product single pages 如何显示WooCommerce产品的自定义分类法? - How to display custom taxonomy of the WooCommerce product? 自定义分类图像-输出到单个产品页面 - Custom taxonomy images - output to single product pages 在Woocommerce单页中无法购买产品时显示自定义消息 - Display a custom message when a product is not purchassable in Woocommerce single pages 通过钩子在 WooCommerce 单个产品页面中显示自定义字段值 - Display custom field value in WooCommerce single product pages via hook 在 Woocommerce 单个产品页面的简短描述下显示自定义字段 - Display a custom field under short description in Woocommerce single product pages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM