• Resolved lucasbustamante

    (@lucasbustamante)


    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() and is_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 of set_total(), I could use get_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

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter lucasbustamante

    (@lucasbustamante)

    Okay, so I think got it:

    
    foreach ( $woo_order->get_items() as $item ) {
        $product_type = wc_get_product( $item->get_product_id() )->get_type();
    
        if ( $product_type === 'pw-gift-card' ) {
    	// Do something with Gift Card being puchased
        }
    }
    		
    foreach ( $woo_order->get_items( 'pw_gift_card' ) as $gift_card ) {
        /** @var \WC_Order_Item_PW_Gift_Card $gift_card */
        // Do something with Gift Card being redeemed
    }
    
    Plugin Author pimwick

    (@pimwick)

    That looks like it would work, I’m marking this as Resolved but let us know if you need anything else.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to know at the code level if a Gift Card is being applied to an order?’ is closed to new replies.