1
votes

I know that add_action is used to call a function at the location of a hook. I used add_action like below and it gives me the output I need (Which is the price of a product in WooCommerce):

add_action( 'woocommerce_before_variations_form', 'woocommerce_single_variation', 10 );

I used the above code inside a custom plugin that i built.

But now I want the price to appear in a different location. I read about do_action and I learned that it is used to make new hooks.

So I made a new hook like below -

do_action('unique_mycustom_hook');

The new hook is kept inside "content-single-product.php" inside Woocommerce template folder.

And then I called my function at my new hook using the code below inside my plugin file.

add_action( 'unique_mycustom_hook', 'woocommerce_single_variation', 10 );

But this time I am NOT getting any output!

Do you have any idea why add_action worked in the pre-existing woocommerce hook but did not work in my new hook made using do_action?

Thanks.


UPDATE - I am adding more information on request.

The purpose of my hook in woocommerce is to have the price of the product appear in a fixed sidebar. So I made a <div> section inside the file "content-single-product.php" inside WooCommerce template folder. This is the exact code that I added to the woocommerce template.

<div class="this_sidebar_is_fixed">
   <?php
      do_action('unique_mycustom_hook');
   ?>
</div>

Then I am calling the hook using a custom plugin I have.

Still it is not working.

1
Please the best thing is to include all the code of the template with your do_action() inside in the desired location… I can't help only with that information.LoicTheAztec
Dude but you wont believe it, i RESOLVED my problem only with your comment. I just had to move the do_action from content-single-product.php to variable.php and now its working! But can you maybe post an answer with the reason why it worked? I am new to hooks and filters.Adam
Accepted that answer. Thanks brother.Adam

1 Answers

1
votes

Because location is also important depending on what you are hooking.
woocommerce_before_variations_form is located on single-product/add-to-cart/variable.php template inside a form.

So for example if you place your unique_mycustom_hook outside this form (or in another template), it will not work. That are the main reasons… They can be others too.

---- updated ----

Inside content-single-product.php your do_action('unique_mycustom_hook'); can't work, because it needs to be located inside the form in single-product/add-to-cart/variable.php