How to know at the code level if a Gift Card is being applied to an order?
-
Hey,
I’m trying to differentiate between an order where a Gift Card is being purchased from an order where a Gift Card is being redeemed. This is necessary for a complex internal report system. Can you help me implement the
is_purchasing_gift_card()
andis_redeeming_a_gift_card()
methods bellow?add-action('woocommerce_order_status_changed', function( int $order_id, string $status_from, string $status_to, WC_Order $order ) { if ( $this->is_purchasing_gift_card() ) { die("You're purchasing a gift card!"); } elseif ( $this->is_redeeming_a_gift_card() ) { die("You're redeeming a gift card!"); } });
At first, I tried this:
private function is_redeeming_a_gift_card(): bool { // If we have a Gift Card defined in the session, we are redeeming a Gift Card return ! empty( (array) WC()->session->get( PWGC_SESSION_KEY ) ); } private function is_purchasing_a_gift_card(): bool { // If we have a Woo line item for "pw_gift_card", we are purchasing a Gift Card return ! empty( $woo_order->get_items( 'pw_gift_card') ); }
But then here
pw-gift-cards/includes/pw-gift-cards-redeeming.php:138
I see that it calculates the total for the redeeming gift cards using$order->get_items( 'pw_gift_card' )
, not the session. So it expects a redeeming card to be an order line item, so my code above wouldn’t work.I also see that it gets the value from the session and save it as order items on
pw-gift-cards/includes/pw-gift-cards-redeeming.php:268
.This way, both when redeeming and purchasing a Gift Card they show up in the order as Order Items.
So I’m not seeing a way to differentiate between a Gift Card being redeemed and a Gift Card being purchased at the code level, you see?
If the calculation on line 163 was using
set_discount()
instead ofset_total()
, I could useget_discount()
to find out, but unfortunately, it’s not…I would really appreciate if you could help me out. I have the premium version, by the way.
Thanks,
Lucas
- The topic ‘How to know at the code level if a Gift Card is being applied to an order?’ is closed to new replies.