WooFrance Dépannage et support WordPress WooCommerce Forums Forum de Support WooCommerce Fonction pour appliquer un code promo automatiquement

2 sujets de 1 à 2 (sur un total de 2)
  • Auteur
    Messages
  • #4047
    Matthieu
    Participant
    Points: 17 pts

    Bonjour,

    Je me lance dans la mise en place d’un site de vente de prestations outdoor. Pour cela, mes activités sont des produits variables (Enfant / Adultes). Chaque variations n’a, bien sûr, pas le même tarif.

    Ce que je veux faire : Pour un produit/prestation donné-e, quand un client ajoute 2 adultes + 2 enfants au panier, celui-ci applique automatiquement un code promo nommé “famille”.

    J’ai trouvé quelques lignes de php à ajouter au fichier function.php de mon thème (maison) qui permet d’appliquer un code promo pour un article donné. J’ai tenté de le modifier pour qu’il tienne compte du titre de la variation (au lieu de l’article), car j’ai plusieurs produits avec les mêmes variations ainsi que des quantités de celles-ci.

    Bien entenu, ça ne fonctionne pas! Déjà, après l’ajout du produit au panier, je fini sur une page blanche, au lieu de celle de l’article. Et en allant dans le panier, le code promo n’est pas appliqué.

    Pouvez-vous m’orienter vers la solution? Ou une idée plus simple? Merci.

    Voici mon code :

    add_action(‘woocommerce_add_to_cart’, ‘wvd_add’, 30);
    add_action(‘woocommerce_after_cart_item_quantity_update’, ‘wvd_update’, 30, 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 ) {
    // Apply discount coupon when 2 kids are booked.
    $value = $product->get_available_variations();
    $title = $value->get_title();
    if ($title == “enfant”) {
    if ($values[‘quantity’] == 2) {
    if ($title == “adulte”) {
    $coupon_code = ‘famille’;
    if ($values[‘quantity’] == 2) {
    // Only apply the coupon if it has not already been applied (to avoid a warning message).
    if (!$woocommerce->cart->has_discount($coupon_code)) {
    $result = $woocommerce->cart->add_discount($coupon_code);
    }
    }
    // Remove the coupon if the quantity drops back to 1.
    if ($values[‘quantity’] == 1) {
    $woocommerce->cart->remove_coupons();
    }
    }
    }
    // Remove the coupon if the quantity drops back to 1.
    if ($values[‘quantity’] == 1) {
    $woocommerce->cart->remove_coupons();
    }
    }
    }
    }

     

    depanagewp
    #4081
    Matthieu
    Participant
    Points: 17 pts

    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();
    }
    }

    depanagewp
2 sujets de 1 à 2 (sur un total de 2)
  • Vous devez être connecté pour répondre à ce sujet.