Skip to content

Instantly share code, notes, and snippets.

@FrancoStino
Last active October 23, 2020 10:28
Show Gist options
  • Save FrancoStino/ae578f3a9d2e80b19b8a64c098d66162 to your computer and use it in GitHub Desktop.
Save FrancoStino/ae578f3a9d2e80b19b8a64c098d66162 to your computer and use it in GitHub Desktop.
Coupon / Sconti condizionali ordine minimo Woocommerce
<?php
/////////////////////////////////////////// COUPON CONDIZIONALI /////////////////////////////////////////////////////
/**
* @snippet Add A Coupon Dynamically based on Cart Subtotal with WooCommerce
* @sourcecode http://hirejordansmith.com
* @author Davide Ladisa
* @compatible WooCommerce 2.4.7
*/
add_action( 'woocommerce_before_cart', 'apply_matched_coupons' );
function apply_matched_coupons() {
global $woocommerce;
// Set your coupon codes
$tri_3 = 'Sconto 3%';
$tri_10 = 'Sconto 10%';
$tri_15 = 'Sconto 15%';
$tri_20 = 'Sconto 20%';
$tri_25 = 'Sconto 25%';
// Get the cart subtotal in non-decimal number format
$cart_subtotal = WC()->cart->subtotal_ex_tax; // TOTALE CARRELLO SENZA IVA
// If cart subtotal is less than 100 remove coupons
if ($cart_subtotal < 100) {
if ( $woocommerce->cart->has_discount( $tri_3 ) || $woocommerce->cart->has_discount( $tri_10 ) || $woocommerce->cart->has_discount( $tri_15 ) || $woocommerce->cart->has_discount( $tri_20 ) || $woocommerce->cart->has_discount( $tri_25 ) ) {
WC()->cart->remove_coupon( $tri_3 );
WC()->cart->remove_coupon( $tri_10 );
WC()->cart->remove_coupon( $tri_15 );
WC()->cart->remove_coupon( $tri_20 );
WC()->cart->remove_coupon( $tri_25 );
}
}
// If cart subtotal is greater 100 AND is less than 200 add or remove coupons
if ($cart_subtotal >= 100 && $cart_subtotal < 200 ) {
if ( $woocommerce->cart->has_discount( $tri_3 ) ) return;
if ( $woocommerce->cart->has_discount( $tri_10 ) || $woocommerce->cart->has_discount( $tri_15 ) || $woocommerce->cart->has_discount( $tri_20 ) || $woocommerce->cart->has_discount( $tri_25 ) ) {
WC()->cart->remove_coupon( $tri_10 );
WC()->cart->remove_coupon( $tri_15 );
WC()->cart->remove_coupon( $tri_20 );
WC()->cart->remove_coupon( $tri_25 );
}
$woocommerce->cart->add_discount( $tri_3 );
}
// If cart subtotal is greater than 200 AND is less than 400 add or remove coupons
if ($cart_subtotal >= 200 && $cart_subtotal < 400) {
if ( $woocommerce->cart->has_discount( $tri_10 ) ) return;
if ( $woocommerce->cart->has_discount( $tri_3 ) || $woocommerce->cart->has_discount( $tri_15 ) || $woocommerce->cart->has_discount( $tri_20 ) || $woocommerce->cart->has_discount( $tri_25 ) ) {
WC()->cart->remove_coupon( $tri_3 );
WC()->cart->remove_coupon( $tri_15 );
WC()->cart->remove_coupon( $tri_20 );
WC()->cart->remove_coupon( $tri_25 );
}
$woocommerce->cart->add_discount( $tri_10 );
}
// If cart subtotal is greater than 400 AND is less than 700 add or remove coupons
if ($cart_subtotal >= 400 && $cart_subtotal < 700) {
if ( $woocommerce->cart->has_discount( $tri_15 ) ) return;
if ( $woocommerce->cart->has_discount( $tri_3 ) || $woocommerce->cart->has_discount( $tri_10 ) || $woocommerce->cart->has_discount( $tri_20 ) || $woocommerce->cart->has_discount( $tri_25 ) ) {
WC()->cart->remove_coupon( $tri_3 );
WC()->cart->remove_coupon( $tri_10 );
WC()->cart->remove_coupon( $tri_20 );
WC()->cart->remove_coupon( $tri_25 );
}
$woocommerce->cart->add_discount( $tri_15 );
}
// If cart subtotal is greater than 700 AND is less than 1000 add or remove coupons
if ($cart_subtotal >= 700 && $cart_subtotal < 1000) {
if ( $woocommerce->cart->has_discount( $tri_20 ) ) return;
if ( $woocommerce->cart->has_discount( $tri_3 ) || $woocommerce->cart->has_discount( $tri_10 ) || $woocommerce->cart->has_discount( $tri_15 ) || $woocommerce->cart->has_discount( $tri_25 ) ) {
WC()->cart->remove_coupon( $tri_3 );
WC()->cart->remove_coupon( $tri_10 );
WC()->cart->remove_coupon( $tri_15 );
WC()->cart->remove_coupon( $tri_25 );
}
$woocommerce->cart->add_discount( $tri_20 );
}
// If cart subtotal is greater than 1000
if ($cart_subtotal >= 1000) {
if ( $woocommerce->cart->has_discount( $tri_25 ) ) return;
if ( $woocommerce->cart->has_discount( $tri_3 ) || $woocommerce->cart->has_discount( $tri_10 ) || $woocommerce->cart->has_discount( $tri_15 ) || $woocommerce->cart->has_discount( $tri_20 ) ) {
WC()->cart->remove_coupon( $tri_3 );
WC()->cart->remove_coupon( $tri_10 );
WC()->cart->remove_coupon( $tri_15 );
WC()->cart->remove_coupon( $tri_20 );
}
$woocommerce->cart->add_discount( $tri_25 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment