/**
  * Add the shipping information to the google object representation of the customers cart.
  *
  * @return void
  **/
 private function AddShippingInformationToCart()
 {
     $shippingZones = GetShippingZoneInfo();
     $defaultZone = null;
     $coveredLocations = array();
     $defaultShippingMethods = array();
     // Do all the normal zones first (skip the default one)
     // this is so that we can work out where "everywhere else" equates to
     foreach ($shippingZones as $shippingZone) {
         // If the zone has no enabled methods
         if (!isset($shippingZone['methods'])) {
             continue;
         }
         // Skip the default zone for now
         if (!isset($shippingZone['locationtype'])) {
             continue;
         }
         foreach ($shippingZone['methods'] as $method) {
             $module = null;
             if (!GetModuleById('shipping', $module, $method['methodmodule'])) {
                 continue;
             }
             if ($module === null) {
                 continue;
             }
             $module->SetMethodId($method['methodid']);
             $deliveryMethods = $module->GetAvailableDeliveryMethods();
             foreach ($deliveryMethods as $deliveryMethod) {
                 $shipping_name = $deliveryMethod . ' (' . $shippingZone['zonename'] . ')';
                 // If it's a flat rate
                 if ($module->_flatrate) {
                     $defaultCost = $module->GetValue('defaultcost');
                     if ($defaultCost === null) {
                         $defaultCost = $this->GetValue('fallbackshippingcost');
                     }
                     $ship = new GoogleMerchantCalculatedShipping($shipping_name, $defaultCost);
                 } else {
                     $ship = new GoogleMerchantCalculatedShipping($shipping_name, $this->GetValue('fallbackshippingcost'));
                 }
                 $ShippingRestrictions = $this->GetShippingRestrictions($shippingZone);
                 $this->AddDefaultShippingRestrictions($shippingZone);
                 if ($ShippingRestrictions !== false) {
                     // Address filters are used when a customer goes to the google checkout page
                     $ship->AddAddressFilters($ShippingRestrictions);
                     // Shipping restrictions are used if the merchant callback calculation fails
                     $ship->AddShippingRestrictions($ShippingRestrictions);
                 }
                 $this->cart->AddShipping($ship);
             }
         }
     }
     // Now add the methods for the default zone
     foreach ($shippingZones as $shippingZone) {
         // If the zone has no enabled methods
         if (!isset($shippingZone['methods'])) {
             continue;
         }
         // Skip any non-default zones now
         if (isset($shippingZone['locationtype'])) {
             continue;
         }
         foreach ($shippingZone['methods'] as $method) {
             $module = null;
             if (!GetModuleById('shipping', $module, $method['methodmodule'])) {
                 continue;
             }
             if ($module === null) {
                 continue;
             }
             $module->SetMethodId($method['methodid']);
             $deliveryMethods = $module->GetAvailableDeliveryMethods();
             foreach ($deliveryMethods as $deliveryMethod) {
                 $shipping_name = $deliveryMethod;
                 // If it's a flat rate
                 if ($module->_flatrate) {
                     $defaultCost = $module->GetValue('defaultcost');
                     if ($defaultCost === null) {
                         $defaultCost = $this->GetValue('fallbackshippingcost');
                     }
                     $ship = new GoogleMerchantCalculatedShipping($shipping_name, $defaultCost);
                 } else {
                     $ship = new GoogleMerchantCalculatedShipping($shipping_name, $this->GetValue('fallbackshippingcost'));
                 }
                 if ($this->defaultZoneGFilter) {
                     // Address filters are used when a customer goes to the google checkout page
                     $ship->AddAddressFilters($this->defaultZoneGFilter);
                     // Shipping restrictions are used if the merchant callback calculation fails
                     $ship->AddShippingRestrictions($this->defaultZoneGFilter);
                 }
                 $this->cart->AddShipping($ship);
             }
         }
     }
 }
Example #2
0
                             $Gfilter->SetAllowedWorldArea(true);
                             if ('US' == SHIPPING_ORIGIN_COUNTRY) {
                                 $Gfilter->SetExcludedCountryArea('ALL');
                             } else {
                                 $Gfilter->AddExcludedPostalArea($shipping_origin_iso_code_2);
                             }
                             break;
                         default:
                             // should never reach here!
                             $Gfilter->SetAllowedWorldArea(true);
                             break;
                     }
                 }
                 if ($ship_calculation_mode == 'True') {
                     if ($default_value != 0) {
                         $Gshipping = new GoogleMerchantCalculatedShipping($shipping_name, $shipping_price);
                         $Gshipping->AddShippingRestrictions($Gfilter);
                         $Gshipping->AddAddressFilters($Gfilter);
                         $Gcart->AddShipping($Gshipping);
                     }
                 } else {
                     $Gshipping = new GoogleFlatRateShipping($shipping_name, $shipping_price);
                     $Gshipping->AddShippingRestrictions($Gfilter);
                     $Gcart->AddShipping($Gshipping);
                 }
             }
         }
     } else {
         $shipping_config_errors .= $key . " (ignored)<br />";
     }
 }
function UseCase3()
{
    //Create a new shopping cart object
    $merchant_id = "";
    // Your Merchant ID
    $merchant_key = "";
    // Your Merchant Key
    $server_type = "sandbox";
    $currency = "USD";
    $cart = new GoogleCart($merchant_id, $merchant_key, $server_type, $currency);
    // Add items to the cart
    $item = new GoogleItem("MegaSound 2GB MP3 Player", "Portable MP3 player - stores 500 songs", 1, 175.49);
    $item->SetMerchantPrivateItemData("<color>blue</color><weight>3.2</weight>");
    $cart->AddItem($item);
    // Add merchant calculations options
    $cart->SetMerchantCalculations("http://200.69.205.154/~brovagnati/tools/unitTest/demo/responsehandlerdemo.php", "false", "true", "true");
    // accept-merchant-gift-certificates
    // Add merchant-calculated-shipping option
    $ship = new GoogleMerchantCalculatedShipping("2nd Day Air", 10.0);
    // Default, fallback price
    $restriction = new GoogleShippingFilters();
    $restriction->AddAllowedPostalArea("GB");
    $restriction->AddAllowedPostalArea("US");
    $restriction->SetAllowUsPoBox(false);
    $ship->AddShippingRestrictions($restriction);
    $address_filter = new GoogleShippingFilters();
    $address_filter->AddAllowedPostalArea("GB");
    $address_filter->AddAllowedPostalArea("US");
    $address_filter->SetAllowUsPoBox(false);
    $ship->AddAddressFilters($address_filter);
    $cart->AddShipping($ship);
    // Set default tax options
    $tax_rule = new GoogleDefaultTaxRule(0.15);
    $tax_rule->SetWorldArea(true);
    $cart->AddDefaultTaxRules($tax_rule);
    $cart->AddRoundingPolicy("UP", "TOTAL");
    // Display XML data
    // echo "<pre>";
    // echo htmlentities($cart->GetXML());
    // echo "</pre>";
    // Display a disabled, small button
    echo $cart->CheckoutButtonCode("SMALL", false);
}
		/**
		 * Add the shipping information to the google object representation of the customers cart.
		 *
		 * @return void
		 **/
		private function AddShippingInformationToCart()
		{
			$quote = getCustomerQuote();
			$noShippingCost = 0;
			$fixedShippingCost = 0;
			$fixedShippingProducts = 0;

			$items = $quote->getItems();
			foreach($items as $item) {
				if($item->getType() != PT_PHYSICAL) {
					continue;
				}

				if($item->hasFreeShipping()) {
					++$noShippingCost;
				}
				else if($item->getFixedShippingCost() > 0) {
					++$fixedShippingProducts;
					$fixedShippingCost += $item->getFixedShippingCost() * $item->getQuantity();
				}
			}

			// Global free shipping options
			$addFreeShipping = false;
			if ($quote->getHasFreeShipping() || $noShippingCost)
			{
				$freeShippingName = GetLang('FreeShipping');
				$addFreeShipping = true;
			}

			// Global fixed shipping on items option
			$addFixedShipping = false;
			if (count($items) == $fixedShippingProducts) {
				$fixedShippingName = GetConfig('StoreName');
				$addFixedShipping = true;
			}

			// Not all the products have a fixed shipping so keep on chugging away
			$shippingZones = GetShippingZoneInfo();

			// Do all the normal zones first (skip the default one)
			// this is so that we can work out where "everywhere else" equates to
			foreach ($shippingZones as $shippingZone) {
				// Skip the default zone for now
				if (!isset($shippingZone['locationtype'])) {
					continue;
				}

				$shippingRestrictions = $this->GetShippingRestrictions($shippingZone);
				$this->AddDefaultShippingRestrictions($shippingZone);

				// Add in the free shipping option if we have it
				if($addFreeShipping || !empty($shippingZone['zonefreeshipping'])) {
					$freeShippingName = GetLang('FreeShipping');
					$ship = new GoogleMerchantCalculatedShipping($freeShippingName . ' ('.$shippingZone['zonename'].')', 0);
					if ($shippingRestrictions !== false) {
						// Address filters are used when a customer goes to the google checkout page
						$ship->AddAddressFilters($shippingRestrictions);

						// Shipping restrictions are used if the merchant callback calculation fails
						$ship->AddShippingRestrictions($shippingRestrictions);
					}

					$ship->AddAddressFilters($shippingRestrictions);
					$this->cart->AddShipping($ship);
				}

				// Add in the fixed shipping option if we have it
				if($addFixedShipping) {
					$ship = new GoogleMerchantCalculatedShipping($fixedShippingName . ' ('.$shippingZone['zonename'].')', $fixedShippingCost);
					if ($shippingRestrictions !== false) {
						// Address filters are used when a customer goes to the google checkout page
						$ship->AddAddressFilters($shippingRestrictions);

						// Shipping restrictions are used if the merchant callback calculation fails
						$ship->AddShippingRestrictions($shippingRestrictions);
					}

					$ship->AddAddressFilters($shippingRestrictions);
					$this->cart->AddShipping($ship);
				}

				$this->AddShippingZoneMethodsToCart($shippingZone, $shippingRestrictions, $fixedShippingCost, true);
			}

			// Now add the methods for the default zone
			foreach ($shippingZones as $shippingZone) {
				// Skip any non-default zones now
				if (isset($shippingZone['locationtype'])) {
					continue;
				}

				// Add free shipping options
				if ($addFreeShipping || !empty($shippingZone['zonefreeshipping']))
				{
					$freeShippingName = GetLang('FreeShipping');
					$ship = new GoogleMerchantCalculatedShipping($freeShippingName, 0);
					$Gfilter = new GoogleShippingFilters();
					$Gfilter->SetAllowedWorldArea(true);
					$ship->AddAddressFilters($Gfilter);

					if ($this->defaultZoneGFilter) {
						// Address filters are used when a customer goes to the google checkout page
						$ship->AddAddressFilters($this->defaultZoneGFilter);

						// Shipping restrictions are used if the merchant callback calculation fails
						$ship->AddShippingRestrictions($this->defaultZoneGFilter);
					}

					$this->cart->AddShipping($ship);
				}

				// Add fixed shipping on items option
				if ($addFixedShipping) {
					$Gfilter = new GoogleShippingFilters();
					$ship = new GoogleMerchantCalculatedShipping($fixedShippingName, $fixedShippingCost);
					$Gfilter->SetAllowedWorldArea(true);
					$ship->AddAddressFilters($Gfilter);

					if ($this->defaultZoneGFilter) {
						// Address filters are used when a customer goes to the google checkout page
						$ship->AddAddressFilters($this->defaultZoneGFilter);

						// Shipping restrictions are used if the merchant callback calculation fails
						$ship->AddShippingRestrictions($this->defaultZoneGFilter);
					}

					$this->cart->AddShipping($ship);
				}

				$this->AddShippingZoneMethodsToCart($shippingZone, $this->defaultZoneGFilter, $fixedShippingCost, false);
			}
		}