$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>hooks - WooCommerce comments_template Filter Not Firing|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

hooks - WooCommerce comments_template Filter Not Firing

matteradmin8PV0评论

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');
}
Post a comment

comment list (0)

  1. No comments so far