|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Adds currency to invididual countries |
|
5
|
|
|
* |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
class CountryPrice_EcommerceCurrency extends DataExtension |
|
|
|
|
|
|
9
|
|
|
{ |
|
10
|
|
|
private static $has_many = array( |
|
|
|
|
|
|
11
|
|
|
"EcommerceCountries" => "EcommerceCountry" |
|
12
|
|
|
); |
|
13
|
|
|
|
|
14
|
|
|
public function updateCMSFields(FieldList $fields) |
|
15
|
|
|
{ |
|
16
|
|
|
$fields->addFieldToTab( |
|
17
|
|
|
"Root.Countries", |
|
18
|
|
|
CheckboxSetField::create( |
|
19
|
|
|
'EcommerceCountries', |
|
20
|
|
|
'Countries', |
|
21
|
|
|
EcommerceCountry::get() |
|
22
|
|
|
->filter(array("EcommerceCurrencyID" => array(0, $this->owner->ID))) |
|
23
|
|
|
->sort(array("EcommerceCurrencyID" => "DESC", "Name" => "ASC")) |
|
24
|
|
|
->map() |
|
25
|
|
|
) |
|
26
|
|
|
); |
|
27
|
|
|
$fields->removeFieldFromTab("Root", "EcommerceCountries"); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param strin $countryCode |
|
33
|
|
|
* @return EcommerceCurrency |
|
34
|
|
|
*/ |
|
35
|
|
|
public static function get_currency_for_country($countryCode) |
|
36
|
|
|
{ |
|
37
|
|
|
$countryObject = CountryPrice_EcommerceCountry::get_real_country($countryCode); |
|
38
|
|
|
if ($countryObject) { |
|
39
|
|
|
$countryCode = $countryObject->Code; |
|
40
|
|
|
} |
|
41
|
|
|
$currencyPerCountry = CountryPrice_EcommerceCurrency::get_currency_per_country(); |
|
42
|
|
|
$currencyDO = null; |
|
43
|
|
|
if ($countryCode) { |
|
44
|
|
|
$currencyCode = isset($currencyPerCountry[$countryCode]) ? $currencyPerCountry[$countryCode] : EcommerceCurrency::default_currency_code(); |
|
45
|
|
|
$currencyDO = EcommerceCurrency::get_one_from_code($currencyCode); |
|
46
|
|
|
} |
|
47
|
|
|
if (! $currencyDO) { |
|
48
|
|
|
$currencyDO = EcommerceCurrency::create_new($currencyCode); |
|
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
if (! $currencyDO) { |
|
51
|
|
|
$currencyDO = EcommerceCurrency::get_default(); |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
return $currencyDO; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* finds the currency for each country |
|
58
|
|
|
* If no currency is found then the default currency is added. |
|
59
|
|
|
* returns something like |
|
60
|
|
|
* |
|
61
|
|
|
* NZ => NZD |
|
62
|
|
|
* AU => AUD |
|
63
|
|
|
* |
|
64
|
|
|
* @return array - list of countries and their currencies ... |
|
65
|
|
|
*/ |
|
66
|
|
|
public static function get_currency_per_country() |
|
67
|
|
|
{ |
|
68
|
|
|
$cachekey = "EcommerceCurrencyCountryMatrix"; |
|
69
|
|
|
$cache = SS_Cache::factory('CountryPrice_EcommerceCurrency'); |
|
70
|
|
|
if (! ($serializedArray = $cache->load($cachekey))) { |
|
71
|
|
|
$countries = CountryPrice_EcommerceCountry::get_real_countries_list(); |
|
72
|
|
|
$unserializedArray = array(); |
|
73
|
|
|
$defaultCurrencyCode = EcommerceCurrency::default_currency_code(); |
|
74
|
|
|
foreach ($countries as $countryObject) { |
|
75
|
|
|
$currencyCode = $defaultCurrencyCode; |
|
76
|
|
|
$currency = $countryObject->EcommerceCurrency(); |
|
77
|
|
|
if ($currency && $currency->exists()) { |
|
78
|
|
|
$currencyCode = $currency->Code; |
|
79
|
|
|
} |
|
80
|
|
|
$countryObject = CountryPrice_EcommerceCountry::get_real_country($countryObject); |
|
81
|
|
|
$unserializedArray[$countryObject->Code] = $currencyCode; |
|
82
|
|
|
} |
|
83
|
|
|
$cache->save(serialize($unserializedArray), $cachekey); |
|
84
|
|
|
return $unserializedArray; |
|
85
|
|
|
} |
|
86
|
|
|
return unserialize($serializedArray); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* list of currencies used on the site |
|
91
|
|
|
* @return Array |
|
92
|
|
|
*/ |
|
93
|
|
|
public static function get_currency_per_country_used_ones() |
|
94
|
|
|
{ |
|
95
|
|
|
$resultArray = array(); |
|
96
|
|
|
$functioningCountryObjects = EcommerceCountry::get() |
|
97
|
|
|
->filter(array("DoNotAllowSales" => 0, 'AlwaysTheSameAsID' => 0)) |
|
98
|
|
|
->exclude(array("DistributorID" => 0)); |
|
99
|
|
|
$countryCurrencies = CountryPrice_EcommerceCurrency::get_currency_per_country(); |
|
100
|
|
|
if ($functioningCountryObjects->count()) { |
|
101
|
|
|
$countryCodes = $functioningCountryObjects->map("Code", "Code")->toArray(); |
|
102
|
|
|
foreach ($countryCodes as $countryCode => $countryCodeAlso) { |
|
103
|
|
|
if (isset($countryCurrencies[$countryCode])) { |
|
104
|
|
|
$resultArray[$countryCode] = $countryCurrencies[$countryCode]; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $resultArray; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
private static $_money_object_currency = array(); |
|
113
|
|
|
/*** |
|
114
|
|
|
* |
|
115
|
|
|
* updates the Currency Code in the get_money_object_from_order_currency function |
|
116
|
|
|
* this is required for the googlebot read correctly the correct currencty as it does not have an order (see allowWrites in ShoppingCart class) |
|
117
|
|
|
* @param string $currencyCode |
|
118
|
|
|
* @return Array |
|
119
|
|
|
*/ |
|
120
|
|
|
public function updateCurrencyCodeForMoneyObect($currencyCode) |
|
121
|
|
|
{ |
|
122
|
|
|
$countryCode = ''; |
|
123
|
|
|
$countryObject = CountryPrice_EcommerceCountry::get_real_country(); |
|
124
|
|
|
if ($countryObject) { |
|
125
|
|
|
$countryCode = $countryObject->Code; |
|
126
|
|
|
} |
|
127
|
|
|
if ($countryCode === '' || $countryCode === EcommerceConfig::get('EcommerceCountry', 'default_country_code')) { |
|
128
|
|
|
return null; |
|
129
|
|
|
} |
|
130
|
|
|
$key = $this->owner->ClassName.'____'.$countryCode; |
|
131
|
|
|
if (! isset(self::$_money_object_currency[$key])) { |
|
132
|
|
|
if(is_null($currencyCode)){ |
|
133
|
|
|
$currency = CountryPrice_EcommerceCurrency::get_currency_for_country($countryCode); |
|
134
|
|
|
if ($currency) { |
|
135
|
|
|
$currencyCode = strtoupper($currency->Code); |
|
136
|
|
|
} |
|
137
|
|
|
else { |
|
138
|
|
|
return null; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
self::$_money_object_currency[$key] = $currencyCode; |
|
142
|
|
|
} |
|
143
|
|
|
return self::$_money_object_currency[$key]; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.