Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WooCommerce 'remove_action' not working in 'init' hook

I am trying to modify the WooCommerce product filter plugin to add the filter as a <div class="row"> instead of "col-md-6".

In functions.php, I have removed the actions which hook into functions that create col-md-6, written new functions that create rows, and added actions that hook into my new functions. See code below.

function filter_styling()
{
    echo '<div class="row">';             
} 

function filter_styling2()
{
    echo '</div><div class="row">';            
}

function product_category_filter_changes()
{
    remove_action('woocommerce_before_main_content','oxy_before_breadcrumbs', 19);
    remove_action('woocommerce_before_main_content', 'oxy_after_breadcrumbs', 20);
    add_action('woocommerce_before_main_content', 'filter_styling', 19);  
    add_action('woocommerce_before_main_content', 'filter_styling2', 20);
}
add_action('init','product_category_filter_changes',10);

The add actions are registering, but not the remove actions. Any ideas?

Thanks! Dan

like image 519
DLateef Avatar asked Dec 15 '22 12:12

DLateef


1 Answers

Removing an action can only be done when an action has already been added. Because of this, the 'init' hook is likely too early.

I recommend using the 'template_redirect' action hook, which will run after the plugins are loaded:

function product_category_filter_changes()
{
    remove_action('woocommerce_before_main_content','oxy_before_breadcrumbs', 19);
    remove_action('woocommerce_before_main_content', 'oxy_after_breadcrumbs', 20);
    add_action('woocommerce_before_main_content', 'filter_styling', 19);  
    add_action('woocommerce_before_main_content', 'filter_styling2', 20);
}
add_action('template_redirect','product_category_filter_changes');
like image 193
rnevius Avatar answered Dec 17 '22 02:12

rnevius