dongtu0363 2015-09-14 14:56
浏览 71
已采纳

WP / WC从另一个函数内部调用add_action“init”

I have a problem with a plugin and WooCommerce.

So I have a plugin with an options page, and a custom checkbox on it.

When this checkbox is activated, I want to hide/remove the default WooCommerce related product container.

I can remove this container if I just add this code:

    add_action( 'init', 'add_action_function');

    function add_action_function(){
        remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
    }

But the problem is that I need to call this function from inside another "add_filter" function.

In the moment I have something like this:

add_filter( 'woocommerce_after_single_product_summary', 'add_filter_function' );

function add_filter_function () {

    // Get the plugin option
    $active = get_option( 'prfx_active', 'no');

    // If option value is "yes", remove the related products container
    if ($active = 'yes') {

        // I think this add_action call is wrong
        add_action( 'init', 'add_action_function');

        function add_action_function(){
           remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
        }

    }//END if $active = yes

  // Do some other stuff here

}//END add_filter_function

But when I change the option in the admin-settings, nothing changes. So I think the "init" hook is not right here.

I cant find the right hook to make this work. What hook must I use when I want it to fire when the plugin options get updated?

Thanks in advanced, Mo


Thanks to Danijel and his answers.

I dont know why I didnt think of it this way. Maybe this was to much "action" for me on that late evening ;)

I now placed the "add_action" outside of the "add_filter" function and just do the conditional-check there.

This is working:

            add_action( 'init', 'hide_related');
            function hide_related () {

                if ( get_option( 'prfx_active', 'no' ) == 'yes' ) {
                    remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
                }

            };

add_filter( 'woocommerce_after_single_product_summary', 'add_filter_function' );
function add_filter_function () {
    ...
  • 写回答

2条回答 默认 最新

  • duanchi8836 2015-09-14 15:15
    关注

    Im quite certain that WP init action is fired before woocommerce_after_single_product_summary filter, and also if ( $active = 'yes' { ... expression will always be evaluated as true ( use == ). Try with this simple example:

    add_action( 'init', function() {
        if ( get_option( 'prfx_active', 'no' ) == 'yes' )
            remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路