From 109220770ac3e5a6e749f6c25a4ddd8b19952303 Mon Sep 17 00:00:00 2001 From: Thomas Steur Date: Thu, 6 Nov 2014 03:19:24 +0100 Subject: refs #6435 fix some more wrong values if a German locale is used This caused me a lot of headache. I ran the system tests with a German locale and noticed so many values are completely different. I was looking for places where there were still any float to string conversions and I was wondering why the existing fix didn't work until I noticed the PiwikTracker did not send floats but floats with a comma as decimal point which in the end was casted to ints etc. --- libs/PiwikTracker/PiwikTracker.php | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) (limited to 'libs') diff --git a/libs/PiwikTracker/PiwikTracker.php b/libs/PiwikTracker/PiwikTracker.php index 051cb74745..39eb82b6bd 100644 --- a/libs/PiwikTracker/PiwikTracker.php +++ b/libs/PiwikTracker/PiwikTracker.php @@ -646,6 +646,9 @@ class PiwikTracker if (empty($sku)) { throw new Exception("You must specify a SKU for the Ecommerce item"); } + + $price = $this->forceDotAsSeparatorForDecimalPoint($price); + $this->ecommerceItems[$sku] = array($sku, $name, $category, $price, $quantity); } @@ -747,7 +750,9 @@ class PiwikTracker $this->pageCustomVar[self::CVAR_INDEX_ECOMMERCE_ITEM_CATEGORY] = array('_pkc', $category); if (!empty($price)) { - $this->pageCustomVar[self::CVAR_INDEX_ECOMMERCE_ITEM_PRICE] = array('_pkp', (float)$price); + $price = (float) $price; + $price = $this->forceDotAsSeparatorForDecimalPoint($price); + $this->pageCustomVar[self::CVAR_INDEX_ECOMMERCE_ITEM_PRICE] = array('_pkp', $price); } // On a category page, do not record "Product name not defined" @@ -763,6 +768,22 @@ class PiwikTracker $this->pageCustomVar[self::CVAR_INDEX_ECOMMERCE_ITEM_NAME] = array('_pkn', $name); } + /** + * Force the separator for decimal point to be a dot. See https://github.com/piwik/piwik/issues/6435 + * If for instance a German locale is used it would be a comma otherwise. + * + * @param float|string $value + * @return string + */ + private function forceDotAsSeparatorForDecimalPoint($value) + { + if (null === $value || false === $value) { + return $value; + } + + return str_replace(',', '.', $value); + } + /** * Returns URL used to track Ecommerce Cart updates * Calling this function will reinitializes the property ecommerceItems to empty array @@ -807,18 +828,23 @@ class PiwikTracker $url = $this->getRequest($this->idSite); $url .= '&idgoal=0'; if (!empty($grandTotal)) { + $grandTotal = $this->forceDotAsSeparatorForDecimalPoint($grandTotal); $url .= '&revenue=' . $grandTotal; } if (!empty($subTotal)) { + $subTotal = $this->forceDotAsSeparatorForDecimalPoint($subTotal); $url .= '&ec_st=' . $subTotal; } if (!empty($tax)) { + $tax = $this->forceDotAsSeparatorForDecimalPoint($tax); $url .= '&ec_tx=' . $tax; } if (!empty($shipping)) { + $shipping = $this->forceDotAsSeparatorForDecimalPoint($shipping); $url .= '&ec_sh=' . $shipping; } if (!empty($discount)) { + $discount = $this->forceDotAsSeparatorForDecimalPoint($discount); $url .= '&ec_dt=' . $discount; } if (!empty($this->ecommerceItems)) { @@ -876,7 +902,8 @@ class PiwikTracker $url .= '&e_n=' . urlencode($name); } if(strlen($value) > 0) { - $url .= '&e_v=' . $value; + $value = $this->forceDotAsSeparatorForDecimalPoint($value); + $url .= '&e_v=' . $value; } return $url; } @@ -982,7 +1009,8 @@ class PiwikTracker $url = $this->getRequest($this->idSite); $url .= '&idgoal=' . $idGoal; if (!empty($revenue)) { - $url .= '&revenue=' . $revenue; + $revenue = $this->forceDotAsSeparatorForDecimalPoint($revenue); + $url .= '&revenue=' . $revenue; } return $url; } -- cgit v1.2.3