private static function _get_cart_delivary_data() { $cart_detail = CartService::instance()->get(); if (empty($cart_detail['cart_product'])) { return array(); } $delivery_cats = array(); foreach ($cart_detail['cart_product']['good'] as $good) { if (key_exists($good['delivery_category_id'], $delivery_cats)) { $delivery_cats[$good['delivery_category_id']]['weight'] += $good['weight'] * $good['quantity']; $delivery_cats[$good['delivery_category_id']]['total_price'] += $good['price'] * $good['quantity']; } else { $delivery_cats[$good['delivery_category_id']] = array('weight' => $good['weight'] * $good['quantity'], 'total_price' => $good['price'] * $good['quantity']); } } //如果是赠品需要物流,相应添加赠品的物流价格 if (isset($cart_detail['cart_product']['gift'])) { $gifts = $cart_detail['cart_product']['gift']; foreach ($gifts as $gift) { if (key_exists($gift['delivery_category_id'], $delivery_cats)) { $delivery_cats[$gift['delivery_category_id']]['weight'] += $gift['weight'] * $gift['quantity']; $delivery_cats[$gift['delivery_category_id']]['total_price'] += $gift['price'] * $gift['quantity']; } else { $delivery_cats[$gift['delivery_category_id']] = array('weight' => $gift['weight'] * $gift['quantity'], 'total_price' => $gift['price'] * $gift['quantity']); } } } return $delivery_cats; }
$cartJson = $app->request()->getBody(); $newCart = json_decode($cartJson, true); if ($newCart) { $cart = CartService::add($newCart); $app->response()->header('Content-Type', 'application/json'); $app->response()->setStatus(200); echo json_encode($cart); } else { $app->response->setStatus(400); echo "Malformat JSON"; } }); $app->get('/carts/', function () use($app) { $carts = CartService::listCarts(); if ($carts) { $app->response()->header('Content-Type', 'application/json'); $app->response()->setStatus(200); echo json_encode($carts); } else { $app->response()->setStatus(204); } }); $app->delete('/carts/:id', function ($id) use($app) { if (CartService::delete($id)) { echo "Cart with id = {$id} was deleted"; } else { $app->response->setStatus('404'); echo "Cart with id = {$id} not found"; } }); $app->run();