From 2f360567880bf3e8854217b1c0cde0b3b4d17aff Mon Sep 17 00:00:00 2001
From: Reinhold Kainhofer <reinhold@kainhofer.com>
Date: Tue, 18 Jun 2013 23:06:00 +0200
Subject: [PATCH] Allow variables of the form {varname} in the rule names; They
 are replaced by the values of the cart variables

---
 rules_shipping_base.php | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)

diff --git a/rules_shipping_base.php b/rules_shipping_base.php
index 7c4b97f..5014e82 100644
--- a/rules_shipping_base.php
+++ b/rules_shipping_base.php
@@ -223,10 +223,11 @@ class plgVmShipmentRules_Shipping_Base extends vmPSPlugin {
 		
 		foreach ($method->rules as $r) {
 			if ($r->matches($cartvals)) {
-				vmdebug('Rule '.$r->name.' ('.$r->rulestring.') matched.');
+				$rulename=$r->getRuleName($cartvals);
+				vmdebug('Rule '.$rulename.' ('.$r->rulestring.') matched.');
 				$method->tax_id = $r->tax_id;
 				$method->matched_rule = $r;
-				$method->rule_name = $r->name;
+				$method->rule_name = $r->getRuleName($cartvals);
 				$method->cost = $r->getShippingCosts($cartvals);
 				$method->includes_tax = $r->includes_tax;
 				return $method->cost;
@@ -469,10 +470,10 @@ class plgVmShipmentRules_Shipping_Base extends vmPSPlugin {
 		foreach ($method->rules as $r) {
 			if ($r->matches($cartvals)) {
 				$method->matched_rule = $r;
-				$method->rule_name = $r->name;
+				$method->rule_name = $r->getRuleName($cartvals);
 				// If NoShipping is set, this method should NOT offer any shipping at all, so return FALSE, otherwise TRUE
 				if ($r->isNoShipping()) {
-					vmdebug('checkConditions '.$method->shipment_name.' indicates NoShipping for rule "'.$r->name.'" ('.$r->rulestring.').');
+					vmdebug('checkConditions '.$method->shipment_name.' indicates NoShipping for rule "'.$method->rule_name.'" ('.$r->rulestring.').');
 					return FALSE;
 				} else {
 					return TRUE;
@@ -845,6 +846,20 @@ class ShippingRule {
 		return true;
 	}
 
+	function getRuleName($vals) {
+		// Replace all {variable} tags in the name by the variables from $vals
+		$matches=array();
+		$name=$this->name;
+		preg_match_all('/{([A-Za-z0-9_]+)}/', $name, $matches);
+		
+		foreach ($matches[1] as $m) {
+			if (isset($vals[$m])) {
+				$name = str_replace("{".$m."}", strval($vals[$m]), $name);
+			}
+		}
+		return $name;
+	}
+	
 	function getShippingCosts($vals) {
 		return $this->calculateShipping($vals);
 	}
-- 
GitLab