Skip to content

Instantly share code, notes, and snippets.

@mtruitt
Created November 15, 2017 20:18
Show Gist options
  • Select an option

  • Save mtruitt/25e52ef4be4671cf6f7237f6078bfe56 to your computer and use it in GitHub Desktop.

Select an option

Save mtruitt/25e52ef4be4671cf6f7237f6078bfe56 to your computer and use it in GitHub Desktop.
Auto applies Coupon w/ use of a different coupon.
<?php
/*
* Auto applies coupon code based on another coupon code and removes all if either is removed
*/
function auto_apply_coupon() {
$applied_coupon_code = 'Coupon manually applied goes here';
$coupon_code = 'coupon you want auto applied';
if ( WC()->cart->has_discount($applied_coupon_code) ) {
WC()->cart->add_discount( $coupon_code );
WC()->cart->calculate_totals();
} elseif ( !WC()->cart->has_discount($applied_coupon_code) && WC()->cart->has_discount($coupon_code) ) {
WC()->cart->remove_coupons();
WC()->cart->calculate_totals();
}
};
add_action( 'woocommerce_before_cart_table','auto_apply_coupon' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment