Skip to content

Instantly share code, notes, and snippets.

@MWDelaney
Last active February 6, 2025 07:53
Show Gist options
  • Select an option

  • Save MWDelaney/0200a347e552350398ee4fe4cc7a10ce to your computer and use it in GitHub Desktop.

Select an option

Save MWDelaney/0200a347e552350398ee4fe4cc7a10ce to your computer and use it in GitHub Desktop.

Revisions

  1. MWDelaney revised this gist Apr 12, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,9 @@ function mwd_get_applied_coupons() {
    }
    }
    // Remove any other coupons of this type
    // Loop through all other applied coupons
    foreach ( WC()->cart->get_coupons() as $code => $coupon ) {
    // Check whether each coupon matches the type of the coupon we just applied
    if($coupon->code != $_POST['coupon_code'] && $coupon->discount_type == $type_to_remove) {
    // Exclude "smart_coupon" type, since users should be able to use as many gift cards as they like
    if($coupon->discount_type != 'smart_coupon') {
  2. MWDelaney created this gist Apr 12, 2016.
    27 changes: 27 additions & 0 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    <?php

    // Hook when a coupon is applied to a cart
    add_action( 'woocommerce_applied_coupon', 'mwd_get_applied_coupons' );

    // Get the current applied coupon and compare it with other applied coupons
    function mwd_get_applied_coupons() {
    // Get the currently applied coupon's type using the $_POST global to retrieve the current coupon's code
    foreach ( WC()->cart->get_coupons() as $code => $coupon ) {
    if($coupon->code == $_POST['coupon_code']) {
    // Get this coupon's type so we can remove all other coupons of this type from the cart
    $type_to_remove = $coupon->discount_type;
    }
    }
    // Remove any other coupons of this type
    foreach ( WC()->cart->get_coupons() as $code => $coupon ) {
    if($coupon->code != $_POST['coupon_code'] && $coupon->discount_type == $type_to_remove) {
    // Exclude "smart_coupon" type, since users should be able to use as many gift cards as they like
    if($coupon->discount_type != 'smart_coupon') {
    // Remove all coupons that match the type of the coupon we just applied
    WC()->cart->remove_coupon( $coupon->code );
    }
    }
    }
    }

    ?>