I’m getting the error message “A non-numeric value encountered” regarding this code sequence below. Can anyone please help me rewrite it correctly? I have markt the line with “// This is where the error occurs”.
public function calculate_cost_per_weight( $package ) {
//global $antalvagnar;
$cost = '';
$totalvikt = 0;
//$antal_vagnar = 0;
// Weight per item
foreach ( $package['contents'] as $item_id => $values ) :
$_product = $values['data'];
if ( $values['quantity'] > 0 && $_product->needs_shipping() && $_product->get_weight() ) :
//$antal_vagnar = $antal_vagnar+1;
$antal_vagnar = $values['quantity'] ;//$_product->get_weight();
//$cost += ( ( $values['quantity'] * $_product->get_weight() ) * $this->cost_per_weight );
endif;
endforeach;
//echo 'antal_vagnar:'.$antal_vagnar;
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = $chosen_methods[0];
if ( $chosen_shipping == 655 || $chosen_shipping == 657 || $chosen_shipping == 652) { /Räkna tillägg på vagnar bara inom EU/
if ($antal_vagnar == 1 ) { $cost += 820; } // This is where the error occurs
if ($antal_vagnar == 2 ) { $cost += 820; }
if ($antal_vagnar == 3 ) { $cost += 1220; }
if ($antal_vagnar == 4 ) { $cost += 1220; }
if ($antal_vagnar == 5 ) { $cost += 2150; }
if ($antal_vagnar == 6 ) { $cost += 2150; }
if ($antal_vagnar > 6 ) { $cost += 3000; }
}
if ($antal_vagnar > 0 && $antal_vagnar < 3) {
if ( $chosen_shipping == 652) { $cost +=0;} // SE
if ( $chosen_shipping == 655) { $cost +=700;} // EU 2
if ( $chosen_shipping == 657) { $cost +=1010;} // EU 3
/*if ( $chosen_shipping == 646) { $cost +=1010;} //outside EU 1*/
}
if ($antal_vagnar > 2 && $antal_vagnar < 5) {
if ( $chosen_shipping == 652) { $cost +=200;} // SE
if ( $chosen_shipping == 655) { $cost +=1420;} // EU 2
if ( $chosen_shipping == 657) { $cost +=2500;} // EU 3
/*if ( $chosen_shipping == 646) { $cost +=2500;} //outside EU 1*/
}
if ($antal_vagnar > 4 && $antal_vagnar < 7) {
if ( $chosen_shipping == 652) { $cost +=0;} // SE
if ( $chosen_shipping == 655) { $cost +=1700;} // EU 2
if ( $chosen_shipping == 657) { $cost +=3570;} // EU 3
/*if ( $chosen_shipping == 646) { $cost +=3570;} //outside EU 1*/
}
//return $antal_vagnar;
return $cost;
}
/ Peter