murada Posted October 20, 2014 Share Posted October 20, 2014 Posted this in the help section but I realized I would have better luck posting this in this forum. I need some urgent help with a simple script a friend wrote for me but isn't returning the correct values. I'm looking to get this script working but my PHP skills are abysmal and I can't for the life of me figure out whats the best way to do this. I've tried contacting my friend but he's moving cross-country so I'm hoping someone could help me quickly. I require the script to be working for a small project of mine so I need some help within the next 3-4 hrs if possible. The script is supposed to return a total shipping price based on the weight of products that are passed to it via an API and the location of the visitor. There are a total of 3 different products (Product A, Product B, Product C) each with it's own weight in grams and it's own shipping rate. The script is needs to follow the following rules: When 1 product (either Product A, B, or C) is in the cart it returns the full fixed shipping price for either US or International Shipping based on $isInternational When 2 or more of Product A or B are added to the cart the full shipping is taken for the heaviest item and a fixed price is added for each additional item (Product and B each have their own fixed prices) When Product C is added to the cart it returns a fixed based on US or International (Product C ships separately and doesn't depend on the weight/prices of Product A or B) The product weights/prices are: Product A: Weight (grams): 250 Us Shipping for first instance: $6.00 International Shipping for first instance: $10.00 Each additional instance: $1.00 Product B: Weight (grams): 300 Us Shipping for first instance: $8.00 International Shipping for first instance: $11.00 Each additional instance: $2.50 Product C: Weight (grams): 120 Us Shipping for each instance: $8.00 International Shipping for each instance: $20.00 An example would be if the cart contained 2 of Product B, 1 of Product A, and 2 of Product C shipping to the US: The price would be 8+2.50+1+8+8=27.50 The script currently ignores the addition of more instances of the items and doesn't take into account US vs International: <? header('Content-Type: application/json'); $json = file_get_contents('php://input'); $request_body = json_decode($json, true); if (is_null($request_body) or !isset($request_body['eventName'])) { header('HTTP/1.1 400 Bad Request'); $response = array( "rates" => "" ); die(json_encode($response)); } switch ($request_body['eventName']) { case 'shippingrates.fetch': // ============= recieve data from cart ============== $country_code = $request_body["content"]["billingAddressCountry"]; $state_code = $request_body["content"]["billingAddressProvince"]; $isInternational = ($country_code != "US"); $quantity = 0; $maxWeight = 0; $cost = 0; $description = "International Shipping"; if($request_body["content"] && $request_body["content"]["items"]){ foreach ($request_body["content"]["items"] as $cart_item){ $quantity += $cart_item["quantity"]; if($cart_item["weight"] >= 300){ $cost += 2.50; } else if($cart_item["weight"] >= 200){ $cost += 1.0; } else if($cart_item >= 100){ $cost += ($isInternational)? 15.0 : 8.0; } if($cart_item["weight"] > $maxWeight){ $maxWeight = $cart_item["weight"]; } } } if($maxWeight >= 300) { $cost += ($isInternational) ? 11.0 : 8.0; $cost -= 2.50; } else if ($maxWeight >= 200) { $cost += ($isInternational) ? 10.0 : 6.0; $cost -= 1; } if($isInternational){ $description = "International Shipping"; } else { $description = "USPS Standard Shipping"; } // =========== return data to cart =============== $rates = array( array( "cost" => $cost, "description" => $description ), ); $response = array( "rates" => $rates ); header('HTTP/1.1 200 OK'); echo(json_encode($response)); break; default: header('HTTP/1.1 200 OK'); break; } ?> Link to comment
Recommended Posts