public static function init($className, $isActive = true, $isDefault = false) { //If the $isActive parameter is false, do not do anything. This will be useful if we store the plugins in the database //The $isDefault parameter will change the default shipping plugin if (!$isActive) { return; } if (!isset(ECommShipping::$activeShippingPlugin[$className])) { require "{$className}.php"; ECommShipping::$activeShippingPlugin[$className] = new $className(); } //If this is the first plugin, make it the default one if (count(ECommShipping::$activeShippingPlugin) == 1) { $isDefault = true; } if ($isDefault) { //If a plugin is the default shipping class: //First, set the defaultPlugin variable to the name of this class //Second, change the order of the array becuase the default plugin MUST always be the first shipping plugin ECommShipping::$defaultPlugin = $className; $tempArray = ECommShipping::$activeShippingPlugin; ECommShipping::$activeShippingPlugin = array(); ECommShipping::$activeShippingPlugin[$className] = $tempArray[$className]; //Put the default plugin at the beginning foreach ($tempArray as $key => $val) { ECommShipping::$activeShippingPlugin[$key] = $val; } } }
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; }