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' ) );
LVL 8
rgranlundAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gr8gonzoConsultantCommented:
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.
0
gr8gonzoConsultantCommented:
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

0
rgranlundAuthor Commented:
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'
0
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

gr8gonzoConsultantCommented:
If you don't have a reference to the original class instance, you'll likely have to loop through the $wp_filter global variable to find the instance of the filter you're looking for, and then remove it.

So where you're currently removing the action/filter, try adding something like this (untested):

$tag = 'woocommerce_after_order_notes';
$class_method = 'woo_os_checkout_fields';

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

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rgranlundAuthor Commented:
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?
0
gr8gonzoConsultantCommented:
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.
0
rgranlundAuthor Commented:
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.
0
gr8gonzoConsultantCommented:
Did you try the previous code mentioned in #42526922?
0
rgranlundAuthor Commented:
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

0
gr8gonzoConsultantCommented:
What do you mean "but it adds it before" ?
0
rgranlundAuthor Commented:
The Class adds the the form field where it would normally add it and then adds mine where I want it.
0
gr8gonzoConsultantCommented:
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));
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.