WooCommerce extra kosten met voorwaarden

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Cedric De Clercq

Cedric De Clercq

04/06/2016 18:08:43
Quote Anchor link
Hey,

Ik probeer om een bepaald aantal kosten toe te voegen aan de WooCommerce cart (totaalprijs winkelmandje) met een aantal voorwaarden.
De kosten zijn afhankelijk van het aantal bestelde items (in dit geval flessen, zie de site www.vinifratelliranft.eu/shop om te testen), dit is al gelukt.

Deze wanneer het subtotaal van het winkelmandje groter is dan €250 en de bestelling vanuit België gebeurt, mogen deze extra kosten wegvallen (= €0)
Voorlopig heb ik onderstaande code:

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
add_action('woocommerce_cart_calculate_fees' , 'add_custom_fees');
function add_custom_fees( WC_Cart $cart ){
    $fees = 0;
    $aantal_items = 0;
     $carttotal = $woocommerce->cart->subtotal;
        $country = $_POST['s_country'];    
    
    
    foreach( $cart->get_cart() as $item ){
    $aantal_items = $aantal_items + $item['quantity'];
}
    
    
    if ($carttotal >= 250 and $country == 'BE') {$fees = 0;}
    if ($carttotal < 250 and $aantal_items >= 1 and $aantal_items <= 6) {$fees = 8.7;}
    if ($carttotal < 250 and $aantal_items >= 7 and $aantal_items <= 12) {$fees = 10.6;}
    if ($carttotal < 250 and $aantal_items >= 13 and $aantal_items <= 18) {$fees = 11.8;}
    if ($carttotal < 250 and $aantal_items >= 19 and $aantal_items <= 24) {$fees = 20.5;}
    if ($carttotal < 250 and $aantal_items >= 25 and $aantal_items <= 30) {$fees = 22.4;}
    if ($carttotal < 250 and $aantal_items >= 31 and $aantal_items <= 36) {$fees = 23.6;}
    if ($carttotal < 250 and $aantal_items >= 37 and $aantal_items <= 42) {$fees = 32.3;}
    if ($carttotal < 250 and $aantal_items >= 43 and $aantal_items <= 48) {$fees = 34.2;}
    if ($carttotal < 250 and $aantal_items >= 49 and $aantal_items <= 54) {$fees = 35.4;}
    
        $cart->add_fee( 'Verpakkingskosten', $fees);
    }


Vergeet ik iets?

Alvast bedankt!

Gr,
Cedric
 
PHP hulp

PHP hulp

25/05/2024 18:36:49
 
Jan de Laet

Jan de Laet

05/06/2016 10:34:35
Quote Anchor link
Ik weet niet of regel 5 goed gaat zoals je nu hebt. Moet dit niet "$carttotal = $cart->subtotal;" zijn?

Je condities zijn volgens mij nog niet helemaal goed/compleet.
- Wat voor fees zijn er voor totaal > 250 en land is niet Belgie?
Ik denk dat je beter met else/elseif kunt gaan werken.

als totaal > 250 en land = Belgie
dan ...
anders
als aantal tussen 1 en 6 enz
 
Cedric De Clercq

Cedric De Clercq

05/06/2016 10:42:18
Quote Anchor link
Hey,

Wel dat had ik ook al even geprobeerd:

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
add_action('woocommerce_cart_calculate_fees' , 'add_custom_fees');
function add_custom_fees( WC_Cart $cart ){
    $fees = 0;
    $aantal_items = 0;
    $carttotal = $cart->subtotal;
    $country = $_POST['s_country'];    
    
    foreach( $cart->get_cart() as $item ){
    $aantal_items = $aantal_items + $item['quantity'];
}
    
    if($country == 'BE' ) {
        if ($carttotal < 250 ) {
            if ($aantal_items >= 1 and $aantal_items <= 6) {$fees = 8.7;}
            else if ($aantal_items >= 7 and $aantal_items <= 12) {$fees = 10.6;}
            else if ($aantal_items >= 13 and $aantal_items <= 18) {$fees = 11.8;}
            else if ($aantal_items >= 19 and $aantal_items <= 24) {$fees = 20.5;}
            else if ($aantal_items >= 25 and $aantal_items <= 30) {$fees = 22.4;}
            else if ($aantal_items >= 31 and $aantal_items <= 36) {$fees = 23.6;}
            else if ($aantal_items >= 37 and $aantal_items <= 42) {$fees = 32.3;}
            else if ($aantal_items >= 43 and $aantal_items <= 48) {$fees = 34.2;}
            else if ($aantal_items >= 49 and $aantal_items <= 54) {$fees = 35.4;}
            }
        else if ($carttotal >= 250 ) {$fees = 0;}
        }
    
        else{
            if ($aantal_items >= 1 and $aantal_items <= 6) {$fees = 8.7;}
            else if ($aantal_items >= 7 and $aantal_items <= 12) {$fees = 10.6;}
            else if ($aantal_items >= 13 and $aantal_items <= 18) {$fees = 11.8;}
            else if ($aantal_items >= 19 and $aantal_items <= 24) {$fees = 20.5;}
            else if ($aantal_items >= 25 and $aantal_items <= 30) {$fees = 22.4;}
            else if ($aantal_items >= 31 and $aantal_items <= 36) {$fees = 23.6;}
            else if ($aantal_items >= 37 and $aantal_items <= 42) {$fees = 32.3;}
            else if ($aantal_items >= 43 and $aantal_items <= 48) {$fees = 34.2;}
            else if ($aantal_items >= 49 and $aantal_items <= 54) {$fees = 35.4;}
    }
    
        $cart->add_fee( 'Verpakkingskosten', $fees);
    }
 
Jan de Laet

Jan de Laet

05/06/2016 11:34:29
Quote Anchor link
Wat zijn fees bij aantal_items > 54?

Omdat je maar 1 uitzondering hebt, is dit misschien duidelijker (en vriendelijker voor onderhoud):
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
        if ($aantal_items >= 1 and $aantal_items <= 6) {$fees = 8.7;}
        elseif ($aantal_items >= 7 and $aantal_items <= 12) {$fees = 10.6;}
        elseif ($aantal_items >= 13 and $aantal_items <= 18) {$fees = 11.8;}
        elseif ($aantal_items >= 19 and $aantal_items <= 24) {$fees = 20.5;}
        elseif ($aantal_items >= 25 and $aantal_items <= 30) {$fees = 22.4;}
        elseif ($aantal_items >= 31 and $aantal_items <= 36) {$fees = 23.6;}
        elseif ($aantal_items >= 37 and $aantal_items <= 42) {$fees = 32.3;}
        elseif ($aantal_items >= 43 and $aantal_items <= 48) {$fees = 34.2;}
        elseif ($aantal_items >= 49 and $aantal_items <= 54) {$fees = 35.4;}
        else {$fees = 123;}

//uitzondering voor BE en > 250
        if ($country == 'BE' and $carttotal >= 250) {$fees = 0;}
 
Cedric De Clercq

Cedric De Clercq

05/06/2016 14:38:08
Quote Anchor link
Fees boven de 54 items is 60.
De kosten zouden er enkel mogen bijkomen indien de methode 'Levering' ($chosen_shipping == 'woocommerce_flatrate_percountry') wordt aangeduid. Dat is gelukt.
Maar het probleem zit nog steeds bij die laatste 'if'-regel (ivm carttotal >= 250)

Dus:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
add_action('woocommerce_cart_calculate_fees' , 'add_custom_fees');
function add_custom_fees( WC_Cart $cart ){
    $fees = 0;
    $aantal_items = 0;
     $carttotal = $cart->subtotal;
    $country = $_POST['s_country'];    
    $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
    $chosen_shipping = $chosen_methods[0];
    
    foreach( $cart->get_cart() as $item ){
    $aantal_items = $aantal_items + $item['quantity'];
}

    //AFHALEN
     if ($chosen_shipping == 'local_pickup') {$fees = 0;}                      
    //LEVERING
     else if ($chosen_shipping == 'woocommerce_flatrate_percountry') {

        if ($aantal_items >= 1 and $aantal_items <= 6) {$fees = 8.7;}
        elseif ($aantal_items >= 7 and $aantal_items <= 12) {$fees = 10.6;}
        elseif ($aantal_items >= 13 and $aantal_items <= 18) {$fees = 11.8;}
        elseif ($aantal_items >= 19 and $aantal_items <= 24) {$fees = 20.5;}
        elseif ($aantal_items >= 25 and $aantal_items <= 30) {$fees = 22.4;}
        elseif ($aantal_items >= 31 and $aantal_items <= 36) {$fees = 23.6;}
        elseif ($aantal_items >= 37 and $aantal_items <= 42) {$fees = 32.3;}
        elseif ($aantal_items >= 43 and $aantal_items <= 48) {$fees = 34.2;}
        elseif ($aantal_items >= 49 and $aantal_items <= 54) {$fees = 35.4;}
        else {$fees = 60;}

//uitzondering voor BE en > 250
        if ($country == 'BE' and $carttotal >= 250) {$fees = 0;}
    
     }
    
        $cart->add_fee( 'Verpakkingskosten', $fees);
    }
 
Jan de Laet

Jan de Laet

05/06/2016 15:45:47
Quote Anchor link
Hebben $country en $carttotal wel de waarde die je verwacht na regel 5 en 6?

Waarom die if op regel 17? Wat als $chosen_shipping niet "local pickup" en niet "woocommerce_flatrate_percountry" is?
 
Cedric De Clercq

Cedric De Clercq

05/06/2016 21:12:09
Quote Anchor link
'Local pickup' en 'woocommerce_flatrate_percountry' zijn de enigste 2 mogelijkheden :) (afhalen of leveren)
En het ligt inderdaad aan die $country en $carttotal, die geven niet het juiste resultaat..
 



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.