| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPFunnels\Compatibility\Plugin; |
| 4 |
|
| 5 |
use WPFunnels\Traits\SingletonTrait; |
| 6 |
|
| 7 |
class FoxCurrencySwitcher extends PluginCompatibility{ |
| 8 |
use SingletonTrait; |
| 9 |
|
| 10 |
/** |
| 11 |
* Constructor: Initialize class and hook into price conversion. |
| 12 |
*/ |
| 13 |
public function init(){ |
| 14 |
add_filter('wpfunnels/modify_order_bump_price_on_main_order', array($this,'maybe_convert_product_price')); |
| 15 |
add_filter('wpfunnels/modify_order_bump_product_price', array($this, 'maybe_convert_order_bump_price')); |
| 16 |
add_filter('wpfunnels/checkout_ajax_custom_price', array($this, 'maybe_skip_custom_price_on_review'), 10, 3); |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Prevent the funnel checkout from zeroing the order total on update_order_review. |
| 21 |
* |
| 22 |
* On every checkout refresh AJAX, WPFunnels overrides each cart item price with the |
| 23 |
* stored `custom_price` at priority 9999, after WOOCS has already converted the price. |
| 24 |
* For a currency without a fixed price (or an unsynced `_price` meta) the stored |
| 25 |
* `custom_price` can resolve to 0, so forcing `set_price(0)` zeroes the order total — |
| 26 |
* the payment methods disappear and the checkout gets stuck in an endless refresh loop |
| 27 |
* (the public JS re-fires `update_checkout` whenever `cart_total <= 0`). |
| 28 |
* |
| 29 |
* Returning `false` skips the override so the already-converted price the customer saw |
| 30 |
* on the initial page render is preserved. Only kicks in while WOOCS is active. |
| 31 |
* |
| 32 |
* @param float $custom_price The stored custom price for the cart item. |
| 33 |
* @param array $cart_item The cart item. |
| 34 |
* @param object $cart The cart object. |
| 35 |
* |
| 36 |
* @return float|false The price to apply, or false to skip the override. |
| 37 |
*/ |
| 38 |
public function maybe_skip_custom_price_on_review($custom_price, $cart_item, $cart){ |
| 39 |
if ( $this->maybe_activate() && floatval($custom_price) <= 0 ) { |
| 40 |
return false; |
| 41 |
} |
| 42 |
|
| 43 |
return $custom_price; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Check if the Currency (WOOCS) is active. |
| 48 |
* |
| 49 |
* @return bool |
| 50 |
*/ |
| 51 |
public function maybe_activate(){ |
| 52 |
return class_exists('WOOCS'); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Convert the price for the order bump using the current currency |
| 57 |
* |
| 58 |
* @param float $price |
| 59 |
* @return float Converted price |
| 60 |
*/ |
| 61 |
public function maybe_convert_product_price($price){ |
| 62 |
global $WOOCS; |
| 63 |
|
| 64 |
if ($this->maybe_activate()) { |
| 65 |
if ($WOOCS->is_multiple_allowed) { |
| 66 |
$current = $WOOCS->current_currency; |
| 67 |
if ($current != $WOOCS->default_currency) { |
| 68 |
$currencies = $WOOCS->get_currencies(); |
| 69 |
$rate = $currencies[$current]['rate']; |
| 70 |
$price = $price / $rate; |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
return $price; |
| 76 |
} |
| 77 |
|
| 78 |
public function maybe_convert_order_bump_price($price){ |
| 79 |
global $WOOCS; |
| 80 |
|
| 81 |
if ($this->maybe_activate()) { |
| 82 |
if ($WOOCS->is_multiple_allowed) { |
| 83 |
$price = $WOOCS->woocs_exchange_value(floatval($price)); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
return $price; |
| 88 |
} |
| 89 |
} |
| 90 |
|