• Resolved resorter

    (@resorter)


    Hi,

    how can we limit the gift card to Redeem only with orders equal or greater of gift card value?

    Thanks

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

    (@pimwick)

    1. Download the free Code Snippets plugin: https://wordpress.org/plugins/code-snippets/
    2. Create a new Snippet with the following code (you can add the snippet to your functions.php instead of using Code Snippets if you are more familiar with that):
    function is_gift_card_allowed( $gift_card ) {
    
        if ( !is_a( $gift_card, 'PW_Gift_Card' ) ) {
            return '';
        }
    
        $minimum_spend_amount = $gift_card->get_balance();
        $error_message = 'You must spend a minimum of ' . html_entity_decode( strip_tags( wc_price( $minimum_spend_amount ) ) ) . ' to be able to use a gift card on this order.';
    
    
        // Check the cart total (before or after gift cards have been applied).
        if ( isset( WC()->cart->pwgc_calculated_total ) ){
            $cart_total = WC()->cart->pwgc_calculated_total;
        } else {
            $cart_total = WC()->cart->get_total( 'edit' );
        }
    
        if ( $cart_total < $minimum_spend_amount ) {
            return $error_message;
        }
    
        return '';
    }
    
    function custom_pwgc_gift_card_can_be_redeemed( $message, $card_number ) {
        if ( empty( $message ) ) {
            $gift_card = new PW_Gift_Card( $card_number );
            $message = is_gift_card_allowed( $gift_card );
        }
    
        return $message;
    }
    add_filter( 'pwgc_gift_card_can_be_redeemed', 'custom_pwgc_gift_card_can_be_redeemed', 10, 2 );
    
    function custom_woocommerce_check_cart_items() {
        if ( !defined( 'PWGC_SESSION_KEY' ) ) { return true; }
    
        $session_data = (array) WC()->session->get( PWGC_SESSION_KEY );
        if ( isset( $session_data['gift_cards'] ) && count( $session_data['gift_cards'] ) > 0 ) {
            foreach ( $session_data['gift_cards'] as $card_number => $amount ) {
                $gift_card = new PW_Gift_Card( $card_number );
    
                $message = is_gift_card_allowed( $gift_card );
                if ( !empty( $message ) ) {
                    // Remove all gift cards from the session.
                    unset( $session_data['gift_cards'] );
                    WC()->session->set( PWGC_SESSION_KEY, $session_data );
    
                    // Display a notice to the customer.
                    wc_add_notice( $message, 'error' );
                    return false;
                }
            }
        }
    
        return true;
    }
    
    add_filter( 'woocommerce_check_cart_items', 'custom_woocommerce_check_cart_items', 10, 1 );
    Plugin Author pimwick

    (@pimwick)

    I’m marking this thread as resolved, let us know if you need anything else.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Redeem only with orders equal or greater’ is closed to new replies.