Need help rewrite this php code

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

This line is commented out. It appears that the data being placed into your variable is NOT a numeric value. The error message says that. If you assign a zero value to a variable using =0, then it sets the variable as numeric. If you do not set it directly, it takes the “type” from the assignment from the source. You can override this using “casting”. You basically force PHP to change the value to what you want.
Loosely, something like this $antal_vagnar = (int)$values[‘quantity’]; This forces the value, which in your code could be a NULL into an INTEGER. But, it might be better to test for NULL’s or invalid numbers.
Here is a list of possible cast’s that you can use in PHP from the PHP.net site:
The casts allowed are:

(int), (integer) - cast to int
(bool), (boolean) - cast to bool
(float), (double), (real) - cast to float
(string) - cast to string
(array) - cast to array
(object) - cast to object
(unset) - cast to NULL

Hope this helps…

Thanks for your reply. I am a beginner in php and the code is written by someone else. I have tried to use (int) before the variables, but I still get the error message, so I have must do something wrong. Just don’t know what.
/ Peter

Yes. Some code is commented out. Please ignore these. These are probably remnants of the test phase when the code was written several years ago. Probably the code worked then due to an older php version.

No problem! That is why we are all here. By the way, welcome to our site! We are here to help you learn!

This line shows that the values array has data item inside it. Also, the other one shows it has a quantity item in it. Since you did not show how this was created, I will explain arrays a little first. Arrays in PHP can be of two basic types. One which is just a list of items like $colors=array(“black”, “white”, “etc”…);
Or it can be an indexed array with keys and values. ( Which is what you have here. )
Like $contents=array(“333”=>array(“data”, “value”), 555=>array(“data”,“value”) );
In the indexed arrays, you can have other arrays. In your case, you have a package array which contains a key called “contents”. In that array, there are item-id’s and values. Inside each of the “values” array there are also keys and data. Something loosely like this:
$package = array(
“contents” => array(
“item_id”=>“values”, “item_id”=>“values”
)
)
Now, as you see, it is a nested pile of arrays. And, therefore to get one value out, you need to use the correct indexing to retrieve the data. In your case, you loop thru the first level of arrays using this:

It takes out each of the main parts of the “package” for index “contents”. This gives you an id and value.
Then, you retrieve the data from inside each as needed using these lines:

Since the second one is failing, I am guessing that you need to total up all the values in there. I am guessing that the live data in the variable is NOT what you think it it. But, instead another array. To check this, you either have to look thru the code where the “package” array is created and study what is place inside of it or just dump it and see what is in it with some live data. You can do this in many ways. You can just use var_dump() function. But, I think it is easier for beginners to use the print_r() function and display it in a formatted version. Easier to see the layout this way. Here is the code you would want to add to your code above. It will kill the script at the die() function and display your array layout in a manner that would make it easy to see what you have. It might be a large display if there is a lot of data, but, I am guessing it will only be your test data. Try this and look at the data to see if the $values[‘quantity’] is a single data amount…

As you see, I added one line just before you use the package array. It should display the entire contents of that full array show you the full layout of it’s structure. In this way you can see what data is actually inside the " $values[‘quantity’] " variable. I am guessing it is a list of values, not just one integer.
So, test it and see what you have for data. If it is not too large, you can post a copy of the output for us to see and then we can help you adjust the code to pull out the correct data. Hope this makes sense!

Thanks, I’ll test and come back.
/ Peter

Sponsor our Newsletter | Privacy Policy | Terms of Service