Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

185
Views
Cálculo del costo de envío basado en la distancia y el peso con la fórmula matemática de php

He agregado el código a continuación con el mecanismo de envío y precio en los comentarios, creo que la última declaración elseif no es correcta y es posible que esté haciendo todo el cálculo incorrectamente. Cualquier ayuda para corregir esto o señalar el método correcto es muy apreciada. Gracias.

 <?php // Shipping mechanism for under 10kg // 0 - 5 km - Price $5.00 // 5 - 10 km - Price $1.00 / per km // > 10 km - Price $0.80 / per km // Shipping mechanism for over 10kg and under 30kg // 0 - 5 km - Price $8.00 // 6 - 15 km - Price $1.00 / per km // > 16 km - Price $1.50 / per km function calc_rockrider_cost($meters, $weight) { // $meters is coming from another calc_distance function using google api $km = round($meters/1000); $cost = NULL; if( $weight <= 10000 ) { if( $km <= 5.0 ) { $cost = 5.00; } elseif( $km > 5.0 && $km <= 10.0) { $cost = 5.00 + (1.0*($km - 5.0)); } elseif ( $km > 5.0 && $km > 10.0) { $cost = 5.00 + (0.8*($km - 10.0)); <- mistake ? } } else { if( $km <= 5.0 ) { $cost = 8.00; } elseif( $km > 5.0 && $km <= 15.0) { $cost = 8.00 + (1.0*($km - 5.0)); } elseif ( $km > 5.0 && $km > 16.0) { $cost = 8.00 + (1.5*($km - 15.0)); <- mistake ? } } // need and else here for more than 30kg return $cost; // get costing based on distance and weight } function shipping_calc_init(){ //$distance from calc_distance function using google api //900 is weight ( 900 grams ) $price = calc_rockrider_cost($distance, 900); echo 'Rock Rider price ' . number_format(round($price), 2)."<br>"; echo 'Distance: '.((int)$distance / 1000).' Km <br>'; echo 'Weight: 900gm '; } shipping_calc_init(); // when running this, i get // Rock Rider price 16.00 <- not right // Distance: 24.015 Km // Weight: 900gm
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Se me ocurrió la función a continuación.

 function calc_rockrider_cost($meters, $grams) { // invalid weight? if ($grams > 30000) return false; // determine cost parameters $costPerKilometer = 0.00; if( $grams <= 10000 ) { $startingCost = 5.00; if ($meters > 10000 ) { $costPerKilometer = 0.80; } elseif ($meters > 5000 ) { $costPerKilometer = 1.00; } } else { $startingCost = 8.00; if ($meters > 15000 ) { $costPerKilometer = 1.50; } elseif ($meters > 5000 ) { $costPerKilometer = 1.00; } } // calculate cost based on parameters return $startingCost + round($meters / 1000) * $costPerKilometer; }

En primer lugar, cambié $weight a $grams para que sea similar al parámetro $meters de la función. La alternativa serían los parámetros $distance y $weight .

Luego separé el cálculo del costo de determinar el valor de los parámetros de costo. De esta manera, el cálculo de costos solo se realiza en un solo lugar, lo que facilita el mantenimiento del código.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!