更改 WooCommerce 产品描述选项卡按钮和标题

当页面的主体类为“parent-product_cat-vinyl”时,我想将 WooCommerce 产品描述选项卡按钮和标题“描述”更改为“Tracklist”。


到目前为止,这是我的代码:


<?php

if ( is_singular() ) {

   $classes = get_body_class();

   if (in_array('parent-product_cat-vinyl',$classes)) {

       add_filter( 'woocommerce_product_description_tab_title','ps_rename_description_product_tab_label');

       function ps_rename_description_product_tab_label() {

           return 'Tracklist';

       }

   }

}

但这似乎不起作用。


九州编程
浏览 135回答 1
1回答

Qyouu

您可以使用以下复合过滤器挂钩($tab_key在您的情况下是description):woocommerce_product_{$tab_key}_tab_titlewoocommerce_product_{$tab_key}_heading可以通过两种方式完成:有条件地使用定义的主体类:add_filter( 'woocommerce_product_description_tab_title', 'change_product_description_tab_text' );add_filter( 'woocommerce_product_description_heading', 'change_product_description_tab_text' );function change_product_description_tab_text( $title ) {&nbsp; &nbsp; global $product;&nbsp; &nbsp; if( in_array( 'parent-product_cat-vinyl', get_body_class() ) ) {&nbsp; &nbsp; &nbsp; &nbsp; return __('Tracklist', 'woocommerce');&nbsp; &nbsp; }&nbsp; &nbsp; return $title;}或者有条件地针对产品类别(包括父产品类别):// Custom conditional function that handle parent product categories toofunction has_product_categories( $categories, $product_id = 0 ) {&nbsp; &nbsp; $parent_term_ids = $categories_ids = array(); // Initializing&nbsp; &nbsp; $taxonomy&nbsp; &nbsp; &nbsp; &nbsp; = 'product_cat';&nbsp; &nbsp; $product_id&nbsp; &nbsp; &nbsp; = $product_id == 0 ? get_the_id() : $product_id;&nbsp; &nbsp; if( is_string( $categories ) ) {&nbsp; &nbsp; &nbsp; &nbsp; $categories = (array) $categories; // Convert string to array&nbsp; &nbsp; }&nbsp; &nbsp; // Convert categories term names and slugs to categories term ids&nbsp; &nbsp; foreach ( $categories as $category ){&nbsp; &nbsp; &nbsp; &nbsp; $result = (array) term_exists( $category, $taxonomy );&nbsp; &nbsp; &nbsp; &nbsp; if ( ! empty( $result ) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $categories_ids[] = reset($result);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // Loop through the current product category terms to get only parent main category term&nbsp; &nbsp; foreach( get_the_terms( $product_id, $taxonomy ) as $term ){&nbsp; &nbsp; &nbsp; &nbsp; if( $term->parent > 0 ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $parent_term_ids[] = $term->parent; // Set the parent product category&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $parent_term_ids[] = $term->term_id; // (and the child)&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $parent_term_ids[] = $term->term_id; // It is the Main category term and we set it.&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true : false;}add_filter( 'woocommerce_product_description_tab_title', 'change_product_description_tab_text' );add_filter( 'woocommerce_product_description_heading', 'change_product_description_tab_text' );function change_product_description_tab_text( $title ) {&nbsp; &nbsp; global $product;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // Here set in the array the targeted product categories&nbsp; &nbsp; $categories = array('Vinyl');&nbsp; &nbsp; if ( has_product_categories( $categories, $product->get_id() ) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return __('Tracklist', 'woocommerce');&nbsp; &nbsp; }&nbsp; &nbsp; return $title;}代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并有效。
打开App,查看更多内容
随时随地看视频慕课网APP