Esempio n. 1
0
	/**
	 * Returns a list of available variants
	 * The structure is:
	 * array(
	 *     stdclass(
	 *         'name' => 'ups'
	 *         , 'display_name' => 'UPS'
	 *         , 'variant' => '01'
	 *         , 'variant_display_name' => 'Domestic'
	 *         ,  price => 12
	 * ))
	 *
	 * @param Cart $cart
	 * @internal param \Address $shipping_address
	 * @return array
	 */
	public function getAvailableVariants(Cart $cart)
	{
		$shipping_address = $cart->getShippingAddress();
		if(!$shipping_address)
		{
			$variant = new stdClass();
			$variant->name = $this->getName();
			$variant->display_name = $this->getDisplayName();
			$variant->variant = NULL;
			$variant->variant_display_name = 'Rates and available UPS services will show up after you enter your shipping address.';
			$variant->price = NULL;
			return array($variant);
		}

		$ups_api = new UpsAPI($this);
		try
		{
			$available_rates = $ups_api->getAvailableRates($cart);
		}
		catch(APIException $exception)
		{
			$available_rates = array();
		}


		$available_variants = array();
		foreach($available_rates as $rate)
		{
			$service_code = $rate['service'];
			$price = $rate['price'];
			// Even though UPS supports a certain service for the current Cart,
			// if the admin didn't enable it, we skip it
			if(!in_array($service_code, $this->enabled_services)) continue;
			$variant = new stdClass();
			$variant->name = $this->getName();
			$variant->display_name = $this->getDisplayName();
			$variant->variant = $service_code;
			$variant->variant_display_name = UPS::$service_names[$service_code];
			$variant->price = $price;
			$available_variants[] = $variant;
		}
		return $available_variants;
	}