Hello I am trying to remove an action from a wordpress plugin file. The plugin is called Woocommerce Points and Rewards. I have found the action I want to remove in one of the class files. When I comment out the “add_action” it does exactly what I want. But I am trying to remove the action from functions.php in my child them. I have been reading on this and I think my problem is I need to “globalize” the class variable that the action is in; but I am not sure what that class variable isâ¦
here is the code where it adds the action (part of a file):
class WC_Points_Rewards_Cart_Checkout {
/**
* Add cart/checkout related hooks / filters
*
* @since 1.0
*/
public function __construct() {
// Coupon display
add_filter( 'woocommerce_cart_totals_coupon_label', array( $this, 'coupon_label' ) );
// Coupon loading
add_action( 'woocommerce_cart_loaded_from_session', array( $this, 'points_last' ) );
add_action( 'woocommerce_applied_coupon', array( $this, 'points_last' ) );
// add earn points/redeem points message above cart / checkout
add_action( 'woocommerce_before_cart', array( $this, 'render_earn_points_message' ), 15 );
add_action( 'woocommerce_before_cart', array( $this, 'render_redeem_points_message' ), 16 );
add_action( 'woocommerce_before_checkout_form', array( $this, 'render_earn_points_message' ), 5 );
add_action( 'woocommerce_before_checkout_form', array( $this, 'render_redeem_points_message' ), 6 );
// handle the apply discount submit on the cart page
add_action( 'wp', array( $this, 'maybe_apply_discount' ) );
// handle the apply discount AJAX submit on the checkout page
add_action( 'wp_ajax_wc_points_rewards_apply_discount', array( $this, 'ajax_maybe_apply_discount' ) );
}
The function I want to remove is this one:
add_action( 'woocommerce_before_cart', array( $this, 'render_redeem_points_message' ), 16 );
so far no luck in getting it removed; here is what I have in functions.php:
global $woocommerce, $wc_points_rewards;
/*
global $this;
*/
remove_action( 'woocommerce_before_cart', array( $this, 'render_redeem_points_message' ), 16 );
so – I am sure this can be done in this way at least I have read that it can be done, I think I just have some thing wrong on thisâ¦
I tried globalizing $this, but that just gave me an error message…
if you need to see the entire file or something else please just let me knowâ¦
So I am hoping someone on here can help me identify what I am doing wrongâ¦
** UPDATE Monday 8/18 ********
Looking for where class is instantiated; I have found this in the “woo commerce-points-and-rewards.php” file; this looks like this may be it but not sure what I am looking at;
-
does this look like where the “WC_Points_Rewards_Cart_Checkout” is instantiated?
-
And if so I am not sure how i use this to write my “remove action” in functions.php…
private function includes() {
// product class require( 'classes/class-wc-points-rewards-product.php' ); $this->product = new WC_Points_Rewards_Product(); // cart / checkout class require( 'classes/class-wc-points-rewards-cart-checkout.php' ); $this->cart = new WC_Points_Rewards_Cart_Checkout(); // order class require( 'classes/class-wc-points-rewards-order.php' ); $this->order = new WC_Points_Rewards_Order(); // discount class require( 'classes/class-wc-points-rewards-discount.php' ); $this->discount = new WC_Points_Rewards_Discount(); // actions class require( 'classes/class-wc-points-rewards-actions.php' ); $this->actions = new WC_Points_Rewards_Actions(); // manager class require( 'classes/class-wc-points-rewards-manager.php' ); // points log access class require( 'classes/class-wc-points-rewards-points-log.php' ); if ( is_admin() ) $this->admin_includes(); }
Thanks so much…
Try this:
As
$this
is an internal referrer to the class it is used in, globalizing it may not be a good thing.Does the plugin allow you to extend the class with your own and use it instead?
you found a solution for this? having the same problem with another wc plugin 😉
alright. found an answer in the wc docs. in my case:
so in your case it should be something like that:
In case anyone else is wondering, I just accomplished removing the action in this plugin in the following way.
I wanted to remove the action defined in
class-wc-points-rewards.php
on line 34:To do that, I looked at
woocommerce-points-and-rewards.php
. On line 46 it shows:And in the same file for the definition of the
WC_Points_Rewards
class, on line 249-250 it shows:With that information, I was able to remove the action:
I was able to figure out how to remove the render_redeem_points_message effectively with the following code:
To share a bit more, I wanted to create a minimum purchase amount to be able to redeem points:
I hope this helps anyone else trying to similarly expand on the WooCommerce Points and Rewards plugin.
SOLUTION-1:
In this case, as we have the plugin object’s global instance, then we can do it easily:
SOLUTION-2:
We will not be lucky always like the above solution getting the instance of the class object if any plugin author creates the class object anonymously without storing it to any global variables or keeping no method which can return it’s own instance. In those cases, we can use the following 🙂
And calling the following line appropriately when we need (may be with a hook or something):
exactly this function worked for me