remove_action() not working in Woocommerce - php

i'm trying to change the order of my product page in woocommerce but i have a few problems with the remove_action().
I activated the child theme and in my functions.php i'm tryin to remove the add-to-cart button to add it back on another position.
I tried to put my remove_action() into a function that gets executed right after my parent theme loads.
function change_order() {
add_action('woocommerce_single_product_summary','woocommerce_template_single_add_to_cart', 15 );
remove_action('woocommerce_single_product_summary','woocommerce_template_single_add_to_cart', 30 );
}
add_action( 'after_setup_theme', 'change_order' );
The add_action gets excuted but the remove_action() isn't working...
I tried the same with 'init' instead of 'after_setup_theme' but it is still not working...
Does anybody know a solution for my problem ?

Use a different single product hook
add_action('woocommerce_single_product_summary','change_order' );
function change_order() {
add_action('woocommerce_product_meta_end','woocommerce_template_single_add_to_cart', 15 );
remove_action('woocommerce_single_product_summary','woocommerce_template_single_add_to_cart', 30 );
}
Works and tested.

Related

Remove function from Wordpress Plugin [duplicate]

This question already has answers here:
remove_action() not working in WordPress plugin
(4 answers)
Removing functions from Woocommerce hooks
(2 answers)
Closed 2 years ago.
The WooCommerce plugin is inserting a script into the footer of my site. I have been attempting to remove this, but nothing seems to be working.
The following action is used to add the script to the footer:
add_action( 'wp_footer', 'wc_print_js', 25 );
I have tried removing this by placing the following in the child theme function file:
add_action( 'after_setup_theme', 'remove_wc_plugin_function', 0 );
function remove_wc_plugin_function() {
remove_action( 'wp_footer', 'wc_print_js' );
}
Have also tried just replacing the whole wc-core-functions.php file in my child theme, and commenting out the relevant functions, but that also doesnt work. I assume the function does not exist when the child theme function file is run. I then attempted to remove all jquery from the home page, which also doesnt get remove the script.
add_action( 'wp_enqueue_scripts', 'my_deregister_javascript' );
function my_deregister_javascript() {
if ( is_front_page('home') ) {
wp_deregister_script( 'jquery' );
}
}
Have also tried just replacing the whole wc-core-functions.php file in my child theme, and commenting out the relevant functions, but that also doesnt work. How can get I get rid of this bloody script in the footer (or preferably just get rid of a specific function)?
TLDR: How to remove a Wordpress plugin function, the script that was inserted with that function?
https://developer.wordpress.org/reference/functions/remove_action/:
To remove a hook, the $function_to_remove and $priority arguments must match when the hook was added.
The function was added to the hook using
add_action( 'wp_footer', 'wc_print_js', 25 );
but you tried to remove it using only remove_action( 'wp_footer', 'wc_print_js' );, and then it will use the default value 10 for that third parameter.
So the correct way here to remove this function from the action again should be
remove_action( 'wp_footer', 'wc_print_js', 25 );

sale_flash badge cannot be removed from woocommerce product loop hook/action

I am trying to remove or edit the "sale!" badge is in woocommrce loop
In the content-product.php,
the comment block says the woocommerce_show_product_loop_sale_flash is hooked with woocommerce_before_shop_loop_item.
However it actually works with woocommerce_after_shop_loop_item_title.
I tried to remove everything from the hook, still, the sales badge still appears:
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_rating' );
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price' );
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_show_product_loop_sale_flash' );
The following is also not working:
remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_show_product_loop_sale_flash' );
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_show_product_loop_sale_flash' );
So I really have no idea where the sales badge function is being called from?
I'm not sure why it's not working for you, because all you have to do is use remove_action, the parameters are the hook location being used and the action name. In this case:
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_show_product_loop_sale_flash', 10 );
This works, I checked. I was looking for the same thing. Just add it to your functions.php file. It removes the action called woocommerce_show_product_loop_sale_flash from being hooked into the woocommerce_before_shop_loop_item_title hook location.
Perhaps there is a plugin putting it there as well?
In my case, I did not want the sale flash to be inside of the link so I moved it up to be a direct child of the li instead. I did this by first removing the action via the above, and then adding a new action with a new hook location. The new hook location is the same as the one used to open the link, so I had to modify the priority number to make sure it was executed before the link action sharing the hook.
add_action( 'woocommerce_before_shop_loop_item', 'woocommerce_show_product_loop_sale_flash', 5 );

Remove structured data from Woocommerce single product pages

I am trying to remove the structured data that Woocommerce adds to the product pages.
I did some research and found that WC_Structured_Data::generate_product_data() generates the structured data markup. It's hooked in the woocommerce_single_product_summary action hook in the woocommerce/templates/content-single-product.php template file.
I tried by adding the following code to the functions.php
remove_action( 'woocommerce_single_product_summary', 'WC_Structured_Data::generate_product_data()', 60 );
So structured data wouldn't be added by Woocommerce, but it doesn't work…
Am I doing something wrong? Is there another way to do what I am trying to achieve?
Instead, you can use dedicated filter hook 'woocommerce_structured_data_product' that is located in WC_Structured_Data for generate_product_data() method nulling the structured data output in single product pages:
add_filter( 'woocommerce_structured_data_product', 'structured_data_product_nulled', 10, 2 );
function structured_data_product_nulled( $markup, $product ){
if( is_product() ) {
$markup = '';
}
return $markup;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
I suspect people want to remove the default tabs and they come here after they see the Woocommerce content-single-product.php template. In this template you see that generate_product_data() is hooked with priority 60.
After inspecting the hooks that run on woocommerce_single_product_summary.
You can easily remove the tabs with:
remove_action( 'woocommerce_single_product_summary', 'woocommerce_output_product_data_tabs', 60 );
I think Woocommerce forgot to mention this add_action.
This is how you can remove hooks associated with instantiated object method. You have to find the variable that holds the new Object instance.
In this case the main WooCommerce object is accessible as $GLOBALS['woocommerce'] and it has public property $structured_data which holds an instance of the WC_Structured_Data object.
Hence, to remove the hook in the question you can write this code:
remove_action( 'woocommerce_before_main_content', array( $GLOBALS['woocommerce']->structured_data, 'generate_website_data' ), 30 );
Add to functions.php:
add_action('wp_loaded', function() {
remove_action('woocommerce_single_product_summary', [$GLOBALS['woocommerce']->structured_data, 'generate_product_data'], 60);
});
Unhooks WC_Structured_Data::generate_product_data(). Will not waste resources on generating product data first for no reason and then "nulling" that same generated data a moment later using a filter.

Hiding breadcrumbs on cart page in woocommerce with storefront theme

I'm trying to hide breadcrumbs with a child theme of storefront. This is the code I have in functions.php however, the conditional does not fire when on the cart page. Removing the conditional causes the breadcrumbs to be hidden
add_action('init', 'remove_shop_breadcrumbs' );
function remove_shop_breadcrumbs()
{
if ( is_cart())
{
remove_action( 'storefront_content_top', 'woocommerce_breadcrumb', 10 );
}
}
From everything I can read, this is correct, does storefront replace this conditional with its own code, hence causing this to fail?
You're code is firing too early. The init hook is triggered before the queries have been run therefore is_cart() won't work. Use the wp action instead.
Change this:
add_action('init', 'remove_shop_breadcrumbs' );
To this:
add_action( 'wp', 'remove_shop_breadcrumbs' );

Remove Woocommerce Product Short Description

I'm trying to remove the Product Short Description showed on a Single Product Page in Woocommerce. By default it's placed right underneath the price, I want it totally gone.
I tried to use this code: remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20); and placed in functions.php of my Child theme folder but it doesn't change anything.
If I try woocommerce_template_single_add_to_cart or woocommerce_template_single_meta to see if the piece of code actually works it removes the add to cart button and product meta but for some reason the excerpt isn't affected.
Any other solutions to remove it?
Thanks!!
Joost
The right snippet is posted here:
How to remove wordpress Short description tab in single product page in wordpress?
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
function woocommerce_template_single_excerpt() {
return;
}
Get the code snippet plugin and run
add_filter( 'woocommerce_product_tabs', 'woo_remove_product_tabs', 98 );
function woo_remove_product_tabs( $tabs ) {
unset( $tabs['additional_information'] );
return $tabs;
}

Categories