Overriding Function from within a WooCommerce class
-
We’re using a plugin to add a “refer a friend” system to WooCommerce. This uses a coupon code in order for people who have been referred to our site to get 10% off their first purchase.
While WooCommerce lets us limit this coupon to only be able to be used once per customer, our requirements mean that we need to extend this so the coupon also gets blocked if the user has already made a purchase with us previously.
For logged in customers this is fairly straight forward as we can check this straight away when the coupon is applied; I have added a filter to woocommerce_coupon_is_valid in our functions.php to check the order count for the user if they’re logged in, and make the coupon not valid if this is > 0.
For non-logged in/guest users, this is more tricky. The way we’ve decided to implement this is to take the billing email address when the user checks out, and see if we have any previous orders from this email address (not foolproof I know, but the best compromise we could think of for at least some protection).
The code to check this is within the WC_Cart class file class-wc-cart.php. I have managed to get this working by directly editing this file; adding code to the check_customer_coupons function to do a database call and find any previous orders with the same billing email.
This works successfully, but obviously isn’t ideal as it’s editing core files. I’ve been looking for ways to move this code edit out into our functions.php file, and I found this:
https://wordpress.findincity.net/view/63538464303732726682979/wc-cart-edit-public-function
Which references the remove_anonymous_object_filter function as described here:
https://w-shadow.com/blog/2012/09/11/how-to-remove-anonymous-object-and-anonymous-function-hooks/
I’ve followed the guide, adding the function to my functions.php, and then the rest of the code from the answer given (changing the hooks given as they did not seem to be right for this hook):
//Replace the hooked function. add_action('woocommerce_after_checkout_validation','replace_customer_coupon_check',0); //Function to remove regular function and add the one we have defined above. function replace_customer_coupon_check(){ //Remove the original hooked function remove_anonymous_object_filter('woocommerce_after_checkout_validation','WC_Cart','check_customer_coupons'); //Hook on our funciton add_action('woocommerce_after_checkout_validation','custom_check_customer_coupons'); }
(Where custom_check_customer_coupons is my modified check_customer_coupons function).
However, this gives an error due to the use of $this within the function:
PHP Fatal error: Using $this when not in object context
I have then changed all the references to ‘$this->’ within my modified function to ‘WC_Cart::’, which stops the fatal error but doesn’t work as the coupon remains valid whether the customer has purchased using the same email address before or not.
Is there something I’m getting wrong here? Or is there an easier way to achieve the desired result without editing core class files in WooCommerce?
Thanks in advance!
- The topic ‘Overriding Function from within a WooCommerce class’ is closed to new replies.