hooks - WooCommerce comments_template Filter Not Firing

admin2025-06-02  1

I have a template file that is being called via wp_ajax that returns data from a specific WooCommerce product.

In the "tabs" section of the template file every tab is working fine:

  • Description - Good
  • Additional Information - Good
  • Custom Tab - Good

The only tab not rendering tab panel content is the reviews tab.

Interestingly the Tab Link (li) shows up with the correct tab count. However, when you click the link and the review panel is no longer display:none and set as active - no content is shown. (This has been verified in dev tools).

The code that is calling all of the individual tabs is:

<?php call_user_func( $tab['callback'], $key, $tab ) ?>

Where $tab is an array with:

  • title
  • priority
  • callback

The reviews tab calls the WooCommerce specified comments_template callback.

This is supposed to trigger the hook in class WC_Template_Loader:

add_filter( 'comments_template', array( __CLASS__, 'comments_template_loader' ) );

In testing I've var_dumped the WC_Template_Loader init function, and the init function, where the add_filters are located, is running.

However, when I var_dump a test string in the comments_template_loader() function, nothing is returned.

As an aside - all other WooCommerce generated content is rendered correctly (global $woocommerce, $post, $product are declared).

Why is this filter not running when seemingly all other default Wordpress tab filters are?

I have a template file that is being called via wp_ajax that returns data from a specific WooCommerce product.

In the "tabs" section of the template file every tab is working fine:

  • Description - Good
  • Additional Information - Good
  • Custom Tab - Good

The only tab not rendering tab panel content is the reviews tab.

Interestingly the Tab Link (li) shows up with the correct tab count. However, when you click the link and the review panel is no longer display:none and set as active - no content is shown. (This has been verified in dev tools).

The code that is calling all of the individual tabs is:

<?php call_user_func( $tab['callback'], $key, $tab ) ?>

Where $tab is an array with:

  • title
  • priority
  • callback

The reviews tab calls the WooCommerce specified comments_template callback.

This is supposed to trigger the hook in class WC_Template_Loader:

add_filter( 'comments_template', array( __CLASS__, 'comments_template_loader' ) );

In testing I've var_dumped the WC_Template_Loader init function, and the init function, where the add_filters are located, is running.

However, when I var_dump a test string in the comments_template_loader() function, nothing is returned.

As an aside - all other WooCommerce generated content is rendered correctly (global $woocommerce, $post, $product are declared).

Why is this filter not running when seemingly all other default Wordpress tab filters are?

Share Improve this question asked Jul 1, 2015 at 16:56 W00tW00t111W00tW00t111 573 silver badges12 bronze badges 2
  • This is a woocommerce template file located within your theme? Possible the native tools are grabbing the wrong file? (hint) I'm hoping you aren't changing a single byte of code anywhere within the Plugin Woocommerce directory. (hint: I've been burnt before working on the wrong custom template.. now I always include <!-- custom_filename.php --> at the top of all customization's, then religiously ensure filenames are accurate.) – zipzit Commented Jul 1, 2015 at 17:23
  • @zipzit The changes in the WC code are strictly to see what is running. The problem seems to be that the file is never being grabbed. I.e. the comments_template_loader() never runs to process the file so the add_filter never is called. – W00tW00t111 Commented Jul 1, 2015 at 17:42
Add a comment  | 

2 Answers 2

Reset to default 1

Okay, for anyone looking to include the WooCommerce review tab not on an actual single-product page here's the secret sauce:

In your template file, you need:

global $withcomments;
$withcomments = true;

Then add a filter for the comments template:

//add filter to use our custom review template
add_filter('comments_template','{{YOUR_HELPER_FUNCTION}}');

/**
* Hook function for using custom quickview review template
* @return [string] [file path to the template]
*/
function YOUR_HELPER_FUNCTION(){
    $template = LCS_DIR . "../templates/tmpl-single-product-reviews.php";
    if ( file_exists( $template ) )
        return( $template );
}

$template is the full file path to the template file you want to use.

BadaBing and you're done!

Alternatively, you could change the callback of the reviews tab to your own function which calls a custom template.

functions.php

add_filter( 'woocommerce_product_tabs', 'fpusa_custom_review_tab', 98 );
function fpusa_custom_review_tab( $tabs ) {
    // change the callback for the reviews tab
    $tabs['reviews']['callback'] = 'fpusa_get_comments_template';
    return $tabs;
}

function fpusa_get_comments_template(){
    // looks for a file in /yourtheme/woocommerce/single-product/customer_reviews.php
    wc_get_template('single-product/customer_reviews.php');
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748850224a314242.html

最新回复(0)