Skip to content
Snippets Groups Projects
Commit b7b6ac6f authored by Reinhold Kainhofer's avatar Reinhold Kainhofer
Browse files

Pass the rule to custom functions in plugins; Fix NoShipping; fix plugins with no matching rule

parent bab7e642
No related branches found
No related tags found
No related merge requests found
...@@ -22,7 +22,8 @@ if (!class_exists ('VmPlugin')) { ...@@ -22,7 +22,8 @@ if (!class_exists ('VmPlugin')) {
} }
// An example callback function to provide a custom function for shipping rules: // An example callback function to provide a custom function for shipping rules:
function custom_test_function($args) { function custom_test_function($args, $rule) {
JFactory::getApplication()->enqueueMessage(JText::sprintf("Evaluating function custom_test_function in rule '%s'", htmlentities($rule->rulestring)), 'warning');
return count($args); return count($args);
} }
...@@ -54,10 +55,12 @@ class plgVmShipmentRulesYOUR_PLUGIN_NAME extends VmPlugin { ...@@ -54,10 +55,12 @@ class plgVmShipmentRulesYOUR_PLUGIN_NAME extends VmPlugin {
* The onVmShippingRulesRegisterCustomFunctions() trigger is expected to return an array of the form: * The onVmShippingRulesRegisterCustomFunctions() trigger is expected to return an array of the form:
* array ('functionname1' => 'function_to_be_called', * array ('functionname1' => 'function_to_be_called',
* 'functionname2' => array($classobject, 'memberfunc')), * 'functionname2' => array($classobject, 'memberfunc')),
* 'functionname3' => array('classname', 'staticmemberfunc')),
* ...); * ...);
* The callback functions referenced here are called with exactly one array argument that holds * The callback functions referenced here are called with an array argument that holds
* all function arguments, i.e. the function signature should be * all function arguments and the rule itself, so that error messages can display the offending rule.
* function function_to_be_called($args) {....} * I.e. the function signature should be
* function function_to_be_called($args, $rule) {....}
* *
* All arguments passed to the function will already be properly evaluated before the function is called. * All arguments passed to the function will already be properly evaluated before the function is called.
*/ */
...@@ -70,7 +73,7 @@ class plgVmShipmentRulesYOUR_PLUGIN_NAME extends VmPlugin { ...@@ -70,7 +73,7 @@ class plgVmShipmentRulesYOUR_PLUGIN_NAME extends VmPlugin {
); );
} }
function custom_test_function_member($args) { function custom_test_function_member($args, $rule) {
return 'CustomTestFunction called with '.count($args).' arguments.'; return 'CustomTestFunction called with '.count($args).' arguments.';
} }
} }
......
<?xml version="1.0" encoding="UTF-8" ?> <?xml version="1.0" encoding="UTF-8" ?>
<extension version="1.5" type="plugin" group="vmshipmentrules" method="upgrade"> <extension version="2.5" type="plugin" group="vmshipmentrules" method="upgrade">
<name>VMSHIPMENTRULES_TEMPLATE</name> <name>VMSHIPMENTRULES_TEMPLATE</name>
<creationDate>2014-11-10</creationDate> <creationDate>2014-11-10</creationDate>
<author>Reinhold Kainhofer, Open Tools</author> <author>Reinhold Kainhofer, Open Tools</author>
......
...@@ -324,7 +324,7 @@ class plgVmShipmentRules_Shipping_Base extends vmPSPlugin { ...@@ -324,7 +324,7 @@ class plgVmShipmentRules_Shipping_Base extends vmPSPlugin {
if (!isset($this->rules[$method->virtuemart_shipmentmethod_id])) if (!isset($this->rules[$method->virtuemart_shipmentmethod_id]))
$this->parseMethodRules($method); $this->parseMethodRules($method);
$match = $this->evaluateMethodRules ($cart, $method, $cart_prices); $match = $this->evaluateMethodRules ($cart, $method, $cart_prices);
if ($match) { if ($match && !is_null ($match['rule'])) {
$method->rule_name = $match["rule_name"]; $method->rule_name = $match["rule_name"];
// If NoShipping is set, this method should NOT offer any shipping at all, so return FALSE, otherwise TRUE // If NoShipping is set, this method should NOT offer any shipping at all, so return FALSE, otherwise TRUE
// If the rule has a name, print it as warning (otherwise don't print anything) // If the rule has a name, print it as warning (otherwise don't print anything)
...@@ -405,7 +405,7 @@ class plgVmShipmentRules_Shipping_Base extends vmPSPlugin { ...@@ -405,7 +405,7 @@ class plgVmShipmentRules_Shipping_Base extends vmPSPlugin {
$cart_prices[$this->_psType . 'Value'] = $calculator->roundInternal ($this->getCosts ($cart, $method, $cart_prices), 'salesPrice'); $cart_prices[$this->_psType . 'Value'] = $calculator->roundInternal ($this->getCosts ($cart, $method, $cart_prices), 'salesPrice');
// BEGIN_RK_CHANGES // BEGIN_RK_CHANGES
$includes_tax = $method->includes_tax; $includes_tax = isset($method->includes_tax) && $method->includes_tax;
if ($includes_tax) { if ($includes_tax) {
$cart_prices['salesPrice' . $_psType] = $cart_prices[$this->_psType . 'Value']; $cart_prices['salesPrice' . $_psType] = $cart_prices[$this->_psType . 'Value'];
} }
...@@ -1222,7 +1222,7 @@ class ShippingRule { ...@@ -1222,7 +1222,7 @@ class ShippingRule {
// This is done first to allow plugins to override even built-in functions! // This is done first to allow plugins to override even built-in functions!
if (isset($this->plugin->custom_functions[$func])) { if (isset($this->plugin->custom_functions[$func])) {
vmDebug("Evaluating custom function $function, defined by a plugin"); vmDebug("Evaluating custom function $function, defined by a plugin");
return call_user_func($this->plugin->custom_functions[$func], $args); return call_user_func($this->plugin->custom_functions[$func], $args, $this);
} }
// Functions with no argument: // Functions with no argument:
...@@ -1300,6 +1300,8 @@ class ShippingRule { ...@@ -1300,6 +1300,8 @@ class ShippingRule {
$varname = strtolower($expr); $varname = strtolower($expr);
if (array_key_exists(strtolower($expr), $vals)) { if (array_key_exists(strtolower($expr), $vals)) {
return $vals[strtolower($expr)]; return $vals[strtolower($expr)];
} elseif ($varname=='noshipping') {
return $varname;
} elseif ($varname=='values') { } elseif ($varname=='values') {
return $vals; return $vals;
} elseif ($varname=='values_debug') { } elseif ($varname=='values_debug') {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment