Append current category name to product title on woocommerce category archives

Qwertzui11

I would like to display the current category in the product name in woocommerce. What only applies for variable products.

The code below is partly working, but need some reforming:

1 - to be applied for only for variable products.

2 - to show the current category that the viewer is in not the main product category.

remove_action('woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10);
function loop_title() { 
global $post;?>
<div class="col-xl-3 col-md-3 col-sm-3">
    <h2><a href="<?php the_permalink(); ?>" class="feed-item-baslik"><?php the_title(); ?> <?php 
    $terms = get_the_terms( $post->ID, 'product_cat' );
    if ( $terms && ! is_wp_error( $terms ) ) :
        if ( ! empty( $terms ) ) {
            echo $terms[0]->name;
        }?>
    <?php endif;?></a></h2>

</div>
<?php }
add_action('woocommerce_shop_loop_item_title', 'loop_title', 10);
LoicTheAztec

Updated: The following should work as you are expecting:

add_action( 'woocommerce_shop_loop_item_title', 'custom_shop_loop_item_title', 5 );
function custom_shop_loop_item_title() {
    global $product;

    if ( $product->is_type('variable') && is_product_category() ) {
        remove_action('woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10);
        add_action('woocommerce_shop_loop_item_title', 'custom_product_loop_title', 10);
    }
}

function custom_product_loop_title() {
    $taxonomy       = 'product_cat';
    $queried_object = get_queried_object();
    ?>
    <div class="col-xl-3 col-md-3 col-sm-3">
        <h2><a href="<?php the_permalink(); ?>" class="feed-item-baslik"><?php the_title();
        // For product category archives pages
        if( is_a($queried_object, 'WP_Term') && $queried_object->taxonomy === $taxonomy ) {
            echo ' ' . $queried_object->name;
        }
        ?></a></h2>
    </div>
    <?php
}

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

WooCommerce - Remove product title from breadcrumbs but keep all category hyperlinks

Get a list of siblings term ids from current product category in WooCommerce

WooCommerce: Display Category Name

Add active class to woocommerce product category current-menu-item

Change product link on archives pages for a specific product category in WooCommerce

Exclude a product category from custom displayed button on Woocommerce archives

Get the product category name and description in Woocommerce Single Product page

Add product title heading tag for WooCommerce category archive pages

Display product attributes on specific Woocommerce product category archives page

Check a product category for a product in Woocommerce

Show WooCommerce product attributes in custom home and product category archives

Display description before the title in WooCommerce product category archives

Get the subcategories of the current product category in Woocommerce archives

How to get all products from current WooCommerce product category?

Get the category name from a WooCommerce product using the product id or the category id

Woocommerce: Product Category Widget

how to get category name of current product (on product detail page) in magento

Woocommerce Product child category from current parent category

Change WooCommerce product category title?

Woocommerce - add last category name to product name

Get on sale products for Woocommerce product category archives using a Get request

Add a tag class to the current Woocommerce product category in a navigation menu

Woocommerce: How to show Product Attribute name on title when in a category page and "filtering" products via '?pa_attribute=' on address bar

Woocommerce - How to alter product category name with hook?

Display loop product category items description below their title in WooCommerce

WooCommerce current category hide

Woocommerce - Displaying only the parent category under the product title

Wordpress / Woocommerce, how to access current product loop Query on category page

Woocommerce - Display the product category under product title in shop/archive page