public static function getCartDetails($sessionId = null, $cartItems = null) { //This function returns an array that contains the sub total, shipping cost, tax cost, and total cost //If $cartItems is an array we will use it as the cart items //Otherwise, we'll get the cart items from the database based on the session ID //If $cartItems is known before calling this method, it is a good idea to pass it to this method for performance purposes //If $sessionId is null, it means we want the current session $session = Session::getActiveSession($sessionId); $sessionId = $session->getId(); if (!is_array($cartItems)) { $cartItems = CartItem::getAll($sessionId); } $userId = $session->getUser(); $userDetails = UserDetails::getUserDetailsBasedOnUserId($userId); $shippingAddress = $userDetails->getAddress('shipping_address'); $cartDetails = array("subTotal" => 0, "tax" => 0, "shipping" => 0, "total" => 0); foreach ($cartItems as $cartItem) { $product = new Product($cartItem->getProduct()); $productPrice = $cartItem->calculatePrice() * $cartItem->getQuantity(); $cartDetails["subTotal"] += $productPrice; $cartDetails["tax"] += TaxRate::calculateTax($product->getTaxClass(), $productPrice, $shippingAddress); } require_once 'plugins/shipping/ECommShipping.php'; $ECommShipping = new ECommShipping(); $shippingObj = $ECommShipping->getPlugin($session->getShippingClass()); $cartDetails["shipping"] = (double) $shippingObj->getShippingCost($sessionId, $cartItems); $cartDetails["total"] = (double) $cartDetails["subTotal"] + (double) $cartDetails["tax"] + (double) $cartDetails["shipping"]; foreach ($cartDetails as &$cartDetailsItem) { $cartDetailsItem = number_format($cartDetailsItem, 2); } return $cartDetails; }