diff options
author | Thomas Steur <thomas.steur@googlemail.com> | 2014-11-06 05:19:24 +0300 |
---|---|---|
committer | Thomas Steur <thomas.steur@googlemail.com> | 2014-11-07 03:26:02 +0300 |
commit | 109220770ac3e5a6e749f6c25a4ddd8b19952303 (patch) | |
tree | 0d12b730e99428ffc644adbfb751afaf6b319e4b /libs | |
parent | f7b1c7a816c12d67dc3bc45d340c548f712594a0 (diff) |
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.
Diffstat (limited to 'libs')
-rw-r--r-- | libs/PiwikTracker/PiwikTracker.php | 34 |
1 files changed, 31 insertions, 3 deletions
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" @@ -764,6 +769,22 @@ class PiwikTracker } /** + * 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 * so items will have to be added again via addEcommerceItem() @@ -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; } |