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 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程