Sunday, April 8, 2007

PHP Dynamic Google Base Shipping Costs based on rules

Use the code below to connect to your database containing your prices and then use a bit of hard coded PHP to pick the correct shipping cost to include in the google base XML feed. Comment away on how you could do it better. Below is based on a Price Level shipping structure, but it could be modified to use weights or a dynamic allocation of shipping charges.

mysql_connect("urdatabase","xxx","pass");
//select which database you want to connect
mysql_select_db("urdatabase");
$result = mysql_query("select
Replace(Price, '$', '') as Price from urproductstable;");;
// i like always having numrows available even though I'm not using it here
$num_rows = mysql_num_rows($result);
while($r=mysql_fetch_array($result))
{
//skip the rest of the xml attributes to get directly to the shipping cost code
if ( $r["Price"] <= 24.99 ) { $shippingcost = "3.95"; } elseif ( $r["Price"] >= 25 and $r["Price"] <= 49.99 ){ $shippingcost = "4.95"; } elseif ( $r["Price"] >= 50 and $r["Price"] <= 74.99 ){ $shippingcost = "5.95"; } elseif ( $r["Price"] >= 75 and $r["Price"] <= 99.99 ){ $shippingcost = "6.95"; } echo '
US
Ground
'.$shippingcost.'

';}

No comments: