/** * Allows access to the cart from anywhere in code. * @return ShoppingCart Object */ public static function singleton() { if (!self::$singletoncart) { self::$singletoncart = ShoppingCart::create(); } return self::$singletoncart; }
public function doAddItemToCart($data) { $product = Product::get()->byID($data['ProductID']); $customisations = array(); foreach ($data as $key => $value) { if (!(strpos($key, 'customise') === false) && $value) { $custom_data = explode("_", $key); if ($custom_item = ProductCustomisation::get()->byID($custom_data[1])) { $modify_price = 0; // Check if the current selected option has a price modification if ($custom_item->Options()->exists()) { $option = $custom_item->Options()->filter("Title", $value)->first(); $modify_price = $option ? $option->ModifyPrice : 0; } $customisations[] = array("Title" => $custom_item->Title, "Value" => $value, "ModifyPrice" => $modify_price); } } } if ($product) { $cart = ShoppingCart::create(); $cart->add($product, $data['Quantity'], $customisations); $cart->save(); // Clear any postage data that has been set Session::clear("Commerce.PostageID"); $message = _t('Commerce.AddedItemToCart', 'Added item to your shopping cart'); $message .= ' <a href="' . $cart->Link() . '">'; $message .= _t('Commerce.ViewCart', 'View cart'); $message .= '</a>'; $this->controller->setSessionMessage("success", $message); } return $this->controller->redirectBack(); }
public function init() { parent::init(); // If no shopping cart doesn't exist, redirect to base if (!ShoppingCart::create()->getItems()->exists()) { return $this->redirect(ShoppingCart::config()->url_segment); } }
/** * Shortcut for ShoppingCart::create, exists because create() * doesn't seem quite right. * * @return ShoppingCart */ public static function get() { return ShoppingCart::create(); }
/** * Return a the current shopping cart * * @return ShoppingCart */ public function getCommerceCart() { return ShoppingCart::create(); }
/** * Function to find relevent postage rates, based on supplied country and * zip/postal code data. * * We make this a function as part of Commerce Controller, as there are * several points in the payment/order process that will make use of this * functionality * * @param $country String listing the country to search, this has to be an ISO 3166 code * @param $zipcode String listing the zip/postage code to filter by */ public function getPostageAreas($country, $zipcode) { $return = new ArrayList(); $countries = new ArrayList(); $cart = ShoppingCart::create(); $all_rates = $this->SiteConfig()->PostageAreas(); // First find all area's for this country directly (no wildcards) foreach ($all_rates as $rate) { if (!(strpos(strtolower($rate->Country), strtolower($country)) === false)) { $countries->add($rate); } } // If we have no countries in the list specificly, then check for wildcards if (!$countries->exists()) { foreach ($all_rates as $rate) { if ($rate->Country == "*") { $countries->add($rate); } } } // If we have a list of countries check them for post codes foreach ($countries as $rate) { $rate_codes = explode(",", $rate->ZipCode); foreach ($rate_codes as $rate_to_check) { $curr_length = strlen($rate_to_check); if (strtolower(substr($zipcode, 0, $curr_length)) == strtolower($rate_to_check)) { $return->add($rate); } } } // If we still don't have anything to return, check or list of countries // for a wildcard if (!$return->exists()) { foreach ($countries as $rate) { if ($rate->ZipCode == "*") { $return->add($rate); } } } // Now we have a list of locations, start checking for additional // rules an remove if not applicable. $total_cost = str_replace(",", "", $cart->SubTotalCost()); $total_weight = str_replace(",", "", $cart->TotalWeight()); $total_items = str_replace(",", "", $cart->TotalItems()); $max_cost = 0; $max_weight = 0; $max_items = 0; // First loop through and find items that are invalid foreach ($return as $location) { if ($location->Calculation == "Price" && (double) $total_cost < $location->Unit) { $return->remove($location); } if ($location->Calculation == "Weight" && (double) $total_weight < $location->Unit) { $return->remove($location); } if ($location->Calculation == "Items" && (double) $total_items < $location->Unit) { $return->remove($location); } } // Now find max values based on units foreach ($return as $location) { if ($location->Calculation == "Price" && $location->Unit > $max_cost) { $max_cost = $location->Unit; } if ($location->Calculation == "Weight" && $location->Unit > $max_weight) { $max_weight = $location->Unit; } if ($location->Calculation == "Items" && $location->Unit > $max_items) { $max_items = $location->Unit; } } // Now loop through again and calculate which brackets each // Location fits in foreach ($return as $location) { if ($location->Calculation == "Price" && $location->Unit < $max_cost) { $return->remove($location); } if ($location->Calculation == "Weight" && $location->Unit < $max_weight) { $return->remove($location); } if ($location->Calculation == "Items" && $location->Unit < $max_items) { $return->remove($location); } } return $return; }