From 9eaf626106046e1acc7daa4f5c8e6361d534a453 Mon Sep 17 00:00:00 2001
From: Reinhold Kainhofer <reinhold@kainhofer.com>
Date: Thu, 25 Jul 2013 20:52:02 +0200
Subject: [PATCH] Implement country/state variables

---
 Makefile                |  2 +-
 rules_shipping.xml      |  2 +-
 rules_shipping_base.php | 33 ++++++++++++++++++++++++++++++---
 3 files changed, 32 insertions(+), 5 deletions(-)

diff --git a/Makefile b/Makefile
index 1797476..ca3d149 100644
--- a/Makefile
+++ b/Makefile
@@ -2,7 +2,7 @@ BASE=rules_shipping
 BASE_ADV=rules_shipping_advanced
 PLUGINTYPE=vmshipment
 ZIPBASE=opentools_vm2
-VERSION=3.0.2
+VERSION=3.1
 
 PLUGINFILES=$(BASE).php $(BASE)_base.php $(BASE).script.php $(BASE).xml index.html
 PLUGINFILES_ADV=$(BASE_ADV).php $(BASE)_base.php $(BASE_ADV).script.php $(BASE_ADV).xml index.html
diff --git a/rules_shipping.xml b/rules_shipping.xml
index 0ffe607..ef943d5 100644
--- a/rules_shipping.xml
+++ b/rules_shipping.xml
@@ -6,7 +6,7 @@
     <authorUrl>http://www.open-tools.net</authorUrl>
     <copyright>Copyright (C) 2013, Reinhold Kainhofer</copyright>
     <license>GPL v3+</license>
-    <version>3.0.2</version>
+    <version>3.1</version>
     <description>VMSHIPMENT_RULES_DESC</description>
     <files>
 	<filename plugin="rules_shipping">rules_shipping.php</filename>
diff --git a/rules_shipping_base.php b/rules_shipping_base.php
index 73d4c5c..e84e2f1 100644
--- a/rules_shipping_base.php
+++ b/rules_shipping_base.php
@@ -415,6 +415,33 @@ class plgVmShipmentRules_Shipping_Base extends vmPSPlugin {
 		return $dimensions;
 	}
 	
+	function getOrderCountryState (VirtueMartCart $cart, $address) {
+		$data = array ();
+		
+		$countriesModel = VmModel::getModel('country');
+		if (isset($address['virtuemart_country_id'])) {
+			$countriesModel->setId($address['virtuemart_country_id']);
+			$country = $countriesModel->getData();
+			$data['countryid'] = $country->virtuemart_country_id;
+			$data['country'] = $country->country_name;
+			$data['country2'] = $country->country_2_code;
+			$data['country3'] = $country->country_3_code;
+		}
+		
+		$statesModel = VmModel::getModel('state');
+		if (isset($address['virtuemart_country_id'])) {
+			$statesModel->setId($address['virtuemart_state_id']);
+			$state = $statesModel->getData();
+			$data['stateid'] = $state->virtuemart_state_id;
+			$data['state'] = $state->state_name;
+			$data['state2'] = $state->state_2_code;
+			$data['state3'] = $state->state_3_code;
+		}
+		
+		return $data;
+
+	}
+	
 	/** Allow child classes to add additional variables for the rules
 	 */
 	protected function addCustomCartValues (VirtueMartCart $cart, $cart_prices, &$values) {
@@ -444,15 +471,15 @@ class plgVmShipmentRules_Shipping_Base extends vmPSPlugin {
 				  'discountamount'=>$cart_prices['discountAmount'],
 				  'pricewithouttax'=>$cart_prices['priceWithoutTax'],
 // 				  'discountbeforetaxbill'=>$cart_prices['discountBeforeTaxBill'],
-
-				  'country'=>$address['virtuemart_country_id'],
 			);
-			
+		
+		$cartvals = array_merge ($cartvals, $this->getOrderCountryState ($cart, $address));
 		$cartvals = array_merge ($cartvals, $this->getOrderWeights ($cart, $method->weight_unit));
 		$cartvals = array_merge ($cartvals, $this->getOrderDimensions ($cart, $method->length_unit));
 		// Let child classes update the $cartvals array, or add new variables
 		$this->addCustomCartValues($cart, $cart_prices, $cartvals);
 // JFactory::getApplication()->enqueueMessage("<pre>cart prices: ".print_r($cart_prices,1)."</pre>", 'error');
+// JFactory::getApplication()->enqueueMessage("<pre>cart: ".print_r($cart,1)."</pre>", 'error');
 		return $cartvals;
 	}
 
-- 
GitLab