/** * Plugin Name: Custom WooCommerce Currency Switcher * Description: A floating currency switcher for WooCommerce with a pop-up selector. * Version: 1.2 * Author: Your Name * License: GPL2 */ // Enqueue CSS and JavaScript function custom_currency_switcher_assets() { wp_enqueue_style('currency-switcher-css', plugin_dir_url(__FILE__) . 'css/currency-switcher.css'); wp_enqueue_script('jquery'); wp_enqueue_script('currency-switcher-js', plugin_dir_url(__FILE__) . 'js/currency-switcher.js', array('jquery'), null, true); wp_localize_script('currency-switcher-js', 'currencySwitcherAjax', array( 'ajax_url' => admin_url('admin-ajax.php'), )); } add_action('wp_enqueue_scripts', 'custom_currency_switcher_assets'); // Ensure WooCommerce session starts properly function start_woocommerce_session() { if (!session_id()) { session_start(); } if (WC()->session) { WC()->session->set('currency', WC()->session->get('currency', 'USD')); } } add_action('init', 'start_woocommerce_session'); // Currency symbols mapping function get_currency_symbols() { return array( 'USD' => '$', 'NGN' => '₦', 'GHS' => '₵', 'ZAR' => 'R' ); } // Add currency switcher HTML (Removed from checkout page) // Add currency switcher only on specific pages function custom_currency_switcher_html() { if (is_checkout() || is_shop()) { return; // Don't show on checkout or shop page } // Check if we're on a single product page and if the product belongs to the "recipe-book" category if (is_product()) { global $post; $product = wc_get_product($post->ID); if (!$product || !has_term('recipe-book', 'product_cat', $product->get_id())) { return; // Hide switcher if not in the "recipe-book" category } } // Ensure it shows on the cart page if (is_cart() || is_product()) { $selected_currency = WC()->session->get('currency', 'USD'); $currencies = array('USD' => 'USD', 'NGN' => 'NGN', 'GHS' => 'GHS', 'ZAR' => 'ZAR'); echo '
'; echo '
Currency: ' . esc_html($selected_currency) . '
'; echo '
'; // Currency Popup echo '
'; echo ''; echo ''; echo '
'; } } add_action('wp_footer', 'custom_currency_switcher_html'); // Handle AJAX currency switching function set_currency() { if (!WC()->session) { WC()->initialize_session(); } if (isset($_POST['currency'])) { $currency = sanitize_text_field($_POST['currency']); WC()->session->set('currency', $currency); // Ensure prices update dynamically via AJAX WC()->cart->calculate_totals(); wp_send_json_success(array('currency' => $currency)); } else { wp_send_json_error(array('message' => 'Invalid request')); } wp_die(); } add_action('wp_ajax_set_currency', 'set_currency'); add_action('wp_ajax_nopriv_set_currency', 'set_currency'); // Display correct price and currency symbol function custom_currency_display_price($price, $product) { if (!WC()->session) { WC()->initialize_session(); } $selected_currency = WC()->session->get('currency', 'USD'); $currency_symbols = get_currency_symbols(); $custom_price = get_post_meta($product->get_id(), '_custom_price_' . strtolower($selected_currency), true); if (!empty($custom_price)) { $symbol = isset($currency_symbols[$selected_currency]) ? $currency_symbols[$selected_currency] : $selected_currency; return $symbol . number_format($custom_price, 2); } return $price; } add_filter('woocommerce_get_price_html', 'custom_currency_display_price', 10, 2); // Update Cart & Checkout Prices Based on Selected Currency function update_cart_totals($cart) { if (!WC()->session) { return; } $selected_currency = WC()->session->get('currency', 'USD'); $currency_symbols = get_currency_symbols(); foreach ($cart->get_cart() as $cart_item) { $product = wc_get_product($cart_item['product_id']); $custom_price = get_post_meta($product->get_id(), '_custom_price_' . strtolower($selected_currency), true); if (!empty($custom_price)) { $cart_item['data']->set_price($custom_price); } } } add_action('woocommerce_before_calculate_totals', 'update_cart_totals', 10, 1); // Ensure Checkout Uses Selected Currency and Symbol function set_currency_at_checkout($currency) { if (WC()->session) { return WC()->session->get('currency', 'USD'); } return $currency; } add_filter('woocommerce_currency', 'set_currency_at_checkout'); // Add AJAX refresh for cart page when switching currency function refresh_cart_on_currency_change() { wc_add_notice(__('Currency updated successfully.', 'woocommerce'), 'success'); wp_send_json_success(); } add_action('wp_ajax_refresh_cart', 'refresh_cart_on_currency_change'); add_action('wp_ajax_nopriv_refresh_cart', 'refresh_cart_on_currency_change'); // Add custom price fields for different currencies in the product edit page function add_custom_currency_fields() { global $post; echo '
'; $currencies = array( 'USD' => 'US Dollar', 'NGN' => 'Nigerian Naira', 'GHS' => 'Ghanaian Cedi', 'ZAR' => 'South African Rand' ); foreach ($currencies as $currency_code => $currency_label) { woocommerce_wp_text_input(array( 'id' => '_custom_price_' . strtolower($currency_code), 'label' => __('Price in ' . $currency_label, 'woocommerce'), 'desc_tip' => 'true', 'description' => __('Set a fixed price for ' . $currency_label . '.', 'woocommerce'), 'type' => 'number', 'custom_attributes' => array( 'step' => '0.01', 'min' => '0' ), )); } echo '
'; } add_action('woocommerce_product_options_pricing', 'add_custom_currency_fields'); // Save the custom prices function save_custom_currency_fields($post_id) { $currencies = array('USD', 'NGN', 'GHS', 'ZAR'); foreach ($currencies as $currency) { $field_id = '_custom_price_' . strtolower($currency); if (isset($_POST[$field_id])) { update_post_meta($post_id, $field_id, sanitize_text_field($_POST[$field_id])); } } } add_action('woocommerce_process_product_meta', 'save_custom_currency_fields'); // Handle AJAX request to fetch product price based on selected currency function get_product_price_by_currency() { if (!isset($_POST['product_id']) || !isset($_POST['currency'])) { wp_send_json_error(array('message' => 'Invalid request.')); } // Get the product ID and selected currency $product_id = intval($_POST['product_id']); $currency = sanitize_text_field($_POST['currency']); // Ensure WooCommerce session is initialized if (!WC()->session) { WC()->initialize_session(); } // Define your currency symbols mapping (you can modify it) $currency_symbols = array( 'USD' => '$', 'NGN' => '₦', 'GHS' => '₵', 'ZAR' => 'R', ); // Get the custom price for the selected currency $custom_price = get_post_meta($product_id, '_custom_price_' . strtolower($currency), true); // If custom price is not set, use the regular price if (empty($custom_price)) { $product = wc_get_product($product_id); $custom_price = $product->get_price(); } // Get the correct currency symbol $currency_symbol = isset($currency_symbols[$currency]) ? $currency_symbols[$currency] : '$'; // Return the formatted price wp_send_json_success(array( 'price' => $currency_symbol . number_format($custom_price, 2), )); } add_action('wp_ajax_get_product_price', 'get_product_price_by_currency'); add_action('wp_ajax_nopriv_get_product_price', 'get_product_price_by_currency'); Hello world! – Joyful-cook Skip to main content
Uncategorized

Hello world!

By December 28, 2024One Comment

Welcome to WordPress. This is your first post. Edit or delete it, then start writing!

One Comment

Leave a Reply