Example #1
0
	/**
	 * Calculates aggregated total amounts
	 *
	 * Iterates over the cart items in the contents of the cart
	 * to calculate aggregated total amounts including the
	 * subtotal, shipping, tax, discounts and grand total
	 *
	 * @since 1.0
	 *
	 * @return void
	 **/
	function totals () {
		if (!($this->retotal || $this->changed())) return true;

		$Totals = new CartTotals();
		$this->Totals = &$Totals;

		// Setup discount calculator
		$Discounts = new CartDiscounts();

		// Free shipping until costs are assessed
		$this->freeshipping = true;

		// Identify downloadable products
		$this->downloads();

		// If no items are shipped, free shipping is disabled
		if (!$this->shipped()) $this->freeshipping = false;

		foreach ($this->contents as $key => $Item) {
			$Item->retotal();

			$Totals->quantity += $Item->quantity;
			$Totals->subtotal +=  $Item->total;

			// Reinitialize item discount amounts
			$Item->discount = 0;

			// Item does not have free shipping,
			// so the cart shouldn't have free shipping
			if (!$Item->freeshipping) $this->freeshipping = false;

		}

		// Calculate Shipping
		$Shipping = new CartShipping();
		if ($this->changed()) {
			// Only fully recalculate shipping costs
			// if the cart contents have changed
			$Totals->shipping = $Shipping->calculate();

			// Save the generated shipping options
			$this->shipping = $Shipping->options();

		} else $Totals->shipping = $Shipping->selected();

		// Calculate discounts
		$Totals->discount = $Discounts->calculate();

		//$this->promotions();
		$Totals->discount = ($Totals->discount > $Totals->subtotal)?$Totals->subtotal:$Totals->discount;

		// Calculate taxes
		$Tax = new CartTax();
		$Totals->taxrate = $Tax->rate();
		$Totals->tax = $Tax->calculate();

		// Calculate final totals
		$Totals->total = roundprice($Totals->subtotal - roundprice($Totals->discount) +
			$Totals->shipping + $Totals->tax);

		do_action_ref_array('ecart_cart_retotal',array(&$this->Totals));
		$this->changed = false;
		$this->retotal = false;


	}