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:


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
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
Hey,

Wel dat had ik ook al even geprobeerd:


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);
    }
Wat zijn fees bij aantal_items > 54?

Omdat je maar 1 uitzondering hebt, is dit misschien duidelijker (en vriendelijker voor onderhoud):

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

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:

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

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?
'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..

Reageren