Home > database >  Add relative URL to related products title on WooCommerce single product page
Add relative URL to related products title on WooCommerce single product page

Time:04-21

It would make a lot more sense if the related products title linked to more of the related products being displayed...

Attempting to change the related products title on the single product page to a link to the end category that correlates to the related products query being displayed.

I have the url slug and name for the end category I just can't escape the <h2> tag to turn it into a link. Any advice?

$translated = '<a href="#"> </a>'.esc_html__(end($term_urls).'Continue Browising '.end($term_names), $domain);

add_filter('gettext', 'change_rp_text', 10, 3);
add_filter('ngettext', 'change_rp_text', 10, 3);

function change_rp_text($translated, $text, $domain)
{
global $woocommerce, $post;

 if ($text === 'Related products' && $domain === 'woocommerce') {
     
 $term_names = wp_get_post_terms( $post->ID, 'product_cat', array('fields' => 'names') );
        $term_urls = wp_get_post_terms( $post->ID, 'product_cat',
            array('fields' => 'slugs') );
     
     $translated = '<a href="#"> </a>'.esc_html__(end($term_urls).'Continue Browising '.end($term_names), $domain);
 
 }
 return $translated;
 
 }

CodePudding user response:

Normally you could use the woocommerce_product_related_products_heading filter hook, which allows you to change $heading. But $heading is passed via esc_html() so you can't add HTML to the output.

Therefore you will have to overwrite the /single-product/related.php file

This template can be overridden by copying it to yourtheme/woocommerce/single-product/related.php.

Replace line 29 - 32 @version 3.9.0

if ( $heading ) :
    ?>
    <h2><?php echo esc_html( $heading ); ?></h2>
<?php endif; ?>

With

if ( $heading ) {
    global $product;
    
    // Is a WC product
    if ( is_a( $product, 'WC_Product' ) ) {
        // Get terms
        $terms = wp_get_post_terms( $product->get_id(), 'product_cat' );
        $end = end( $terms ); 

        // URL
        echo '<a href="' . get_term_link( $end->term_id, 'product_cat' ) . '">' . $end->name . '</a>';
    }
}
?>

CodePudding user response:

This appears to work with out having to copy and modify the /single-product/related.php file. It could be more efficient.

Using get_term_link( from the answer @7uc1f3r provided

add_filter('gettext', 'change_rp_text', 10, 3);
add_filter('ngettext', 'change_rp_text', 10, 3);

function change_rp_text($translated, $text, $domain)
{
global $woocommerce, $post;


 if ($text === 'Related products' && $domain === 'woocommerce') {

        $term_names = wp_get_post_terms( $post->ID, 'product_cat',
            array('fields' => 'names') );
        $term_urls = wp_get_post_terms( $post->ID, 'product_cat',
            array('fields' => 'slugs') );
        $term_ids = wp_get_post_terms( $post->ID, 'product_cat',
            array('fields' => 'ids') );
     

     $last_term_id = end($term_ids);

     $translated = _e('<h2><a href="' . get_term_link( $last_term_id, 'product_cat' ) . '">More ' . end($term_names) . '</a></h2>', $domain);

 }
 return $translated;


}
  • Related