Skip to content

Instantly share code, notes, and snippets.

@banarsiamin
Created January 17, 2024 11:42
Show Gist options
  • Save banarsiamin/2a388996e1abb1a6facbcd1737479dc0 to your computer and use it in GitHub Desktop.
Save banarsiamin/2a388996e1abb1a6facbcd1737479dc0 to your computer and use it in GitHub Desktop.
<?php
/*
*
* Description: Simplified and faster WooCommerce Set Single Currency with live echenge
* Author: banarsiamin
* Author URI: https://www.ayatinfotech.com
* Version: 1.0.0
*/
if ( !in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
function akbCE_wc_admin_notice() {
deactivate_plugins(__FILE__);
if ( isset( $_GET['activate'] ) ) {
unset( $_GET['activate'] );
}
?>
<div id="message" class="error">
<p>This plugin requires <a href="https://wordpress.org/extend/plugins/woocommerce/">WooCommerce</a> plugin to be installed and active!</p>
</div>
<?php
}
add_action( 'admin_notices', 'akbCE_wc_admin_notice' );
}
// Display Fields
add_action('woocommerce_product_options_general_product_data', 'akbce_woocommerce_product_custom_fields');
// Save Fields
add_action('woocommerce_process_product_meta', 'akbce_woocommerce_product_custom_fields_save');
function akbce_woocommerce_product_custom_fields()
{
global $woocommerce, $post;
$admin_url = admin_url();
$p_id = get_the_ID();
global $wpdb;
$wp_posts = $wpdb->prefix . "posts";
$wp_postmeta = $wpdb->prefix . "postmeta";
$_akbce_status_field = get_post_meta(get_the_ID(),'_akbce_status_field',true);
$_akbce_price_in_usd = get_post_meta(get_the_ID(),'_akbce_price_in_usd',true);
?>
<div id="ayt_product_data" class="product_custom_field">
<p class="form-field _akbce_status_field_field ">
<label for="_akbce_status_field">ZAR price to adjust automatically as the exchange rate</label>
<input type="checkbox" class="short" style="width: auto;" name="_akbce_status_field" id="_akbce_status_field" value="yes" <?php echo (isset($_akbce_status_field) && ('yes'==$_akbce_status_field))?'checked':'';?>>
</p>
<p class="form-field _akbce_price_in_usd_field ">
<label for="_akbce_price_in_usd">Set USD Price to change ZAR IN front end site</label>
<input type="number" class="short" style="" name="_akbce_price_in_usd" id="_akbce_price_in_usd" value="<?php echo $_akbce_price_in_usd;?>" placeholder="Enter Price in USD" step="any" min="0">
</p>
</div>
<?php
}
function akbce_woocommerce_product_custom_fields_save($post_id)
{
// Custom Product Text Field
$woocommerce_akbce_status_field = $_POST['_akbce_status_field'];
if (!empty($woocommerce_akbce_status_field))
update_post_meta($post_id, '_akbce_status_field', esc_attr($woocommerce_akbce_status_field));
// Custom Product Number Field
$woocommerce_akbce_price_in_usd = $_POST['_akbce_price_in_usd'];
if (!empty($woocommerce_akbce_price_in_usd))
update_post_meta($post_id, '_akbce_price_in_usd', esc_attr($woocommerce_akbce_price_in_usd));
}
// add_action('wp_ajax_akbce_liveAPI_ajaxfn', 'akbce_liveAPI_ajaxfn');
// add_action('wp_ajax_nopriv_akbce_liveAPI_ajaxfn', 'akbce_liveAPI_ajaxfn');
if(isset($_GET['ayt'])){
// echo akbce_convert_product_price_live(19893);
}
function akbce_convert_product_price_live($priceUSD,$price=''){
$priceUSD = !empty($priceUSD)? $priceUSD:'';
$accessKey = '*******************************';
$baseCurrency = 'USD';
$targetCurrency = 'ZAR';
// Make an API request to get the latest exchange rates
// $apiUrl = "http://data.fixer.io/api/latest?access_key=$accessKey&base=$baseCurrency&symbols=$targetCurrency";
$apiUrl = "https://v6.exchangerate-api.com/v6/$accessKey/latest/$baseCurrency";
$ch = curl_init($apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
if ($response === false) {
die('Error fetching data from Fixer API.');
}
// Decode the JSON response
$data = json_decode($response, true);
if(isset($_GET['ayt'])){
echo "<PRE>";print_r($data);die;
}
$conversion_rates = isset($data['conversion_rates'][$targetCurrency])?$data['conversion_rates'][$targetCurrency]:'';
if(!empty($conversion_rates) && $conversion_rates!=0){
$zarPrice = round(($priceUSD * $conversion_rates), 0);
// Round to the nearest multiple of 5
// $zarPrice = round($zarPrice / 5) * 5;
// echo strlen($zarPrice);die;
if(strlen($zarPrice)>3){
$newLastTwoDigits=95;
$zarPrice = substr($zarPrice, 0, -2) . $newLastTwoDigits;
}
}else{
$zarPrice = $price;
}
return $zarPrice;
}
// Hook to modify product price on the front end this
function akbce_modify_product_price($price, $product) {
$product_id = $product->get_id();
$_akbce_status = get_post_meta($product_id,'_akbce_status_field',true);
$_akbce_usd_pr = get_post_meta($product_id,'_akbce_price_in_usd',true);
if($_akbce_status=='yes' && !empty($_akbce_usd_pr) && $_akbce_usd_pr!=0){
// Check if it's a simple product
if ($product->is_type('simple')) {
$converted_price = akbce_convert_product_price_live($_akbce_usd_pr,$price);
return wc_price($converted_price);
}
}else{
return $price;
}
}
add_filter('woocommerce_get_price_html', 'akbce_modify_product_price', 10, 2);
@banarsiamin
Copy link
Author

Woo commerce expert in currency conversion

Job Description: I need help on my south african woo commerce website. I need to add the $ price in the back end and show the ZAR price in the front end to the customer. i would also need the ZAR price to adjust automatically as the exchange rate changes and oi need it to be product specific

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment