1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* LSX Currency Frontend Class |
4
|
|
|
* |
5
|
|
|
* @package LSX Currencies |
6
|
|
|
* @author LightSpeed |
7
|
|
|
* @license GPL3 |
8
|
|
|
* @link |
9
|
|
|
* @copyright 2019 LightSpeed |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace lsx\currencies\classes; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Holds the WooCommerce Integrations |
16
|
|
|
*/ |
17
|
|
|
class WooCommerce { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Holds instance of the class |
21
|
|
|
* |
22
|
|
|
* @var object \lsx\currencies\classes\WooCommerce() |
23
|
|
|
*/ |
24
|
|
|
private static $instance; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Holds the current currency. |
28
|
|
|
* |
29
|
|
|
* @var boolean |
30
|
|
|
*/ |
31
|
|
|
public $currency = false; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Constructor |
35
|
|
|
*/ |
36
|
|
|
public function __construct() { |
|
|
|
|
37
|
|
|
add_filter( 'wc_price', array( $this, 'price_filter' ), 300, 3 ); |
38
|
|
|
add_filter( 'lsx_currencies_base_currency', array( $this, 'set_base_currency' ), 10, 1 ); |
39
|
|
|
} |
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Return an instance of this class. |
43
|
|
|
* |
44
|
|
|
* @return object |
45
|
|
|
*/ |
46
|
|
|
public static function init() { |
47
|
|
|
// If the single instance hasn't been set, set it now. |
48
|
|
|
if ( ! isset( self::$instance ) ) { |
|
|
|
|
49
|
|
|
self::$instance = new self(); |
50
|
|
|
} |
|
|
|
|
51
|
|
|
return self::$instance; |
52
|
|
|
} |
|
|
|
|
53
|
|
|
|
54
|
|
|
/** |
|
|
|
|
55
|
|
|
* Filter the WooCommerce Price. |
56
|
|
|
* |
57
|
|
|
* @param $return mixed |
|
|
|
|
58
|
|
|
* @param $price string |
|
|
|
|
59
|
|
|
* @param $args array |
|
|
|
|
60
|
|
|
* |
61
|
|
|
* @return mixed |
62
|
|
|
*/ |
63
|
|
|
public function price_filter( $return, $price, $args ) { |
64
|
|
|
if ( '' !== $price ) { |
|
|
|
|
65
|
|
|
$return = str_replace( 'class', 'data-price-' . lsx_currencies()->base_currency . '=' . $price . ' class', $return ); |
66
|
|
|
$return = str_replace( 'woocommerce-Price-amount', 'woocommerce-Price-amount lsx-currencies', $return ); |
67
|
|
|
} |
|
|
|
|
68
|
|
|
return $return; |
69
|
|
|
} |
|
|
|
|
70
|
|
|
|
71
|
|
|
/** |
|
|
|
|
72
|
|
|
* @param $cart_subtotal |
|
|
|
|
73
|
|
|
* @param $compound |
|
|
|
|
74
|
|
|
* @param $obj |
|
|
|
|
75
|
|
|
* |
76
|
|
|
* @return mixed |
77
|
|
|
*/ |
78
|
|
|
public function cart_subtotal( $cart_subtotal, $compound, $obj ) { |
|
|
|
|
79
|
|
|
|
80
|
|
|
$return = str_replace( 'class', 'data-price-' . $this->currency . '=' . $price . ' class', $return ); |
81
|
|
|
$return = str_replace( 'woocommerce-Price-amount', 'woocommerce-Price-amount lsx-currencies', $return ); |
82
|
|
|
|
83
|
|
|
return $cart_subtotal; |
84
|
|
|
} |
|
|
|
|
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Make sure our base currency is set to the same as woocommerce. |
88
|
|
|
* |
89
|
|
|
* @param string $currency |
|
|
|
|
90
|
|
|
* @return void |
|
|
|
|
91
|
|
|
*/ |
92
|
|
|
public function set_base_currency( $currency ) { |
93
|
|
|
return get_woocommerce_currency(); |
94
|
|
|
} |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|