WP / WC调用add_action" init"来自另一个功能

时间:2015-09-14 14:56:28

标签: php wordpress woocommerce

我的插件和WooCommerce有问题。

所以我有一个带有选项页面的插件,上面有一个自定义复选框。

如果激活此复选框,我想隐藏/删除默认的WooCommerce相关产品容器。

如果我只是添加此代码,我可以删除此容器:

    add_action( 'init', 'add_action_function');

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

但问题是我需要从另一个内部调用此函数" add_filter"功能

此刻我有这样的事情:

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

但是当我更改admin-settings中的选项时,没有任何变化。所以我认为" init"钩子不在这里。

我无法找到合适的钩子来完成这项工作。当我希望它在插件选项更新时触发时,我必须使用什么钩子?

先谢谢了, 沫

感谢Danijel和他的回答。

我不知道为什么我没想到这一点。 也许这是很多"行动"在那个深夜对我来说;)

我现在放置了" add_action"在" add_filter"之外功能,只需在那里进行条件检查。

这是有效的:

            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 个答案:

答案 0 :(得分:2)

我非常肯定在init过滤器之前触发了WP woocommerce_after_single_product_summary操作,并且if ( $active = 'yes' { ...表达式始终会被评估为true(使用==) 。试试这个简单的例子:

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

答案 1 :(得分:1)

尝试将add_action_funciton移到add_filter_function

之外
add_filter( 'woocommerce_after_single_product_summary', 'add_filter_function' );
function add_action_function(){
   remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
}

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');
    }//END if $active = yes
  // Do some other stuff here
}//END add_filter_function
相关问题