Skip to content

Instantly share code, notes, and snippets.

@mathvp
Created May 22, 2023 23:56
Show Gist options
  • Save mathvp/4b60588d61bd737cb2b7a06500584885 to your computer and use it in GitHub Desktop.
Save mathvp/4b60588d61bd737cb2b7a06500584885 to your computer and use it in GitHub Desktop.
Peachpay Currency
<?php
/*
Plugin Name: Peachpay Currency Switcher shortcode
Description: Custom Peachpay currency switcher shortcode
Version: 1.0.0
Author: Matheus Vieira
Author URI: https://github.com/mathvp
License: GPL2
*/
if (!defined('ABSPATH')) {
die;
}
function peachpay_currency_switcher_register_plugin_scripts () {
if ( ! wp_script_is( 'peachpay_currency_widget', 'enqueued' )) {
wp_register_script('peachpay_currency_widget', plugins_url( 'peachpay-for-woocommerce/public/dist/currency-switcher-widget.bundle.js' ), array(), '1.92.2', true);
}
}
add_action( 'wp_enqueue_scripts', 'peachpay_currency_switcher_register_plugin_scripts' );
function custom_peachpay_currency_switcher() {
$currencies = peachpay_currencies_by_iso( peachpay_get_client_country() );
$active_currency = ( $_COOKIE && isset( $_COOKIE['pp_active_currency'] ) ) ? sanitize_text_field( wp_unslash( $_COOKIE['pp_active_currency'] ) ) : peachpay_best_currency( peachpay_get_client_country() );
$html = '<select id=pp-currency-widget>';
foreach ( $currencies as $currency ) {
$html .= '<option value="' . esc_html( $currency );
$html .= $active_currency === $currency ? '" selected>' : '">';
$html .= esc_html( PEACHPAY_SUPPORTED_CURRENCIES[ $currency ] );
$html .= '</option>';
}
$html .= '</select>';
return $html;
}
add_shortcode('custom-peachpay-currency-switcher', 'custom_peachpay_currency_switcher');
// Just use the shortcode: [custom-peachpay-currency-switcher]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment