Search code examples
phpwordpresswoocommercehook-woocommercecoupon

woocommerce_get_shop_coupon_data being called 3 times


I am writing a plugin for Woocommerce to do an api call to a coupon provider and I'm hooking into woocommerce_get_shop_coupon_data with:

add_filter( 'woocommerce_get_shop_coupon_data', 'wigroup_coupon_injecion', 1, 3  );

All is working as expected, but this hook is called 3 times on each page load, as well as when a coupon is entered, or removed.

So my question:

How to hook into the coupon being applied other than woocommerce_get_shop_coupon_data?

Is it normal for it being called 3 times each time?

Just on a side note on the woocommerce Cart, I need a unique Identifier for each cart. I do understand there is only a unique identifier when the order is completed, But I need to pass a identifier to each coupon transaction.

Any ideas there would be appreciated.


Solution

  • You should better track customer events related to coupons instead:

    1. When customer apply a coupon code using woocommerce_applied_coupon action hook:

      add_action('woocommerce_applied_coupon', 'action_applied_coupon', 10, 1 );
      function action_applied_coupon( $coupon_code ) {
          // Your code here
      }
      
    2. When customer remove a coupon code using woocommerce_removed_coupon action hook:

      add_action('woocommerce_removed_coupon', 'action_removed_coupon', 10, 1 );
      function action_removed_coupon( $coupon_code ) {
          // Your code here
      }
      

    This hooks will be called one time only for each customer event regarding coupons…