From 6b752e9c576fd456f0ce2e352a74c19267a7a227 Mon Sep 17 00:00:00 2001 From: Reinhold Kainhofer <reinhold@kainhofer.com> Date: Fri, 3 May 2013 22:47:29 +0200 Subject: [PATCH] Implement MinWeight/MaxWeight --- rules_shipping.script.php | 6 +++ rules_shipping_advanced.php | 2 +- rules_shipping_advanced.script.php | 6 +++ rules_shipping_base.php | 62 +++++++++++++++++------------- 4 files changed, 49 insertions(+), 27 deletions(-) diff --git a/rules_shipping.script.php b/rules_shipping.script.php index 7d92ad4..d6fc0b8 100644 --- a/rules_shipping.script.php +++ b/rules_shipping.script.php @@ -1,5 +1,11 @@ <?php defined('_JEXEC') or die('Restricted access'); +/** + * Installation script for the plugin + * + * @copyright Copyright (C) 2013 Reinhold Kainhofer, office@open-tools.net + * @license GPL v3+, http://www.gnu.org/copyleft/gpl.html + */ class plgVmShipmentRules_ShippingInstallerScript { diff --git a/rules_shipping_advanced.php b/rules_shipping_advanced.php index c2c0990..d8a5eb8 100644 --- a/rules_shipping_advanced.php +++ b/rules_shipping_advanced.php @@ -8,7 +8,7 @@ defined ('_JEXEC') or die('Restricted access'); * * @package VirtueMart * @subpackage Plugins - shipment. - * @copyright Copyright (C) 2013 Reinhold Kainhofer, office@open-tools.n.et + * @copyright Copyright (C) 2013 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 diff --git a/rules_shipping_advanced.script.php b/rules_shipping_advanced.script.php index 40021b2..bc02d80 100644 --- a/rules_shipping_advanced.script.php +++ b/rules_shipping_advanced.script.php @@ -1,5 +1,11 @@ <?php defined('_JEXEC') or die('Restricted access'); +/** + * Installation script for the plugin + * + * @copyright Copyright (C) 2013 Reinhold Kainhofer, office@open-tools.net + * @license GPL v3+, http://www.gnu.org/copyleft/gpl.html + */ class plgVmShipmentRules_Shipping_AdvancedInstallerScript { diff --git a/rules_shipping_base.php b/rules_shipping_base.php index afbc40c..1c4bb8c 100644 --- a/rules_shipping_base.php +++ b/rules_shipping_base.php @@ -24,8 +24,10 @@ defined ('_JEXEC') or die('Restricted access'); if (!class_exists ('vmPSPlugin')) { require(JPATH_VM_PLUGINS . DS . 'vmpsplugin.php'); } -if (!class_exists ('plgVmShipmentRules_Shipping_Base')) { // Only declare the class once... +if (class_exists ('plgVmShipmentRules_Shipping_Base')) { + return; +} /** Shipping costs according to general rules. * Supported Variables: Weight, ZIP, Amount, Products (1 for each product, even if multiple ordered), Articles @@ -342,6 +344,15 @@ class plgVmShipmentRules_Shipping_Base extends vmPSPlugin { return $articles; } + protected function getOrderProducts (VirtueMartCart $cart) { + /* Cache the value in a static variable and calculate it only once! */ + static $products = 0; + if(empty($products) and count($cart->products)>0){ + $products = count($cart->products); + } + return $products; + } + protected function getOrderDimensions (VirtueMartCart $cart, $length_dimension) { /* Cache the value in a static variable and calculate it only once! */ static $calculated = 0; @@ -359,7 +370,6 @@ class plgVmShipmentRules_Shipping_Base extends vmPSPlugin { $l = ShopFunctions::convertDimensionUnit ($product->product_length, $product->product_lwh_uom, $length_dimension); $w = ShopFunctions::convertDimensionUnit ($product->product_width, $product->product_lwh_uom, $length_dimension); $h = ShopFunctions::convertDimensionUnit ($product->product_height, $product->product_lwh_uom, $length_dimension); -// JFactory::getApplication()->enqueueMessage("<pre>Product, returned dimensions: $l x $w x $h, from $product->product_lwh_uom to $length_dimension, ".print_r($product,1)."</pre>", 'error'); $volume = $l * $w * $h; $dimensions['volume'] += $volume * $product->quantity; @@ -376,18 +386,26 @@ class plgVmShipmentRules_Shipping_Base extends vmPSPlugin { $dimensions['totalheight'] += $h * $product->quantity; } } -// JFactory::getApplication()->enqueueMessage("<pre>Dimensions: ".print_r($dimensions,1)."</pre>", 'error'); return $dimensions; } - - protected function getOrderProducts (VirtueMartCart $cart) { - /* Cache the value in a static variable and calculate it only once! */ - static $products = 0; - if(empty($products) and count($cart->products)>0){ - $products = count($cart->products); + + function getOrderWeights (VirtueMartCart $cart, $weight_units) { + static $calculated = 0; + static $dimensions=array( + 'weight' => 0, + 'maxweight' => 0, 'minweight' => 9999999999, + ); + if ($calculated==0 && count($cart->products)>0) { + $calculated = 1; + foreach ($cart->products as $product) { + $w = ShopFunctions::convertWeigthUnit ($product->product_weight, $product->product_weight_uom, $to_weight_unit); + $dimensions['maxweight'] = max ($dimensions['maxweight'], $w); + $dimensions['minweight'] = min ($dimensions['minweight'], $w); + $dimensions['weight'] += $w * $product->quantity; + } } - return $products; + return $dimensions; } /** Allow child classes to add additional variables for the rules @@ -395,26 +413,17 @@ class plgVmShipmentRules_Shipping_Base extends vmPSPlugin { protected function addCustomCartValues (VirtueMartCart $cart, $cart_prices, &$values) { } protected function getCartValues (VirtueMartCart $cart, $method, $cart_prices) { - $orderWeight = $this->getOrderWeight ($cart, $method->weight_unit); $address = (($cart->ST == 0) ? $cart->BT : $cart->ST); - - $products = 0; - $articles = 0; - foreach ($cart->products as $product) { - $products += 1; - $articles += $product->quantity; - } $zip = trim($address['zip']); - $cartvals = array('weight'=>$orderWeight, - 'zip'=>$zip, + $cartvals = array('zip'=>$zip, 'zip1'=>substr($zip,0,1), 'zip2'=>substr($zip,0,2), 'zip3'=>substr($zip,0,3), 'zip4'=>substr($zip,0,4), 'zip5'=>substr($zip,0,5), 'zip6'=>substr($zip,0,6), - 'articles'=>$articles, - 'products'=>$products, + 'articles'=>$this->getOrderArticles($cart), + 'products'=>$this->getOrderProducts($cart), 'amount'=>$cart_prices['salesPrice'], 'amountWithTax'=>$cart_prices['salesPrice'], 'amountWithoutTax'=>$cart_prices['priceWithoutTax'], @@ -431,6 +440,8 @@ class plgVmShipmentRules_Shipping_Base extends vmPSPlugin { 'country'=>$address['virtuemart_country_id'], ); + + $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); @@ -575,11 +586,11 @@ class plgVmShipmentRules_Shipping_Base extends vmPSPlugin { return $ret; } - -} } -if (!class_exists ('ShippingRule')) { +if (class_exists ('ShippingRule')) { + return; +} class ShippingRule { var $rulestring = ''; @@ -788,5 +799,4 @@ class ShippingRule { } -} // No closing tag -- GitLab