- I am child theming a WooTheme
- This WooTheme uses the WooCommerce plugin
- The parent theme declares several WooCommerce plugin functions overrides (to override the default plugin output)
<strong>So how can I override these parent functions for the child theme?</strong>. From my understanding, this is certainly possible if/where the parent theme functions are pluggable/conditional. But this WooTheme is not (by my understanding)
For example, the parent theme declares the following functions in its functions.php file to override the default plugin output ...
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
add_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
function woocommerce_output_related_products() {
woocommerce_related_products(3,3); // 3 products, 3 columns
}
I obviously cannot redeclare this function in the child theme functions.php as the child theme code gets executed before the parent.
I hope someone can help.
Peter Michael answers:
But you can remove the filter and re-add it with a different function name.
Peter Michael comments:
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
function my_output_related_products() {
// Your Stuff Here
}
add_action( 'woocommerce_after_single_product_summary', 'my_output_related_products', 20);
Peter Michael comments:
And? Any update?
Jerson Baguio answers:
this might help
function osu_twentyten_continue_reading_link() {
return ' <a href="'. get_permalink() . '">' . __( 'Read on <span class="meta-nav">→</span>', 'twentyten-child' ) . '</a>';
}
function osu_twentyten_auto_excerpt_more( $more ) {
return ' …' . osu_twentyten_continue_reading_link();
}
function my_child_theme_setup() {
remove_filter( 'excerpt_more', 'twentyten_auto_excerpt_more' );
add_filter( 'excerpt_more', 'osu_twentyten_auto_excerpt_more' );
}
add_action( 'after_setup_theme', 'my_child_theme_setup' );
the code above if from this article
[[LINK href="http://wordpress.stackexchange.com/questions/7557/how-to-override-parent-functions-in-child-themes"]][[/LINK]]
Jerson Baguio comments:
here's the link from that article http://wordpress.stackexchange.com/questions/7557/how-to-override-parent-functions-in-child-themes
Francisco Javier Carazo Gil answers:
Hi,
As you can see in the reference:
<blockquote>Unlike style.css, the functions.php of a child theme does not override its counterpart from the parent. Instead, it is loaded in addition to the parent’s functions.php. (Specifically, it is loaded right before the parent’s file.)
</blockquote>
So, you will have to run your code after theme setup and:
1. Make new functions
2. Remove your filter and actions
3. Create new filter and actions with your new functions.
For example:
function osu_twentyten_continue_reading_link() {
return ' <a href="'. get_permalink() . '">' . __( 'Read on <span class="meta-nav">→</span>', 'twentyten-child' ) . '</a>';
}
function osu_twentyten_auto_excerpt_more( $more ) {
return ' …' . osu_twentyten_continue_reading_link();
}
function my_child_theme_setup() {
remove_filter( 'excerpt_more', 'twentyten_auto_excerpt_more' );
add_filter( 'excerpt_more', 'osu_twentyten_auto_excerpt_more' );
}
add_action( 'after_setup_theme', 'my_child_theme_setup' );
Francisco Javier Carazo Gil comments:
Jerson and me have seen this page: http://wordpress.stackexchange.com/questions/7557/how-to-override-parent-functions-in-child-themes
Julio Potier answers:
Hello try this :
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
add_action( 'woocommerce_after_single_product_summary', 'my_woocommerce_output_related_products', 20);
function my_woocommerce_output_related_products() {
woocommerce_related_products(3,3); // 3 products, 3 columns
}
Romel Apuya answers:
try this :
function remove_woocommerce_after_single_product_summary() {
remove_action('woocommerce_after_single_product_summary','woocommerce_related_products',20);
}
add_action('init','remove_woocommerce_after_single_product_summary');
function woocommerce_output_related_products() {
woocommerce_related_products(3,3); // 3 products, 3 columns
}
add_action('woocommerce_after_single_product_summary','woocommerce_output_related_products', 20);