PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.4
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.4
1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk 1.2.0 All 47 releases
fluent-cart / api / CurrencySettings.php

CurrencySettings.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.6.4, at api/CurrencySettings.php

179 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\Api;
4
5 use FluentCart\App\Helpers\CurrenciesHelper;
6 use FluentCart\App\Helpers\Helper;
7 use FluentCart\Framework\Support\Arr;
8
9 class CurrencySettings
10 {
11
12 /**
13 * @param string $key Store settings option name
14 *
15 * without param it will return all currency settings | defaults
16 *
17 *
18 * @return array|\ArrayAccess|mixed|null
19 */
20 public static function get(string $key = '')
21 {
22
23 $storeSettings = (new StoreSettings())->get();
24
25 $settings = Arr::only($storeSettings, array(
26 'currency',
27 'locale',
28 'currency_position',
29 'currency_separator',
30 'decimal_separator',
31 'order_mode',
32 ));
33
34 $settings = [
35 'currency' => static::getValue($settings, 'currency', 'USD'),
36 'locale' => static::getValue($settings, 'locale', 'auto'),
37 'currency_position' => static::getValue($settings, 'currency_position', 'left'),
38 'currency_separator' => static::getValue($settings, 'currency_separator', 'dot'),
39 'decimal_separator' => static::getValue($settings, 'decimal_separator', '.'),
40 'decimal_points' => static::getValue($settings, 'decimal_points', 0),
41 'settings_type' => static::getValue($settings, 'settings_type', 'global'),
42 'order_mode' => static::getValue($settings, 'order_mode', 'test')
43 ];
44
45 $settings['is_zero_decimal'] = CurrenciesHelper::isZeroDecimal($settings['currency']);
46 $settings['currency_sign'] = html_entity_decode(CurrenciesHelper::getCurrencySign($settings['currency']));
47
48 if ($key) {
49 return Arr::get($settings, $key);
50 }
51
52 return apply_filters('fluent_cart/global_currency_setting', $settings, []);
53 }
54
55 public function getCurrency($settings = [])
56 {
57 return static::get('currency');
58 }
59
60 public static function getValue(array $settings, $key, $default = null)
61 {
62 $value = Arr::get($settings, $key, $default);
63 if (empty($value)) {
64 return $default;
65 }
66 return $value;
67 }
68
69 /**
70 * @param string $currencyCode like 'USD'
71 * @return bool
72 *
73 * check if currency is zero decimal
74 */
75 public function isZeroDecimal($currencyCode)
76 {
77 return CurrenciesHelper::isZeroDecimal($currencyCode);
78 }
79
80 public static function convertCentToUnit($amount)
81 {
82 $unitAmount = 0;
83 if (intval($amount)) {
84 $unitAmount = intval($amount) / 100;
85 }
86 return $unitAmount;
87 }
88
89 /**
90 * @return array
91 *
92 * Get label value array for all currencies
93 */
94 public static function getFormattedCurrencies()
95 {
96 $currencies = CurrenciesHelper::getCurrencies();
97 $formatted = array();
98 foreach ($currencies as $code => $name) {
99 $formatted[] = array(
100 "label" => $name,
101 "value" => $code,
102 );
103 }
104 return $formatted;
105 }
106
107 public static function getPriceHtml($amount, $currencyCode = null, $showDecimal = true, $withTranslatedDigit = false)
108 {
109 return static::getFormattedPrice($amount, $currencyCode, false, $showDecimal, $withTranslatedDigit);
110 }
111
112 public static function getFormattedPrice($amount, $currencyCode = null, $asList = false, $showDecimal = true, $withTranslatedDigit = false)
113 {
114 $settings = static::get();
115
116 if (!$currencyCode) {
117 $currencyCode = Arr::get($settings, 'currency');
118 }
119
120 $sign = CurrenciesHelper::getCurrencySign($currencyCode);
121 $position = Arr::get($settings, 'currency_position', 'before');
122
123 // Decimal configuration
124 $decimal = $showDecimal ? 2 : 0;
125
126 if (!empty($settings['is_zero_decimal'])) {
127 $decimal = 0;
128 }
129
130 // Separator configuration
131 $decimalSeparatorSetting = Arr::get($settings, 'decimal_separator', 'dot');
132 $decimalSeparator = $decimalSeparatorSetting === 'comma' ? ',' : '.';
133 $thousandSeparator = $decimalSeparator === ',' ? '.' : ',';
134
135 // Sanitize amount
136 $amount = is_numeric($amount) ? $amount : 0;
137 $amount = floatval($amount / 100);
138
139 // Format number
140 $price = number_format($amount, $decimal, $decimalSeparator, $thousandSeparator);
141
142 if($withTranslatedDigit) {
143 $price = Helper::translateNumber($price);
144 }
145
146 // If array output is requested
147 if ($asList) {
148 return [
149 'price' => $price,
150 'currency_sign' => $sign,
151 'currency_code' => $currencyCode,
152 'currency_title' => CurrenciesHelper::getCurrencies($currencyCode),
153 'position' => $position
154 ];
155 }
156
157 // Apply full currency position logic (same as toDecimal)
158 switch ($position) {
159 case 'before':
160 return $sign . $price;
161 case 'after':
162 return $price . $sign;
163 case 'iso_before':
164 return $currencyCode . ' ' . $price;
165 case 'iso_after':
166 return $price . ' ' . $currencyCode;
167 case 'symbool_before_iso':
168 return $sign . $price . ' ' . $currencyCode;
169 case 'symbool_after_iso':
170 return $currencyCode . ' ' . $price . $sign;
171 case 'symbool_and_iso':
172 return $currencyCode . ' ' . $sign . $price;
173 default:
174 return $sign . $price;
175 }
176 }
177
178 }
179