skip to Main Content

I would like to add "Category: " text before category link (in the same line) on Woocommerce single product page. Currently user (specially older users I am aiming my page to) may be confused what the link is. Example below (marked in yellow):

enter image description here

Currently code looks like this, inside "summary entry-summary" div:

<span class="single-product-category">
    <a href="https://www.store.domain/category-name/" rel="tag">bielizna ochronna</a>
</span>

Is it doable without editing template?

3

Answers


  1. Chosen as BEST ANSWER

    Ok, I thought I will maybe share my "workaround" (as I don't like adding content to page via CSS); however still counting on some bright idea how to do it differently ;-)

    .single-product-category:before {
      content: "Kategoria: ";
    }
    

  2. functions.php add the bottom code

    add_filter( 'woocommerce_product_single_category_title', 'add_text_before_category_link', 10, 2 );
    
    function add_text_before_category_link( $category_title, $category ) {
        // Replace "Extra Text" with the text you want to add before the category link
        return 'Extra Text '. $category_title;
    }
    

    Save the changes in the functions.php file.
    Refresh single product page to see the updated category link.

    Login or Signup to reply.
  3. add_action( 'woocommerce_single_product_summary', 'add_category_text', 1 );
    function add_category_text() {
    global $product;
    $terms = get_the_terms( $product->get_id(), 'product_cat' );
    if ( ! empty( $terms ) ) {
        $term = array_pop( $terms );
        echo '<p>' . __('Category: ', 'woocommerce') . '<a href="' . get_term_link( $term ) . '">' . $term->name . '</a></p>';
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search