WooCommerce extra kosten met voorwaarden
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)
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
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);
}
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
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
Wel dat had ik ook al even geprobeerd:
Code (php)
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
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);
}
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);
}
Omdat je maar 1 uitzondering hebt, is dit misschien duidelijker (en vriendelijker voor onderhoud):
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
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;}
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;}
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)
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
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);
}
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);
}
Waarom die if op regel 17? Wat als $chosen_shipping niet "local pickup" en niet "woocommerce_flatrate_percountry" is?
En het ligt inderdaad aan die $country en $carttotal, die geven niet het juiste resultaat..