Remove coupons from a cart when a customer applies a gift card to pay for the order.
Note:
This is a Developer level doc. If you are unfamiliar working with code and resolving potential conflicts, we recommend you work with a Woo Agency Partner for larger projects, or find a WooCommerce developer on Codeable for smaller customizations. We are unable to provide support for customizations under our Support Policy.
Test custom code on a staging site before using it on your store. Add PHP to a custom plugin or a child theme’s functions.php file. If the file is already inside a PHP block, omit the opening PHP tag.
Use this code to remove any applied coupons when a customer uses a gift card to pay. This is different from the Block gift card discounts setting, which controls discounts on gift card products being purchased.
<?php
add_action( 'woocommerce_check_cart_items', 'sw_gc_check_cart_coupons' );
/**
* Remove coupons when a gift card is applied to the cart.
*/
function sw_gc_check_cart_coupons() {
$using_giftcards = ! empty( WC_GC()->giftcards->get() );
if ( $using_giftcards ) {
foreach ( WC()->cart->get_applied_coupons() as $code ) {
$coupon = new WC_Coupon( $code );
$coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_INVALID_REMOVED );
WC()->cart->remove_coupon( $code );
}
}
}
Verify the change
↑ Back to topOn a staging site, apply a coupon and a gift card to a cart and confirm the coupon is removed. Repeat the test with the gift card added first. Check the cart and checkout your store uses.
Questions and support
↑ Back to topSomething missing from this documentation? Still have questions and need assistance?
- If you have a question about a specific extension or theme you’d like to purchase, contact us to get answers.
- If you already purchased this product and need some assistance, get in touch with a Happiness Engineer via our support page and select this product’s name from the Product dropdown.