Hi Dave,
We determine the valid amount for the gift card in the “woocommerce_after_calculate_totals” hook. This occurs after the woocommerce_cart_calculate_fees hook.
See this file:
/pw-gift-cards/includes/pw-gift-cards-redeeming.php
We need to know the total amount of the cart before the gift card amount is applied. You may want to hook into “woocommerce_after_calculate_totals” with a later priority (we use priority 40 as you will see in the source code). If you hook in after then you can access the gift card amount using code like this:
$session_data = (array) WC()->session->get( PWGC_SESSION_KEY );
if ( !isset( $session_data['gift_cards'] ) ) {
return;
}
foreach ( $session_data['gift_cards'] as $card_number => $amount ) {
// Do something with $amount here
}
Keep in mind that a customer can redeem multiple gift cards on the same order, so that is why it is an array.
Once you have the total sum of gift card balances being applied, you could remove your “woocommerce_after_calculate_totals” hook and force the cart to recalculate again.
So the process could be something like:
1. woocommerce_after_calculate_totals fires for the PW Gift Cards plugin.
2. woocommerce_after_calculate_totals fires for your plugin.
3. You calculate the total gift card amounts.
4. Unhook your woocommerce_after_calculate_totals
5. Recalculate the cart. Something like WC()->cart->calculate_totals() should work.
6. Hook your woocommerce_after_calculate_totals again.
Hope this helps!