Skip to content

Instantly share code, notes, and snippets.

@miniMAC miniMAC/coupon.php Secret
Created Nov 8, 2019

Embed
What would you like to do?
Coupon price
<?php
/**
* WooCommerce - Apply Coupon automaticcaly in Cart
*
* @author miniMAC
*
* @link https://docs.woocommerce.com/wc-apidocs/class-WC_Coupon.html
*/
function woo_automatic_coupons() {
// Richiamo la globale
global $woocommerce;
// Nomi esatti dei coupons precedentemente creati
$couponName1 = 'sconto 10';
$couponName2 = 'sconto 20';
$couponName3 = 'sconto 30';
// Prendo i dati dai coupons
$couponData1 = new WC_Coupon($couponName1);
$couponData2 = new WC_Coupon($couponName2);
$couponData3 = new WC_Coupon($couponName3);
// Prendo l'importo minimo stabilito dai coupons
$couponMinAmount1 = $couponData1->get_minimum_amount();
$couponMinAmount2 = $couponData2->get_minimum_amount();
$couponMinAmount3 = $couponData3->get_minimum_amount();
// Prendo l'importo massimo stabilito dai coupons
$couponMaxAmount1 = $couponData1->get_maximum_amount();
$couponMaxAmount2 = $couponData2->get_maximum_amount();
$couponMaxAmount3 = $couponData3->get_maximum_amount();
// Prendo il prezzo totale attuale dal carello (escluso il costo di spedizione)
$cartTotalAmount = $woocommerce->cart->get_cart_total();
/**
* Condizioni per applicare il coupon in automatico
* se il totale del carrello è compreso nel range
* tra il valore minimo e massimo del coupon
*/
if ( $cartTotalAmount >= $couponMinAmount1 && $cartTotalAmount <= $couponMaxAmount1 ) {
$woocommerce->cart->add_discount( $couponName1 ); // Sconto 10%
} elseif ( $cartTotalAmount >= $couponMinAmount2 && $cartTotalAmount <= $couponMaxAmount2 ) {
$woocommerce->cart->add_discount( $couponName2 ); // Sconto 20%
} elseif ( $cartTotalAmount >= $couponMinAmount3 && $cartTotalAmount <= $couponMaxAmount3 ) {
$woocommerce->cart->add_discount( $couponName3 ); // Sconto 30%
}
}
add_action( 'woocommerce_before_cart', 'woo_automatic_coupons' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.