Created
December 13, 2023 13:35
-
-
Save braddalton/857b2fbfc02232bd1818b402428c4c40 to your computer and use it in GitHub Desktop.
Add Custom Settings Tab for Number Field to WooCommerce https://wpsites.net/?p=114654
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action('woocommerce_settings_tabs_exchange_rates', 'add_exchange_rate_usd_field'); | |
add_action('woocommerce_admin_field_number', 'add_exchange_rate_usd_field'); | |
// Add custom exchange rate field to WooCommerce settings under a new tab | |
function add_exchange_rate_usd_field() { | |
woocommerce_admin_fields(array( | |
array( | |
'type' => 'title', | |
'id' => 'exchange_rates_tab_title', | |
'title' => __('Exchange Rates', 'woocommerce'), | |
), | |
array( | |
'title' => __('Exchange Rate (USD)', 'woocommerce'), | |
'type' => 'number', | |
'id' => 'exchange_rate_usd', | |
'default' => '', | |
'desc_tip' => true, | |
), | |
array( | |
'type' => 'sectionend', | |
'id' => 'exchange_rates_tab_end', | |
), | |
)); | |
} | |
add_action('woocommerce_update_options', 'save_exchange_rate_usd_field'); // Correct hook for saving options | |
function save_exchange_rate_usd_field() { | |
if (isset($_POST['exchange_rate_usd'])) { | |
update_option('exchange_rate_usd', sanitize_text_field($_POST['exchange_rate_usd'])); | |
} | |
} | |
add_filter('woocommerce_settings_tabs_array', function($settings_tabs) { | |
$settings_tabs['exchange_rates'] = __('Exchange Rates', 'woocommerce'); | |
return $settings_tabs; | |
}, 50); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment