Support » Plugin: PW WooCommerce Gift Cards » Get Cart Gift Card Value

  • Resolved davidgrishaver

    (@davidgrishaver)


    Hi There,

    Is there a way to get the amount/value of the gift card that has been applied to the cart?

    We add a custom surcharge (with the woocommerce fees API) that needs to account for the amount of a gift card that is applied. We run an action on the hook: woocommerce_cart_calculate_fees so hopefully we can get the gift card value at the time this executes?

    https://d.pr/free/i/Z6AqDn

    Thanks!
    Dave

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author pimwick

    (@pimwick)

    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!

    Thread Starter davidgrishaver

    (@davidgrishaver)

    This helps! It appears (so far) that I’m able to use this code in the woocommerce_cart_calculate_fees hook and have it work as expected.

        $giftCardAmt = 0;
        $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 ) {
           $giftCardAmt += $amount;
        }
    Plugin Author pimwick

    (@pimwick)

    Great, glad to hear it! I’m marking this as Resolved, let me know if you need anything else. Best of luck with your store!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Get Cart Gift Card Value’ is closed to new replies.