WooFrance Dépannage et support WordPress WooCommerce › Forums › Forum de Support WooCommerce › Fonction pour appliquer un code promo automatiquement
- Ce sujet contient 1 réponse, 1 participant et a été mis à jour pour la dernière fois par
Matthieu, le il y a 7 années et 10 mois.
-
AuteurMessages
-
15 juillet 2017 à 22 h 37 min #4081
Jescription m’auto-repond car j’ai trouvé une solution qui me satisfait.
====================//réductions
add_action(‘woocommerce_add_to_cart’, ‘wvd_add’, 2);
add_action(‘woocommerce_after_cart_item_quantity_update’, ‘wvd_update’, 2, 2);function wvd_add() {
wvd_add_discount();
}
function wvd_update($cart_item_key, $quantity) {
wvd_add_discount();
}
function wvd_add_discount(){
global $woocommerce;// Check each item in the cart
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
//var_dump($values);// valide la réduction quand 2 enfants sont réservés.
if ($values[‘variation’][‘attribute_pa_ages’] == ‘enfant’) {
if ($values[‘quantity’] >= 2) {
$enfants = true;
}
if ($values[‘quantity’] <= 1) {
$enfants = false;
}
}
// valide la réduction quand 2 adultes sont réservés.
if ($values[‘variation’][‘attribute_pa_ages’] == ‘adulte’) {
if ($values[‘quantity’] >= 2) {
$adultes = true;
}
if ($values[‘quantity’] <= 1) {
$adultes = false;
}
}
}
//Choix du coupon
//$term_list = wp_get_post_terms($values[‘product_id’],’product_cat’,array(‘fields’=>’ids’));
//$cat_id = (int)$term_list[0];
//$cat = basename(get_term_link ($cat_id, ‘product_cat’), « / »);
$cat = »;
$demij = array( 6318, 6313, 6304, 6266, 6122, 6119, 6107, 6104 );
$journee = array( 6175, 6125, 6115 );
$bivouac = array( 6128 );
$enduro = array( 6308, 6270, 6100 );
$initvtt = array( 6170 );
$test = array( 6326, 6332, 6331 );
if ($cat == ’12j’ OR in_array($values[‘product_id’], $demij) ) {
$coupon_code = ‘famille1/2j’;
}
if ($cat == ‘journee’ OR in_array($values[‘product_id’], $journee)) {
$coupon_code = ‘famillej’;
}
if ($cat == ‘bivouac’ OR in_array($values[‘product_id’], $bivouac)) {
$coupon_code = ‘famillebivouac’;
}
if ($cat == ‘enduro’ OR in_array($values[‘product_id’], $enduro)) {
$coupon_code = ‘familleendurovtt’;
}
if ($cat == ‘initiation’ OR in_array($values[‘product_id’], $initvtt)) {
$coupon_code = ‘familleinitvtt’;
}
if ($cat == ‘test’ OR in_array($values[‘product_id’], $test)) {
$coupon_code = ‘famille1/2j’;
}
else{
$woocommerce->cart->remove_coupons();
}//Applique la bonne réduction
if ($enfants === true && $adultes === true) {
var_dump(« coupon : » . $coupon_code);
if (!$woocommerce->cart->has_discount($coupon_code)) {
$result = $woocommerce->cart->add_discount($coupon_code);
}
}
else {
$woocommerce->cart->remove_coupons();
}
}
-
AuteurMessages
- Vous devez être connecté pour répondre à ce sujet.