What is the procedure of overriding functions that are hooked into WordPress?
The following function in wc-template-functions.php is one of the functions I'm looking to override:
function woocommerce_template_loop_product_link_close() {
echo '</a>';
}
It is hooked in at wc-template-hooks.php:
add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_product_link_close', 5 );
If I write the following code, the function is then unhooked:
function remove_default_wc_behaviour() {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_product_link_close', 5 );
}
add_action( 'init', 'remove_default_wc_behaviour' );
But what if I want to override the function, ie. implement my own functionality instead of the default functionality from WooCommerce?
Would I call a remove_action() first, and then hook in my function with add_action(), or is there a procedure which can simply override the function that is hooked in my WooCommerce?
Thanks in advance
The first option is the right one: In your main function hooked in init action hook, you can add your replacement hooked function:
add_action( 'init', 'replace_default_wc_behaviour' );
function remove_default_wc_behaviour() {
// remove the default behavior
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_product_link_close', 5 );
// Replace by your custom behavior
add_action( 'woocommerce_after_shop_loop_item', 'custom_WC_loop_product_link_close', 5 );
function custom_WC_loop_product_link_close() {
// Do your custumizations
// add the close tag
echo '</a>';
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Related
I have a child theme that I am trying to override a WooCommerce function.
WooCommerce file in plugin location: woocommerce/includes/wc-template-hooks.php. I created a new file in the same hierarchy in my child theme and it's located here: my-child-theme/woocommerce/includes/wc-template-hooks.php
My edited section is I commented out the product title in child theme:
/**
* Product Summary Box.
*
* #see woocommerce_template_single_title()
* #see woocommerce_template_single_rating()
* #see woocommerce_template_single_price()
* #see woocommerce_template_single_excerpt()
* #see woocommerce_template_single_meta()
* #see woocommerce_template_single_sharing()
*/
//add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 ); //commented
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_rating', 10 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_sharing', 50 );
However, when I goto my product page, I still see the title.
Any idea what I am doing wrong?
You need to remove action woocommerce_single_product_summary to this work properly.
add_action( 'woocommerce_before_single_product', 'remove_woocommerce_single_product_summary', 10 );
function remove_woocommerce_single_product_summary() {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
}
Just add this to your functions.php and will do the magic.
You also can try to add remove_action() out of the add_action(). Just like this...
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 15 );
...directly to functions.php
Play with it.
TIPS & TRICKS
If you want to change your HTML in the title or do anything you want, here is example:
add_action( 'woocommerce_before_single_product', 'remove_woocommerce_single_product_summary', 10 );
function remove_woocommerce_single_product_summary() {
// First, we remove it
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
// Second, we add new title
add_action( 'woocommerce_single_product_summary', 'woocommerce_my_new_title', 1 );
}
function woocommerce_my_new_title(){
echo '<h1 class="some-my-custom-classes">' . the_title() . '</h1>';
}
If your active theme does not support Woocommerce and you want to add
a Woocommerce template to your active theme, you must register
WooCommerce, or give Woocommerce features.
Check out these links for more details:
https://github.com/woocommerce/woocommerce/wiki/Declaring-WooCommerce-support-in-themes
https://docs.woocommerce.com/document/template-structure/
One important thing:
If you change the template folder within the Woocommerce plugin, there is a good chance that you will lose your settings after the plugin update. That is why it is better to work in an active theme.
function my_custom_action() {
echo '<p>Hello here</p>';
};
add_action( 'woocommerce_single_product_summary', 'my_custom_action', 15 );
I'm currently trying to remove all messages from the WooCommerce account with the following code line:
remove_action( 'woocommerce_account_content', 'woocommerce_output_all_notices', 10 );
Sadly, the wrapper for messages is still there:
<div class="woocommerce-notices-wrapper">lol</div>
I've added a lol to the function that displays the wrapper and it's the correct function I'm trying to remove. No idea why it's not working...
If your active theme uses the default woocommerce templates and hooks for myaccount pages, then add the follows code snippet to achieve the above -
function modify_wc_hooks() {
// remove all wc my account's notices wrapper
remove_action( 'woocommerce_account_content', 'woocommerce_output_all_notices', 5 );
remove_action( 'woocommerce_before_customer_login_form', 'woocommerce_output_all_notices', 10 );
remove_action( 'woocommerce_before_lost_password_form', 'woocommerce_output_all_notices', 10 );
remove_action( 'before_woocommerce_pay', 'woocommerce_output_all_notices', 10 );
remove_action( 'woocommerce_before_reset_password_form', 'woocommerce_output_all_notices', 10 );
}
add_action( 'init', 'modify_wc_hooks', 99 );
Codes goes to your active theme's functions.php
Up until a recent update (for Woocommerce, and also the Storefront theme), I was able to remove breadcrumbs using the following code in the child theme functions.php
add_action( 'init', 'z_remove_storefront_breadcrumb' );
function z_remove_storefront_breadcrumb() {
remove_action( 'storefront_content_top', 'woocommerce_breadcrumb', 10 );
That solution is described here.
Since the update (and I am not sure if it was the WC or SF update that did it) this no longer works.
I also tried the other method suggested in the above-mentioned post. And a few other methods, all listed here:
add_filter( ‘woocommerce_get_breadcrumb’, ‘__return_false’ );
remove_action( 'woocommerce_before_main_content','woocommerce_breadcrumb', 20, 0);
and
add_action( 'init', 'jk_remove_storefront_breadcrumb' );
function jk_remove_storefront_breadcrumb() {
remove_action( 'storefront_content_top', 'woocommerce_breadcrumb', 10 );
}
also
add_action( 'init', 'woo_remove_wc_breadcrumbs' );
function woo_remove_wc_breadcrumbs() {
remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20, 0 );
}
also
add_filter( ‘woocommerce_get_breadcrumb’, ‘__return_false’ );
remove_action( 'woocommerce_before_main_content','woocommerce_breadcrumb', 20, 0);
also
add_filter( 'woocommerce_before_main_content', 'remove_breadcrumbs');
function remove_breadcrumbs() {
remove_action( 'woocommerce_before_main_content','woocommerce_breadcrumb', 20, 0);
}
Can anyone suggest what the current way to remove breadcrumbs from the Storefront theme is?
Okay, so I finally find the answer. The following code will remove the Storefront theme breadcrumbs:
add_action( 'init', 'wc_remove_storefront_breadcrumbs');
function wc_remove_storefront_breadcrumbs() {
remove_action( 'storefront_before_content', 'woocommerce_breadcrumb', 10 );
}
How can i change order of Up-sell & Related products sections on single product. It need to show first related products and then up-sell
Try the following that will move upsells after related products:
1) For Most themes:
add_action( 'init', 'move_upsells_after_related' );
function move_upsells_after_related( ) {
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_upsell_display', 15 );
add_action( 'woocommerce_after_single_product_summary', 'woocommerce_upsell_display', 25 );
}
Now in some themes like Storefront, a custom hook is used.
2) For Storefront theme only you will use instead:
add_action( 'init', 'move_upsells_after_related' );
function move_upsells_after_related() {
remove_action( 'woocommerce_after_single_product_summary', 'storefront_upsell_display', 15 );
add_action( 'woocommerce_after_single_product_summary', 'storefront_upsell_display', 25 );
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Here's a part of codes in wc-template-hooks.php:
add_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_product_data_tabs', 10 );
add_action( 'woocommerce_after_single_product_summary', 'woocommerce_upsell_display', 15 );
add_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
Here's the code from content-single-product.php
do_action( 'woocommerce_after_single_product_summary' );
What code should I put in content-single-product.php to remove woocommerce_output_related_products without editing the wc-template-hooks.php.
Sorry, I'm new in PHP. Thanks in advance.
You dont have to put it in content-single-product.php, try adding it to functions.php and if you just need to remove it on a single products page use woocommerce conditional functions to allow it only on the pages you want. for instance.
<?php if(is_product()){
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 ); }?>
I have tried all above solutions but not working. Woo-commerce Version 3.4.1
Working Code, Added in child theme in functions.php
add_filter('woocommerce_product_related_posts_query', '__return_empty_array', 20);
If I correctly understood your question, put this in the functions.php inside your theme folder:
<?php
function woocommerce_remove_related_products() {
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
}
add_action('woocommerce_after_single_product_summary', 'woocommerce_remove_related_products');
?>
You can remove action try adding it to functions.php -
add_filter( 'body_class', 'remove_action_from_woocommerce');
function remove_action_from_woocommerce()
{
if(is_product()){
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 ); }
}
}
Here you can get WooCommerce Action and Filter Hook
-https://docs.woothemes.com/wc-apidocs/hook-docs.html