Dirk Vervaet
Dirk Vervaet

Reputation: 13

Change WooCommerce product description tab button and title

I want to change the WooCommerce product description tab button and title "Description" into "Tracklist" when the page has a body class of "parent-product_cat-vinyl".

Here is my code so far:

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

But it doesn't seem to work.

Upvotes: 1

Views: 1497

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 254271

You can use the following composite filter hooks (where $tab_key is in your case description):

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

It can be done in 2 ways:

Conditionally with a defined body class:

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

Or conditionally for a product category (including parent product categories):

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

    if( is_string( $categories ) ) {
        $categories = (array) $categories; // Convert string to array
    }

    // Convert categories term names and slugs to categories term ids
    foreach ( $categories as $category ){
        $result = (array) term_exists( $category, $taxonomy );
        if ( ! empty( $result ) ) {
            $categories_ids[] = reset($result);
        }
    }

    // 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;
}

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

Upvotes: 0

Related Questions