Remove 'email_addresses' action from 'woocommerce_email_customer_details' hook - php

In WooCommerce, there is a public static class WC_Emails, defined in /woocommerce/includes/class-wc-emails.php.
Within that class, there is a reference to a hook called woocommerce_email_customer_details. To that hook, an action called email_addresses is assigned, which is simply a PHP file that generates some HTML for billing and shipping addresses to be added to email notifications.
add_action( 'woocommerce_email_customer_details', array( $this, 'email_addresses' ), 20, 3 );
I am trying to remove that action and can't seem to figure out how to do it.
Here's an example bit of code in my child theme's functions.php that does not work:
add_action( 'init', 'remove_default_addresses');
function remove_default_addresses() {
remove_action( 'woocommerce_email_customer_details',
array( 'WC_Emails', 'email_addresses' ), 20);
}
Instead of the init hook there, I have tried wp-head. Since WC_Emails is a static function, the above code is the way the WordPress Codex suggests, as far as using an array() above, instead of just the action name. Regardless, this doesn't work, either (with just the action name):
add_action( 'init', 'remove_default_addresses');
function remove_default_addresses() {
remove_action( 'woocommerce_email_customer_details', 'email_addresses', 20);
}
20 is the priority on the add_action Woo does, and I also understand that a remove_action() must have the same priority as what the original add_action() had.
No matter what I try, the billing and shipping addresses still appear on the email notifications.
Once that gets working, it will be great! However, there is an additional requirement: This needs to happen only inside code I have for the woocommerce_order_status_pending_to_processing_notification hook in Woo.
In other words, I have other code that fires and generates an email when an order status changes from Pending to Processing (and I have tried my code above with all its variations within that hook as well). I need the billing and shipping addresses to NOT show only in the custom email that is generated.
Any ideas? Thanks so much. :)

Here is the code:
add_action( 'woocommerce_email', function ( $email_class ) {
remove_action( 'woocommerce_email_customer_details', array( $email_class, 'email_addresses' ), 20, 3 );
});
Source

Related

Unhook remove_non_recurring_fees() WooCommerce Subscriptions function

I'm desperately trying to remove an action while the cart calculates the total.
Here is my code:
remove_action('woocommerce_cart_calculate_fees', array('WCS_Cart_Renewal', 'remove_non_recurring_fees'), 1000);
While the original action hook is taking place on the WooCommerce Subscriptions plugin:
// Remove non-recurring fees from renewal carts. Hooked in late (priority 1000), to ensure we handle all fees added by third-parties.
add_action( 'woocommerce_cart_calculate_fees', array( $this, 'remove_non_recurring_fees' ), 1000 );
Unfortunately I could not remove the remove_non_recurring_fees hooked function.
Any idea why?
When you Look at WCS_Cart_Renewal Class and remove_non_recurring_fees() function, you will see that this function removes all fees first and re-add only recurring fees, when a subscription is involved. This function is hooked with a priority of 1000.
Instead of trying to remove the action hook that trigger this function, you have 2 other choices:
1). For custom fees added by you via your theme's functions.php file:
You will have just to use a greater priority like in this following example:
add_action( 'woocommerce_cart_calculate_fees', 'my_custom_fee', 2000 );
function my_custom_fee( $cart ) {
// Your code
}
2). Or better using woocommerce_subscriptions_is_recurring_fee available filter hook:
This filter hook that allows to re-add all desired fees that are not recurring with this simple code line:
add_filter( 'woocommerce_subscriptions_is_recurring_fee', '__return_true' );
Code goes in functions.php file of the active child theme (or active theme). Tested and works.

Override function in custom plugin

I'm using a plugin with this action:
add_filter( 'pre_comment_approved', array( $this, 'pre_save_review' ), 10, 2 );
I've written a plugin to do some tweaking to my site here and there. Mostly REST API customizations.
I'd like to override, subvert, ignore, remove, that filter. Right now I've got it "working" by just returning on the first line of the pre_save_review function.
I tried:
remove_action( 'pre_comment_approved', 'pre_save_review');
... but I wonder if there's some namespacing issue. I don't know a lot about PHP so I don't know how to refer to classes in other files/plugins, which I imagine is the issue.
Thanks!
You can remove all filters with a wordpress function "remove_all_filters"
remove_all_filters('pre_comment_approved');
This also has a second parameter to pass the priority number, since this one is added with priority 10 it would be this:
remove_all_filters('pre_comment_approved', 10);
Reference remove_all_filters
You'll want to use the remove_filter function for this.
See the Example in the documentation here: https://codex.wordpress.org/Function_Reference/remove_filter
If a filter has been added from within a class, for example by a
plugin, removing it will require accessing the class variable.
global $my_class;
remove_filter( 'the_content', array($my_class, 'class_filter_function') );
So maybe give the following a try;
global $this;
remove_filter( 'pre_comment_approved', array( $this, 'pre_save_review' ) );

woocommerce hook when order is placed through REST API

I need to run some custom PHP code when an order is placed on a WooCommerce store. Currently, I am using woocommerce_order_status_changed hook which is working perfectly for web front.
add_action('woocommerce_order_status_changed', 'order_confirmation',10, 3);
function order_confirmation($order_id,$oldstatus,$newstatus){
//my custom code...
}
But when an order is placed through API, this hook is not called.
Is there any hook that we can use to execute some php code when an order is placed through WooCommerce's Rest Api V2?
i think you are sending the set_paid property to true. It sets the status to processing and reduce stock items. if you need to to perform action when order payment is completed, you can use the woocommerce_payment_complete action hook.
function on_woocommerce_payment_complete($order_id){
}
add_action( 'woocommerce_payment_complete', 'on_woocommerce_payment_complete'
);`
However the above hook only fire when order status was from the following array
on-hold', 'pending', 'failed', 'cancelled
before marking the payment completed.
For other order statues the following hook is fired.
do_action( 'woocommerce_payment_complete_order_status_' . $this->get_status(), $this->get_id() );
For more detail you can check the
public function payment_complete( $transaction_id = '' ) {
define in
woocommerce\includes\class-wc-order.php

Removing functions from Woocommerce hooks

This will probably open a door for me as there's something I'm missing so a WooCommerce 101 please;
part of the WooCommerce template archive-product.php contains the code;
<?php
/**
* woocommerce_before_shop_loop hook.
*
* #hooked woocommerce_result_count - 20
* #hooked woocommerce_catalog_ordering - 30
*/
do_action( 'woocommerce_before_shop_loop' );
?>
From this, and reading the documentation, it implies that this;
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 );
should remove the result count from the product categories returned. Only it doesn't.
What's wrong?
I was going to say you should read the documentation but it is leaving an important part out.
remove_action() cannot be called directly and must, itself, be added to an action hook. The action hook needs to come before the action being removed. In this case I would just use the same hook, but an earlier priority (default is 10, I've used 1)
function so_38878702_remove_hook(){
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 );
}
add_action( 'woocommerce_before_shop_loop', 'so_38878702_remove_hook', 1 );
To remove result count action from Woocommerce -
add_action('woocommerce_before_shop_loop', 'remove_result_count' );
function remove_result_count()
{
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20);
}
use this code in your function.php file.
Important: To remove a hook, the $function_to_remove and $priority
arguments must match when the hook was added. This goes for both
filters and actions. No warning will be given on removal failure.
http://codex.wordpress.org/Function_Reference/remove_action
Here you can get WooCommerce Action and Filter Hook -
https://docs.woothemes.com/wc-apidocs/hook-docs.html

WooCommerce: hook when saving changes to product in an order

I have been searching for hours...
I can't figure out how get a function to be executed when clicking "save" after editing the quantity of a product in an existing order.
I tried this:
add_action('woocommerce_order_edit_product', 'your_function_name');
function your_function_name(){
//my php function code would be here
}
but the your_function_name function is not being called when clicking save.
I tested the function and when calling it directly it works as it should, so I think I got the wrong hook...
after wrestling with this issue for 2 days now, i found it: there is two hooks, one before and one after saving:
woocommerce_before_save_order_items
woocommerce_saved_order_items
both are fired when saving an order in the backend. one before the save and one afterwards.
both two hooks carry the same variables: $order_id (int) & $items (array)
i guess with the first hook, you could get the old order and compare its contents with the items array to see what has changed. at least this is what i try to accomplish now.
so this is how you would trigger this:
add_action( 'woocommerce_before_save_order_items', 'so42270384_woocommerce_before_save_order_items', 10, 2 );
function so42270384_woocommerce_before_save_order_items( $order_id, $items ) {
echo $order_id;
var_dump( $items );
}
be aware..
adding a product to an exitsting order does implement another hook that gets called before this (so when hitting SAVE, the above function will fire, but the order and its items are already set BEFORE saving (when adding a product, the order will save immediately). that means $order = new WC_Order( $order_id ); will have the new items already in it, before and after, so there is no way to find, what has changed.). but the woocommerce_ajax_add_order_item_meta hook is triggered on 'add product' and helped me on that end. happy coding everyone..
Check your error log. Should be some info there. If I am looking at the correct action it takes four arguments:
do_action( 'woocommerce_order_edit_product', $this->id, $item_id, $args, $product );
So your code should be:
add_action('woocommerce_order_edit_product', 'your_function_name', 10, 4);
function your_function_name($id, $item_id, $args, $product){
//my php function code would be here
}

Categories