1

I want to add some code to my child theme to remove an action set in the parent theme.

The action is:

add_action( 'woocommerce_product_options_inventory_product_data', array( 'Electro_WC_Helper', 'product_options_inventory_product_data' ) );

It is within a class 'Electro_WC_Helper'.

I have tried:

remove_action( 'woocommerce_product_options_inventory_product_data', array( 'Electro_WC_Helper', 'product_options_inventory_product_data' ), 99 );

Which didn't work, so I thought this is probably because the child functions is firing before the parent, so I added this:

add_action( 'admin_head', 'test_function', 99 );

function test_function() {

    echo 'test543';

    remove_action( 'woocommerce_product_options_inventory_product_data', array( Electro_WC_Helper, 'product_options_inventory_product_data' ), 99 );

}

Which also doesn't work.

How to remove the action?

3

To remove an action or filter, the function/method name and the priority must match with the previously added action/filter. The action you want to remove is added with a priority of 10 (default value) while you are trying to remove the action with priority of 99.

Try this:

remove_action( 'woocommerce_product_options_inventory_product_data', array( 'Electro_WC_Helper', 'product_options_inventory_product_data' ) );
// It is the same that:
// remove_action( 'woocommerce_product_options_inventory_product_data', array( 'Electro_WC_Helper', 'product_options_inventory_product_data' ), 10 );

Also, functions.php file from child theme is loaded before the functions.php file from the parent theme, so, the execution of remove_action() function in the child theme must be deferred using some action hook, because it must wait until the parent theme adds the action we want to remove. admin_head is an action that only happens on admin side and not associated with themes, so it does not triggered on front-end. You should use a proper action, which depends how exactly the parent theme is adding the action event; typically you should use after_setup_theme with a high priority:

add_action( 'after_setup_theme', 'cyb_remove_parent_theme_action', 0 );
function cyb_remove_parent_theme_action() {
    remove_action( 'woocommerce_product_options_inventory_product_data', array( 'Electro_WC_Helper', 'product_options_inventory_product_data' ) );
}
  • Yes, that was the first thing I tried, it does not remove the action. – Steveo Sep 22 '17 at 11:30
  • Answer updated. – cybmeta Sep 22 '17 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.