Wordpress remove_action within class - wordpress

I am trying to remove an action added by the Woocommerce Memberships plugin. Tracing back the actions contains functions it is initially added to the hook under
class WC_Memberships_Frontend {
public function __construct() {
add_action( 'woocommerce_thankyou', array( $this, 'maybe_render_thank_you_content' ), 9 );
But this class is called by a private function and is a protected by the variable that its parent class declaration (as far as I can tell)
class WC_Memberships extends Framework\SV_WC_Plugin {
protected $frontend;
private function frontend_includes() {
// init shortcodes
require_once( $this->get_plugin_path() . '/includes/class-wc-memberships-shortcodes.php' );
\WC_Memberships_Shortcodes::initialize();
// load front end
$this->frontend = $this->load_class( '/includes/frontend/class-wc-memberships-frontend.php', 'WC_Memberships_Frontend' );
}
I have search and tried a number of ways to remove this
remove_action( 'woocommerce_thankyou', array( 'WC_Memberships', 'maybe_render_thank_you_content' ), 9 );
remove_action( 'woocommerce_thankyou', array( 'WC_Memberships_Frontend', 'maybe_render_thank_you_content' ), 9 );
global $WC_Memberships;
remove_action( 'woocommerce_thankyou', array( $WC_Memberships, 'maybe_render_thank_you_content' ), 9 );
global $WC_Memberships_Frontend;
remove_action( 'woocommerce_thankyou', array( $WC_Memberships_Frontend, 'maybe_render_thank_you_content' ), 9 );
None of the above work, and other ways of trying to call WC_Memberships()->frontend; throw errors like 'Cannot access protected property WC_Memberships::$frontend'
Im not sure if the private function or protected variable are getting in the way, or if Im just not understanding something about removing an action within a class or nested classes but help would be greatly appreciated.
Edit:
Based on the code found here Ive tried
remove_action( 'woocommerce_thankyou', array( wc_memberships()->get_frontend_instance(), 'maybe_render_thank_you_content', 9 ) );
but still no success.

My issue was trying to figure out how to match the $this in the original add_action add_action( 'woocommerce_thankyou', array( $this, 'maybe_render_thank_you_content' ), 9 ); to remove it.
I finally stumbled on this chunk of code which showed me how to reference the proper instance of the class.
Final working code:
remove_action( 'woocommerce_thankyou', array( wc_memberships()->get_frontend_instance(), 'maybe_render_thank_you_content', 9 ) );

As you probably know, it's usually not good practice to modify core or plugin files. Using a modification to the answer here , you could try adding to your themes functions.php file
function remove_aggravating_wcactions()
{
remove_action( 'woocommerce_thankyou', 'maybe_render_thank_you_content', 19 );
remove_action( 'woocommerce_thankyou', 'maybe_some_other_content', 20);
...
}
add_action('template_redirect','remove_aggravating_wcactions');

Related

How to Unhook actions/filters in within Class in plugin

I have a business website for vacation home rentals in which the WordPress theme has its own booking system for house bookings only and it has been great!
I also, have Tour/Services (not Houses) also bookable but for these I use WooCommerce/WooCommerce Bookings plugins, not the House booking system.
Everything has been great!
But recently the theme update introduced a WooCommerce checkout option for the Home booking system which never existed prior to the update. Although I have the themes WooCommerce checkout option turned OFF, the code is still being executed in the theme's core plugin. I have been going back and forth with an open support ticket with the theme provider for a few weeks for them to put a conditional statement to NOT run the new code when the user has the option turned OFF but I do not know when or if it will ever be done. What happens now is that the theme's core plugin is manipulating the WooCommerce checkout and after the final step of placing the order of any Tour/Services in which I had always been previously using WooCommerce separately now times out after placing the order and generates over 1,000 additional "Phantom" Checkout pages in the back-end.
Bottom line:
When 3 lines (lines 27-29) are commented out of the theme's core plugin file, everything works fine. But I do not want to have to comment out these 3 lines after each plugin update.
//add_action( 'woocommerce_thankyou', array($this,'order_attach') );
//add_filter( 'woocommerce_checkout_fields', array($this,'custom_override_checkout_fields') );
//add_filter('woocommerce_create_account_default_checked', '__return_true');
Here is the full code (with lines 27-29 commented out): https://pastebin.com/jqtBpCpA
I have tried the instructions on both pages below without success:
https://mekshq.com/remove-wordpress-action-filter-class
https://github.com/herewithme/wp-filters-extras
I have inserted the following into the functions.php file
METHOD 1:
global $Wpestate_Global_Payments; //get access to the class object instance
remove_action( 'woocommerce_thankyou', array($Wpestate_Global_Payments,'order_attach') );
remove_filter( 'woocommerce_checkout_fields', array($Wpestate_Global_Payments,'custom_override_checkout_fields') );
remove_filter('woocommerce_create_account_default_checked', '__return_true');
Then using the plugin, here: https://github.com/herewithme/wp-filters-extras
I tried both methods:
METHOD 2:
remove_filters_with_method_name( 'woocommerce_thankyou', 'order_attach' );
remove_filters_with_method_name( 'woocommerce_checkout_fields', 'custom_override_checkout_fields' );
remove_filters_with_method_name( 'woocommerce_create_account_default_checked', '__return_true' );
METHOD 3:
remove_filters_for_anonymous_class( 'woocommerce_thankyou', 'Wpestate_Global_Payments', 'order_attach' );
remove_filters_for_anonymous_class( 'woocommerce_checkout_fields', 'Wpestate_Global_Payments', 'custom_override_checkout_fields' );
remove_filters_for_anonymous_class( 'woocommerce_create_account_default_checked', 'Wpestate_Global_Payments', '__return_true' );
class Wpestate_Global_Payments {
public $stripe_payments;
public $is_woo;
public $userID;
public $user_email;
function __construct() {
$this->is_woo = wprentals_get_option('wp_estate_enable_woo','') ;
$current_user = wp_get_current_user();
$this->userID = $current_user->ID;
$this->user_email = $current_user->user_email;
add_filter( 'woocommerce_cart_item_permalink','__return_false');
add_action( 'wp_ajax_wpestate_woo_pay', array( $this, 'wpestate_woo_pay') );
add_action( 'wp_ajax_mopriv_wpestate_woo_pay', array( $this, 'wpestate_woo_pay') );
add_filter( 'woocommerce_thankyou_order_received_text', array($this, 'wpestate_woocommerce_thankyou_order_received_text'),10,2 );
add_action( 'woocommerce_before_single_product', array($this, 'wpestate_product_redirect') );
add_action( 'woocommerce_product_query', array($this, 'wpestate_custom_pre_get_posts_query' ));
add_action( 'woocommerce_order_status_completed', array($this, 'wpestate_payment_complete') );
add_action( 'woocommerce_order_status_processing', array($this, 'wpestate_payment_complete') );
//EVERYTHING WORKS ONLY WHEN THE 3 LINES BELOW ARE COMMENTED OUT
//add_action( 'woocommerce_thankyou', array($this,'order_attach') );
//add_filter( 'woocommerce_checkout_fields', array($this,'custom_override_checkout_fields') );
//add_filter('woocommerce_create_account_default_checked', '__return_true');
Can anyone provide any insight into how I can insert a remove_action & remove_filter for each in the functions.php to remove/unhook the 3 actions/filters that I currently have commented out in the plugin file?
I'm not 100% sure but my guess would be that your attempts to remove the hooks are being called before the class Wpestate_Global_Payments. This basically means they can't be removed because they haven't been added yet.
I would suggest retrying your methods but when the wp_loaded action is fired.
wp_loaded This hook is fired once WP, all plugins, and the theme are fully loaded and instantiated.
So for example, in your functions.php file...
function remove_my_themes_actions() {
remove_filters_with_method_name( 'woocommerce_thankyou', 'order_attach' );
remove_filters_with_method_name( 'woocommerce_checkout_fields', 'custom_override_checkout_fields' );
remove_filters_with_method_name( 'woocommerce_create_account_default_checked', '__return_true' );
}
add_action( 'wp_loaded', 'remove_my_themes_actions' );
If your running PHP version 5.3.0 or greater you can use an anonymous function...
add_action( 'wp_loaded', function() {
remove_filters_with_method_name( 'woocommerce_thankyou', 'order_attach' );
remove_filters_with_method_name( 'woocommerce_checkout_fields', 'custom_override_checkout_fields' );
remove_filters_with_method_name( 'woocommerce_create_account_default_checked', '__return_true' );
} );
Hope this helps!

Action init not firing after update WP 5.2.2

I recently updated my website to WP 5.2.2 and 'init' action seems not to fire anymore.. I had few VC element mapped to that action but they are not working anymore..
This is the vc_map init code:
class VC_Extensions_FancyBox extends WPBakeryShortCode {
function __construct() {
if( has_action('init') ){
die('has init'); // this is printing correctly
}
add_action( 'init', array($this, 'banner_init'));
add_shortcode('vc_fancybox', array($this, 'vc_fancybox_func'));
}
function banner_init() {
if( has_action('init') ){
die('has banner_init'); //this is not printing at all..
}
vc_map( array(........) );
}
function vc_fancybox_func() {
....
}
}
I added 2 checks in the code, the first one debugs correctly, the other one doesn't. Any idea why this is happening?
Thank you very much
EDIT: using action 'wp_loaded' the element is showing correctly...
As per Edit, I just replace 'init' to 'wp_loaded' and Worked perfectly
add_action( 'wp_loaded', array( $this, 'vc_progressbar_mapping' ) );
//add_action( 'init', array( $this, 'vc_progressbar_mapping' ) );

Remove an action - Wordpress (Specifically Woocommerce PIP plugin)

I have Woocommerce website with the plugin Print Invoices/Packing Lists installed.
I'm trying to remove an action and I've narrowed down the action to this (shortened) code;
class WC_PIP_Document_Invoice extends WC_PIP_Document {
public function __construct( array $args ) {
parent::__construct( $args );
// add a "View Invoice" link on order processing/complete emails sent to customer
add_action( 'woocommerce_email_order_meta', array( $this, 'order_paid_email_view_invoice_link' ), 40, 3 );
}
}
So I'm looking at removing this using; https://codex.wordpress.org/Function_Reference/remove_action
However as the action as added within a class, I can't quite work out what to pass into the function name. Would it be something like;
remove_action( 'woocommerce_email_order_meta', array( 'WC_PIP_Document_Invoice', 'order_paid_email_view_invoice_link' ), 40 );
Can anyone point me in the right direction?
Many thanks
Try following code that should work for you.
add_action( 'init', 'wpse_106269_remove_hooks', 11 );
function wpse_106269_remove_hooks(){
remove_action( 'woocommerce_email_order_meta', 'action_woocommerce_email_order_meta', 10, 4 );
}

Woocommerce override plugin action

The file email-order-items.php has the following line of code:
echo "\n" . sprintf( __( 'Cost: %s', 'woocommerce' ), $order->get_formatted_line_subtotal( $item ) );
The following action hook has been added to a plugin I am using (Woocommerce Composite Products):
add_action( 'woocommerce_order_formatted_line_subtotal', array( $this, 'wc_cp_order_item_subtotal' ), 10, 3 );
I would like to override the function wc_cp_order_item_subtotal to change the way to item subtotal is displayed. I have tried adding the following to my child theme functions.php, but it doesn't do anything.
remove_action( 'woocommerce_order_formatted_line_subtotal', 'wc_cp_order_item_subtotal', 10);
add_action( 'woocommerce_order_formatted_line_subtotal', 'child_wc_cp_order_item_subtotal', 10, 3);
function child_wc_cp_add_order_item_meta( $order_item_id, $cart_item_values, $cart_item_key = '' ) {
return 'xxxxxxx';
}
Any tips to help me get this working would be greatly appreciated.
It isn't mentioned in the Codex, but I usually call the remove_action() function from a hook.
Also, as covered in the Codex example for action added from classes, you need to access the class instance or variable.
I don't see wc_cp_order_item_subtotal in the Composite plugin anywhere, so I presume you aren't using Woo's version. In which case, I don't have access to the code and can't tell you exactly how to access the class variable.
But if you were using Woo's Composite Products it would be as follows:
Edited for Composites 2.4
function so_remove_action(){
global $woocommerce_composite_products;
remove_action( 'woocommerce_order_formatted_line_subtotal', array( $woocommerce_composite_products->order, 'wc_cp_order_item_subtotal' ), 10, 3 ); //not sure why it isn't a filter, but also not sure if there is a huge difference
}
add_action('init', 'so_remove_action');

WP Plugin: using add_filter inside of a class

I am using WP v3.3.1, and I am trying to make a plugin. I have gotten it semi-working. It initiated, and add_action works, but for some reason my filters don't get triggered. When I googled around, I saw I was supposed to do it like this, but it is not working. I also tried including it outside of the class, which didn't work either. The error log is written to from the constructor, but not the xmlAddMethod. I tested the xmlrpc call in a single file, and it worked, but having problems making classes.
//DOESN'T WORK HERE
add_filter( 'xmlrpc_methods', array( &$this, 'xmlAddMethod') );
class TargetDomain extends Domain
{
public function __construct()
{
error_log('TARGET: __construct');
//DOESN'T WORK HERE EITHER
add_filter( 'xmlrpc_methods', array( &$this, 'xmlAddMethod') );
parent::__construct();
}
function xmlAddMethod( $methods )
{
error_log('TARGET: xml_add_method');
$methods['myBlog.publishPost'] = 'publishMyPost';
return $methods;
}
Change this:
add_filter( 'xmlrpc_methods', array( &$this, 'xmlAddMethod') );
To:
add_filter( 'xmlrpc_methods', array( 'TargetDomain', 'xmlAddMethod') );
You can also use php's magic __CLASS__ constant.
add_filter( 'xmlrpc_methods', array( __CLASS__, 'xmlAddMethod') );

Resources