Link to home
Start Free TrialLog in
Avatar of Robert Granlund
Robert GranlundFlag for United States of America

asked on

Remove an action from a Class Function

Wordpress remove a function from a class.
I want to remove a function from a class and in turn add a function.  The adding the function works 100%  but what I want to remove does not.
 What I want to remove from the following function is:
      // add custom fields to checkout page
            add_action( 'woocommerce_after_order_notes', array( $this, 'woo_os_checkout_fields' ) );

Here is the original Class:
<?php
public function add_hooks() {
	class Woo_Os_Public {			
			// checkout process
			add_action( 'woocommerce_checkout_process', array( $this, 'woo_os_checkout_field_process' ) );
			
			// add custom fields to checkout page
		add_action( 'woocommerce_after_order_notes', array( $this, 'woo_os_checkout_fields' ) );
			
			// save checkout custom meta
			add_action( 'woocommerce_checkout_update_order_meta', array( $this, 'woo_os_checkout_field_update_order_meta' ), 10, 1 );
			
			// add action to add an extra detail on order confirmation page
			add_action( 'woocommerce_order_details_after_order_table', array( $this, 'woo_os_woocommerce_order_details_after_order_table' ),10, 1 );
			
			//add action to add custom detail in woocommerce email for customer and admin.
			add_action( 'woocommerce_email_after_order_table',array( $this,  'woo_os_woocommerce_email_after_order_table' ), 10, 4 );
			
			// display Agreement PDF ( V 1.0.4 )
			add_action( 'init', array( $this, 'woo_os_agreement_pdf_view' ), 100 );
		}
}

Open in new window


Here is my code.
$woo_os_public = new Woo_Os_Public();
//  This filter adjusts where on the page the sign pad appears 
    class Wizard_class extends Woo_Os_Public {

        public function add_hooks() {
            remove_action( 'woocommerce_after_order_notes', array( $this, 'woo_os_checkout_fields' ) );
			// add custom fields to checkout page
			add_action('woocommerce_checkout_after_customer_details', array($this, 'woo_os_checkout_fields'));
			

        }

    }

$wizard_public = new Wizard_class;
$wizard_public->add_hooks();

Open in new window

What I want to remove from the original Class/method is : remove_action( 'woocommerce_after_order_notes', array( $this, 'woo_os_checkout_fields' ) );
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Usually if remove_action() doesn't work, then either:
1. You've got the wrong action / hook name.
2. You're calling remove too early - the action might not have been added yet.
3. You have a bad reference - e.g. you're passing array($this, "woo_os_checkout_fields") as the callback, but $this refers to the extended "Wizard_class" instance that you've just created, so if there's some other class instance that has the added action, then you need to remove THAT instance's callback, not $this one.
If you're just trying to clear all filters that match that specific tag (maybe because you know there's only one and you don't want to pass around an object instance), you can also use remove_all_filters():
https://codex.wordpress.org/Function_Reference/remove_all_filters

remove_all_filters('woocommerce_after_order_notes');

Open in new window

Avatar of Robert Granlund

ASKER

Thank you for your response, that helps me clarify my question:

I want to add "woo_os_checkout_fields" to "woocommerce_checkout_after_customer_details" not to "woocommerce_after_order_notes".

So I guess what I am asking is How do I first remove it from the original plugin and then add the new action with my plugin?
I can't clear them all because there are others that are hooking to 'woocommerce_after_order_notes'
ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'm not sure I follow "If you don't have a reference to the original class instance,"  The function is in the class Woo_Os_Public { Class.  I left out that code because the page is really long.  The function woo_os_checkout_fields basically builds a form field.  Is that what you are asking?  What Class is "woo_os_checkout_fields" in?
Oh wait - I just saw that you have this line:
$woo_os_public = new Woo_Os_Public();

...which might be the original class instance.

So to understand this, let's say you have these two classes:

class Woo_Os_Public
{
  public function add_hooks()
  {
    add_action( 'action_xyz', array( $this, 'handle_xyz' ) );
  }

  public function handle_xyz()
  {
    ...some code here...
  }
}

Open in new window


class Wizard_class extends Woo_Os_Public
{
  public function add_hooks()
  {
    remove_action( 'action_xyz', array( $this, 'handle_xyz' ) );
  }
}

Open in new window


Then you actually use those two classes:
$woo_os_public = new Woo_Os_Public();
$woo_os_public->add_hooks();

$wizard_public = new Wizard_class();
$wizard_public->add_hooks();

Open in new window


You have two different class instances here, $woo_os_public and $wizard_public.

In $wizard_public's class, you're trying to remove a previously-added action using the $this variable:
remove_action( 'action_xyz', array( $this, 'handle_xyz' ) );

However, in your Wizard class, $this refers back to $wizard_public, and $wizard_public wasn't the class instance that added the action, so the removal isn't going to work, because you're trying to remove something that doesn't exist. Rather, the class instance that added the action was $woo_os_public. So for that remove_action to work properly, you have to pass $woo_os_public to the remove_action call (and you have to use global to bring the variable into the current scope):

global $woo_os_public;
remove_action( 'action_xyz', array( $woo_os_public, 'handle_xyz' ) );

Now, what I was saying before was that I didn't see the line of code where you had actually created the instance of Woo_Os_Public:
$woo_os_public = new Woo_Os_Public();

...so I didn't know if your code would be able to access that variable or not. But since you DO have it, then I'm assuming that's the correct name and just fixing the remove_action statement as I showed above will likely work.
This code does not remove "add_action( 'woocommerce_after_order_notes', array( $this, 'woo_os_checkout_fields' ) );"

<?php
$woo_os_public = new Woo_Os_Public();
$woo_os_public->add_hooks();
//  This filter adjusts where on the page the sign pad appears 
    class Wizard_class extends Woo_Os_Public {

        public function add_hooks() {
            global $woo_os_public;
             remove_action( 'woocommerce_after_order_notes', array( $woo_os_public, 'woo_os_checkout_fields' ) );	
// add custom fields to checkout page
	add_action('woocommerce_checkout_after_customer_details', array($this, 'woo_os_checkout_fields'));			

        }
    }

$wizard_public = new Wizard_class;
$wizard_public->add_hooks();

Open in new window


Am I missing something simple?  It does not remove it.  However, it does add it before my code runs.
Did you try the previous code mentioned in #42526922?
I tried the following but it adds it before

$woo_os_public = new Woo_Os_Public();
$woo_os_public->add_hooks();

global $wp_filter ;
if (isset($wp_filter[$tag]))
{
  // Find all callbacks for the given filter/tag
  foreach ($wp_filter[$tag] as $filterIDX => $callbacks)
  {
    foreach ($callbacks as $callback_name => $callback_definition)
    {
      // Check to see if it's callback matches our method name
      if (strpos($callback_name, $class_method) !== false)
      {
        // It does, remove it from the filters list
        unset($wp_filter[$tag][$filterIDX][$callback_name]) ;
      }
    }
  }
}
//  This filter adjusts where on the page the sign pad appears 
    class Wizard_class extends Woo_Os_Public {

        public function add_hooks() {
            global $woo_os_public;
             remove_action( 'woocommerce_after_order_notes', array( $woo_os_public, 'woo_os_checkout_fields' ) );	
// add custom fields to checkout page
	add_action('woocommerce_checkout_after_customer_details', array($this, 'woo_os_checkout_fields'));			

        }
    }

$wizard_public = new Wizard_class;
$wizard_public->add_hooks();

Open in new window

What do you mean "but it adds it before" ?
The Class adds the the form field where it would normally add it and then adds mine where I want it.
After the $wizard_public->add_hooks(), can you log the contents of $wp_filter to a file and paste it here?

file_put_contents("wp_filter.log",print_r($wp_filter,true));