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:
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:
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:
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:
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?
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');
}
<!-- custom_filename.php -->
at the top of all customization's, then religiously ensure filenames are accurate.) – zipzit Commented Jul 1, 2015 at 17:23comments_template_loader()
never runs to process the file so the add_filter never is called. – W00tW00t111 Commented Jul 1, 2015 at 17:42