Login
Register

VirtueMart

WooCommerce

Others

Docs

Support

Blog

About

Shipping by Rules for VirtueMart

IMPORTANT ANNOUNCEMENT: Plugin development ceased, all plugins made available freely (GPL)

With great sadness we have to announce that we are ceasing development of all our VirtueMart, WooCommerce and Joomla plugins. Effective immediately, all our plugins -- even those that were paid downloads -- are made available for free from our homepage (GPL license still applies), but we cannot and will not provide any support anymore.

It has been a great pleasure to be part of the thriving development communities of VirtueMart as well as WooCommerce. However, during the last year it became painstakingly clear that in addition to a full-time job, a young family and several other time-consuming hobbies at professional level (like being a professional singer) the plugin development and the support that it requires is not sustainable and is taking its toll. It has been an honor, but it is now time to say good bye!

×

Notice

The forum is in read only mode.
Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC:

what is my mistake? customfields 27 Feb 2016 05:33 #1

  • masay
  • masay's Avatar Topic Author
i get following error:
Evaluation yields unknown value while evaluating rule part 'PesoCarrello'.
what is my mistake?
this is the code.
<?php

defined ('_JEXEC') or die('Restricted access');

/**
 * Plugin providing Custom variables for VM Shipping by Rules
 *
 * @subpackage Plugins - VmShipmentRules
 * @copyright Copyright (C) 2014 Reinhold Kainhofer, office@open-tools.net
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
 * VirtueMart is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 * See administrator/components/com_virtuemart/COPYRIGHT.php for copyright notices and details.
 *
 * http://open-tools.net/
 *
 */
if (!class_exists ('VmPlugin')) {
	require(JPATH_VM_PLUGINS . DS . 'vmplugin.php');
}

// An example callback function to provide a custom function for shipping rules:
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);
}
/** Extension plugin for the "Shipping by Rules" shipping plugin for VirtueMart
 */
class plgVmShipmentRulesshipping_custom extends VmPlugin {
	/**  Trigger to add variables to the cart values
	  *  You can add new variables to the $cartvals array or modify existing ones. They will be directly 
	  *  available in all rules.
	  *  This trigger will be first called right before any rule is evaluated. In that case, $products 
	  *  will contain all products in the cart and $cart_prices will be an arrow containing the calculated
	  *  prices of the order.
	  *  Please notice that this function might also be called for only a subset of products of the cart
	  *  when the plugin evaluates a scoping function like evaluate_for_categories(...).
	  *  In that case, $cart_prices will be NULL and the $products array will hold only those products that 
	  *  actually match the filter, and only those should be used to calculate your custom variables.
	  *  So you can not in general rely on the cart_prices argument to hold the properly summed prices.
	  
	function onVmShippingRulesGetCartValues(&$cartvals, $cart, $products, $method, $cart_prices) {
		if ($cart_prices) {
			// Called for the whole cart...
		} else {
			// Called when any of the scoping operators need the cart values for only a subset of products
		}
		$cartvals['template_example'] = 123456789;
		
	}*/
		function onVmShippingRulesGetCartValues(&$cartvals, $cart, $products, $method, $cart_prices) {
  $cartvals['PesoCarrello'] = 0;
  foreach ($products as $p) {
    foreach ($p->customProductData as $cf) {
      if ($cf->customfield_name=="Scegli la confezione:") {
        $matches=$cf->customfield_title;
	$numeri= preg_match_all('/\d+/', $str, $matches);
        $peso=0;
	if (intval($numeri[1]) >=100) {
	$peso=intval($numeri[1])/1000;}
	else {
	$peso=$peso + $numeri[1];
	}
	
      }
    }
  }
	$cartvals['PesoCarrello'] = $peso;
}
	/** Trigger to register custom functions for the Shipping by Rules plugin 
	 *  The onVmShippingRulesRegisterCustomFunctions() trigger is expected to return an array of the form:
	 *   array ('functionname1' => 'function_to_be_called',
	 *          'functionname2' => array($classobject, 'memberfunc')),
	 *          'functionname3' => array('classname', 'staticmemberfunc')),
	 *          ...);
	 *  The callback functions referenced here are called with an array argument that holds 
	 *  all function arguments and the rule itself, so that error messages can display the offending rule.
	 *  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.
	 */
	function onVmShippingRulesRegisterCustomFunctions() {
		return array(
			// An example of a custom function that calls a member of this plugin class:
			'customTestFunctionMember' => array($this, 'custom_test_function_member'),
			// An example of a custom function that calls an ordinary top-level php function:
			'customTestFunction' => 'custom_test_function',
		);
	}
	
	function custom_test_function_member($args, $rule) {
		return 'CustomTestFunction called with '.count($args).' arguments.';
	}
}

// No closing tag

what is my mistake? customfields 29 Feb 2016 11:25 #2

Dear Masay,
Which version of the plugin do you have installed?
To debug issues with the shipping plugins, you can do multiple things:

1) To check which values are set by the plugin, you can look at the list of all variables as described in the debugging section of the documentation: open-tools.net/documentation/shipping-by...emart.html#debugging

2) If the variable is not set at all, you can check whether the plugin is loaded at all and whether the onVmShippingRulesGetCartValues function is properly executed by inserting a debug call like
    JFactory::getApplication()->enqueueMessage("Executing plugin function onVmShippingRulesGetCartValues", "warning");
(a) at the very beginning of the file (before the class definition), (b) in the class constructor (which you need to add yourself) and (c) inside the onVmShippingRulesGetCartValues function.

The first debug message should always be printed when anything related to shipping happens (e.g. in the backend when you go to the shipping methods section). The second one should always be printed when you have a method from the shipping by rules plugin. And the third should always be printed when a shipping method of the shipping by rules plugin calculates the shipping cost. Unless the third one is printed properly, the plugin won't work.

I would use that first to make sure that the function you implement is really executed. If it is, then we'll have to debug further, but my expectation is that it is never properly executed...

Best regards,
Reinhold
  • Page:
  • 1