How to unhook a function in Woocommerce Template?

I have a basic question that has always stumped me. I’m trying to move the Star Rating into the comment meta, which will help me style it. Here’s the template file review.php section:

<div class="comment-text">

           <?php
           /**
            * The woocommerce_review_before_comment_meta hook.
            *
            * @hooked woocommerce_review_display_rating - 10 //Want to unhook this.
            */
           do_action('woocommerce_review_before_comment_meta', $comment);

           /**
            * The woocommerce_review_meta hook.
            *
            * @hooked woocommerce_review_display_meta - 10
            */
           do_action('woocommerce_review_meta', $comment);

           do_action('woocommerce_review_before_comment_text', $comment);

           /**
            * The woocommerce_review_comment_text hook
            *
            * @hooked woocommerce_review_display_comment_text - 10
            */
           do_action('woocommerce_review_comment_text', $comment);

           do_action('woocommerce_review_after_comment_text', $comment);
           ?>

       </div>

I was thinking I could add the following to my functions.php:

    remove_action('woocommerce_review_display_rating', 10);

And add it back in the review-meta.php, but I can’t remove / unhook the star rating. What am I doing wrong?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You need to unhook the function from that action specifically:

remove_action( 'woocommerce_review_before_comment_meta',
               'woocommerce_review_display_rating', 10 );

(e.g. look for the opposite add_action() line in includes/wc-template-hooks.php)

You’ll also need to make sure that this is run after WooCommerce has loaded and added the action you’re about to remove, e.g. place this in a plugins_loaded hook:

function unhook_display_rating() {
    remove_action( 'woocommerce_review_before_comment_meta',
                   'woocommerce_review_display_rating', 10 );
}
add_action( 'plugins_loaded', 'unhook_display_rating', 10, 0 );

Method 2

remove_action requires that the second parameter is the name of the function/callable that was added, and the priority must match what was added.

The official documentation says its parameters are:

remove_action( string $tag, callable $function_to_remove, int $priority = 10 )

You can use also remove_all_actions:

remove_all_actions( 'hook name' );

Remember, hooks are like events, you can’t remove something that hasn’t been added yet, so when you call this function matters, not just how it’s called.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x