Skip to content

Instantly share code, notes, and snippets.

@wooexperte
Last active March 26, 2021 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wooexperte/9c8fc3bd862034283f67e8dda2016489 to your computer and use it in GitHub Desktop.
Save wooexperte/9c8fc3bd862034283f67e8dda2016489 to your computer and use it in GitHub Desktop.
WooCommerce Gutschein und Coupon automatisch in den Warenkorb legen
/**
* Das Snippet fügt im Warenkorb automatisch einen WooCommerce Gutschein hinzu und zeigt einen Hinweistext an.
Für das Snippet ist einen Gutschein mit dem Titel "COUPON" in WooCommerce notwendig.
*/
add_action( 'woocommerce_before_cart' , 'add_coupon_notice' );
add_action( 'woocommerce_before_checkout_form' , 'add_coupon_notice' );
function add_coupon_notice() {
$cart_total = WC()->cart->get_subtotal();
$minimum_amount = 50;
$currency_code = get_woocommerce_currency();
wc_clear_notices();
if ( $cart_total < $minimum_amount ) {
WC()->cart->remove_coupon( 'COUPON' );
wc_print_notice( "Erhalten Sie 3% Rabatt bei einem Mindestbestellwert von $minimum_amount $currency_code!", 'notice' );
} else {
WC()->cart->apply_coupon( 'COUPON' );
wc_print_notice( 'Sie bekommen 3% Rabatt auf Ihre Bestellung!', 'notice' );
}
wc_clear_notices();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment