doulang7699 2016-07-03 16:42
浏览 51
已采纳

使用现有钩子的函数不能使用自定义do_action钩子

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条回答 默认 最新

  • doutuan4361 2016-07-03 17:56
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置