How to disable an action from template hooks in Wordpress' Woocommerce? - php

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

Related

Why is WooCommerce function not overriding the default function in child theme

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 );

Remove WooCommerce messages from account content

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

Remove breadcrumbs from Storefront theme in Woocommerce 3.3

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 );
}

Move Upsells after Related products section in WooCommerce

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.

Remove single product tabs and add the related content instead In Woocommerce

I have a client who wants to pull the information that defaults into tabs on single product pages in WooCommerce into a different location on the page and remove the tabs entirely.
There are three default product tabs:
product Description,
Additional Information
and Reviews.
Removing the tabs and setting up the Description to display was easy enough to set up in
/wp-content/plugins/woocommerce/includes/wc-template-hooks.php:
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_product_data_tabs', 10 );
function woocommerce_template_product_description() {
woocommerce_get_template( 'single-product/tabs/description.php' );
}
add_action( 'woocommerce_after_single_product_summary', 'woocommerce_template_product_description', 10 );
That works fine.
I tried to repeat the process by building out new functions that access the template files for Additional Info and Reviews like so:
function woocommerce_template_product_addinfo() {
woocommerce_get_template( 'single-product/tabs/additional-information.php' );
}
add_action( 'woocommerce_after_single_product_summary', 'woocommerce_template_product_addinfo', 20 );
function woocommerce_template_product_reviews() {
woocommerce_get_template( 'single-product/review-rating.php' );
}
add_action( 'woocommerce_after_single_product_summary', 'woocommerce_template_product_reviews', 30 );
But neither is displaying. What am I doing wrong here?
First woocommerce_get_template() is deprecated and replaced by wc_get_template() instead. After some searching and testing (mainly to get the reviews displayed), I have found the way:
add_action( 'woocommerce_after_single_product_summary', 'removing_product_tabs', 2 );
function removing_product_tabs(){
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_product_data_tabs', 10 );
add_action( 'woocommerce_after_single_product_summary', 'get_product_tab_templates_displayed', 10 );
}
function get_product_tab_templates_displayed() {
wc_get_template( 'single-product/tabs/description.php' );
wc_get_template( 'single-product/tabs/additional-information.php' );
comments_template();
}
Code goes in function.php file of your active child theme (or theme). Tested and work (WC 3+).

Categories