Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpmudev-sls/84a307ea2277f234c4ad8527b42351d8 to your computer and use it in GitHub Desktop.
Save wpmudev-sls/84a307ea2277f234c4ad8527b42351d8 to your computer and use it in GitHub Desktop.
[Forminator] - Support multiple currencies in the currency field.
<?php
/**
* Plugin Name: [Forminator] - Support multiple currencies in the currency field.
* Description: [Forminator] - Support multiple currencies in the currency field.
* Jira: SLS-303
* Author: Thobk @ WPMUDEV
* Author URI: https://premium.wpmudev.org
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
} elseif ( defined( 'WP_CLI' ) && WP_CLI ) {
return;
}
add_action( 'after_setup_theme', 'wpmudev_forminator_support_multi_currencies_in_currence_field_func', 100 );
function wpmudev_forminator_support_multi_currencies_in_currence_field_func() {
if ( defined('FORMINATOR_PRO') && class_exists( 'Forminator' ) ) {
class WPMUDEV_FM_MU_Currencies{
public $include_form_ids = [];
public $exclude_form_ids = [];//enter list exclude form ids, e.g: [234,456]
private $default_currency;
private $current_form_id;
public function __construct(){
add_filter( 'forminator_field_markup', array( $this, 'custom_currency_field' ), 10, 3 );
add_action( 'wp_enqueue_scripts', array( $this, 'custom_assets' ) );
add_action( 'wp_footer', array( $this, 'custom_script' ), 21 );
add_filter( 'forminator_paypal_properties', array( $this, 'save_default_currency' ), 9999 );
add_filter( 'forminator_custom_form_mail_admin_message', array( $this, 'wpmudev_show_formatted_currency' ), 10, 5 );
}
public function wpmudev_show_formatted_currency( $message, $custom_form, $data, $entry, $cls ) {
if( ! empty( $this->include_form_ids ) ){
if( in_array( $custom_form->id, $this->include_form_ids ) ){
$message = str_replace( 'USD', $data['wpmudev-currency'], $message ) ;
}
}
return $message;
}
public function custom_assets(){
wp_enqueue_style( 'select2', 'https://cdn.jsdelivr.net/npm/select2@4.1.0-beta.1/dist/css/select2.min.css', array('forminator-utilities') );
$data = '
.forminator-suffix.wpmudev-currency-box, .forminator-suffix.wpmudev-currency-box select{
width:65px;
}
.forminator-suffix.wpmudev-currency-box{
padding:0!important;
right:5px!important;
margin-top:-2px;
}
';
wp_add_inline_style( 'select2', $data );
}
public function enabled_on_this_form( $form_id ){
static $checked_forms = [];
if( ! isset( $checked_forms[ $form_id ] ) ){
if( ! empty( $this->include_form_ids ) ){
if( ! in_array( $form_id, $this->include_form_ids ) ){
$checked_forms[ $form_id ] = false;
}
}elseif( ! empty( $this->exclude_form_ids ) && in_array( $form_id, $this->exclude_form_ids ) ){
$checked_forms[ $form_id ] = false;
}
$checked_forms[ $form_id ] = isset( $checked_forms[ $form_id ] ) ? $checked_forms[ $form_id ] : true;
}
return $checked_forms[ $form_id ];
}
public function custom_currency_field( $html, $field, $custom_form ){
if( empty( $field['type'] ) || 'currency' !== $field['type'] || ! $this->enabled_on_this_form( $custom_form->model->id ) ){
return $html;
}
$paypal_list_currencies = forminator_pp_currency_list();
$mul_currencies_field = '<div class="forminator-suffix wpmudev-currency-box"><select id="wpmudev-currency-field" name="wpmudev-currency">';
foreach( $paypal_list_currencies as $code => $name ){
$mul_currencies_field .= sprintf('<option value="%s" %s>%s</option>', $code, selected( $field['currency'], $code, false ), $code );
}
$mul_currencies_field .='</select></div>';
$html = str_replace(sprintf('<span class="forminator-suffix">%s</span>', $field['currency'] ), $mul_currencies_field, $html );
return $html;
}
public function save_default_currency( $properties ){
if( isset( $properties['currency'] ) ){
$this->default_currency = $properties['currency'];
}
return $properties;
}
public function custom_script(){
if( ! $this->default_currency ){
return;
}
?>
<script>
(function($){
$(function(){
if (typeof ($.fn.forminatorLoader) === 'undefined') {
// nothing to do here
} else {
// support ajax form
$(document).on('after.load.forminator', function(e, _form_id){
let _form = $('#forminator-module-'+ _form_id),
_curreny_field = _form.find('#wpmudev-currency-field'),
_default_currency = '<?php echo esc_js( $this->default_currency );?>',
_paypal_script = $("script[src^='https://www.paypal.com/sdk/js']"),
_paypal_button = _form.find('#paypal-button-container-'+ _form_id );
if( _curreny_field.length && _paypal_script.length && _paypal_button.length ){
// select field
wpmudev_autocomplete_select_field( _curreny_field );
_curreny_field.on('change', function(){
let _new_currency = $(this).val(),
_paypal_form = _form.data('forminatorFrontPayPal');
if( _paypal_form && _new_currency !== _default_currency ){
_paypal_script.attr('src', _paypal_script.attr('src').replace('currency='+ _default_currency, 'currency='+ _new_currency ) );
_default_currency = _new_currency;
// empty button
_paypal_button.empty();
// load button again
_paypal_form.render_paypal_button();
}
});
}
});
function wpmudev_autocomplete_select_field( _curreny_field ){
_curreny_field.FUIselect2();
}
}
});
})(window.jQuery)
</script>
<?php
}
}
$run = new WPMUDEV_FM_MU_Currencies;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment