- Download the free Code Snippets plugin: https://wordpress.org/plugins/code-snippets/
- 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 );
I’m marking this thread as resolved, let us know if you need anything else.