Remove parent theme action in child theme

I have create Child theme from Shophistic Lite theme.

I want to remove an action in child theme.

// wp-content\plugins\woocommerce\templates\content-product.php ...

/**
 * woocommerce_shop_loop_item_title hook.
 *
 * @hooked woocommerce_template_loop_product_title - 10
 */
do_action( 'woocommerce_shop_loop_item_title' );

/**
 * woocommerce_after_shop_loop_item_title hook.
 *
 * @hooked woocommerce_template_loop_rating - 5
 * @hooked woocommerce_template_loop_price - 10
 */
do_action( 'woocommerce_after_shop_loop_item_title' );

/**
 * woocommerce_after_shop_loop_item hook.
 *
 * @hooked woocommerce_template_loop_product_link_close - 5
 * @hooked woocommerce_template_loop_add_to_cart - 10
 */
do_action( 'woocommerce_after_shop_loop_item' );

...

// \wp-content\themes\shophistic-lite\framework\functions\woocommerce_support.php

...
/**
 * Adds the Switch View buttons
 */
function shophistic_lite_show_attribute() {
...
}
add_action( 'woocommerce_after_shop_loop_item_title', 'shophistic_lite_show_attribute', 15 );
...

// \wp-content\themes\shophistic-lite-child\functions.php

...
function remove_functions() {
    remove_action( 'woocommerce_after_shop_loop_item_title', 'shophistic_lite_show_attribute', 15 );
}
add_action('woocommerce_after_shop_loop_item' , 'remove_functions' );
...

I made it by this article: https://code.tutsplus.com/tutorials/a-guide-to-overriding-parent-theme-functions-in-your-child-theme--cms-22623 But it does no working for me. The shophistic_lite_show_attribute function is still executing.

Please help me to resolve this problem.

Upvotes: 3

Views: 4804

Answers (2)

Martin Pclin
Martin Pclin

Reputation: 11

In my case, I was trying to remove related product section from single product page. It was not the default related product section, it was specific to my theme (and so I had to remove action from parent theme).

Using wp_loaded hook worked fine, while init hook mentionned above didn't worked.

So finally, this code in my child theme is working fine:

function child_custom_actions() {
    remove_action( 'woocommerce_after_single_product', 'woocommerce_output_related_products', 20 );
}
add_action( 'wp_loaded' , 'child_custom_actions' );

Warning : woocommerce_after_single_product hook is not the default hook for woocommerce related product section, it is specific to my theme, you might need to use woocommerce_after_single_product_summary hook.

Upvotes: 1

LoicTheAztec
LoicTheAztec

Reputation: 253804

You should try the 'init' hook instead for your remove action, to remove it at initialization:

function child_custom_actions() {
    remove_action( 'woocommerce_after_shop_loop_item_title', 'shophistic_lite_show_attribute', 15 );
}
add_action( 'init' , 'child_custom_actions' );

This code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested and works (see below).


Update related to your comment:

I have uploaded your theme Shophistic Lite in a test server, created a child theme.

I have temporary changed the function on the main theme in woocommerce_support.php file, to make it display something, without any special settings needs (and saved) this way:

/**
 * Adds the Switch View buttons
 */
function shophistic_lite_show_attribute() {
    echo '<p>BLA BLA</p>';
}
add_action( 'woocommerce_after_shop_loop_item_title', 'shophistic_lite_show_attribute', 15 );

And it displays on shop page this:

enter image description here

Then I have add the code of the first snippet above on function.php file of the child theme (the same you have tried with the 'init' hook), and it just works perfectly, removing that "BLA BLA".

So may be you are trying to remove something else that is not generated by this hook…

Upvotes: 5

Related Questions