更改WooCommerce产品描述标签按钮和标题

时间:2020-07-22 09:30:32

标签: php wordpress woocommerce tabs product

当页面的正文类为“ parent-product_cat-vinyl”时,我想将WooCommerce产品描述选项卡按钮和标题“描述”更改为“跟踪列表”。

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

<?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';
       }
   }
}

但这似乎不起作用。

1 个答案:

答案 0 :(得分:0)

您可以使用以下复合过滤器挂钩(在您的情况下,$tab_keydescription):

  • woocommerce_product_{$tab_key}_tab_title
  • woocommerce_product_{$tab_key}_heading

可以通过两种方式完成:

1)。有条件的具有定义的身体类别:

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 ) {
    global $product;

    if( in_array( 'parent-product_cat-vinyl', get_body_class() ) ) {
        return __('Tracklist', 'woocommerce');
    }
    return $title;
}

或有条件地针对产品类别(包括母产品类别):

// Custom conditional function that handle parent product categories too
function has_product_categories( $categories, $product_id = 0 ) {
     // Initializing
    $parent_term_ids = $categories_ids = array();
    $taxonomy        = 'product_cat';

    if( is_string( $categories ) ) {
        $categories = (array) $categories;
    }

    $product_id = $product_id > 0 ? get_the_id() : $product_id;

    // Convert categories term names and slugs to categories term ids
    foreach ( $categories as $category ){
        if( is_numeric( $category ) ) {
            $categories_ids[] = (int) $category;
        } elseif ( term_exists( sanitize_title( $category ), $taxonomy ) ) {
            $categories_ids[] = get_term_by( 'slug', sanitize_title( $category ), $taxonomy )->term_id;
        }
    }

    // Loop through the current product category terms to get only parent main category term
    foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
        if( $term->parent > 0 ){
            $parent_term_ids[] = $term->parent; // Set the parent product category
            $parent_term_ids[] = $term->term_id; // (and the child)
        } else {
            $parent_term_ids[] = $term->term_id; // It is the Main category term and we set it.
        }
    }
    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 ) {
    global $product;
    
    // Here set in the array the targeted product categories
    $categories = array('Vinyl');

    if ( has_product_categories( $categories, $product->get_id() ) ) {
         return __('Tracklist', 'woocommerce');
    }
    return $title;
}

代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。