'aeta_sanitize_settings'
) );
}
/**
* Sanitize plugin settings before saving to database.
*/
function aeta_sanitize_settings( $input ) {
$new_input = array();
$new_input['is_enabled'] = isset( $input['is_enabled'] ) && $input['is_enabled'] === 'no' ? 'no' : 'yes';
$new_input['text'] = isset( $input['text'] ) ? sanitize_text_field( $input['text'] ) : 'I want to add tax';
$new_input['type'] = isset( $input['type'] ) && $input['type'] === 'fixed' ? 'fixed' : 'percentage';
$new_input['percentage_value'] = isset( $input['percentage_value'] ) ? floatval( $input['percentage_value'] ) : 0;
$new_input['fixed_value'] = isset( $input['fixed_value'] ) ? floatval( $input['fixed_value'] ) : 0;
$new_input['bg_color'] = isset( $input['bg_color'] ) ? sanitize_hex_color( $input['bg_color'] ) : '#f9f9f9';
$new_input['text_color'] = isset( $input['text_color'] ) ? sanitize_hex_color( $input['text_color'] ) : '#333333';
$new_input['enable_hover'] = isset( $input['enable_hover'] ) && $input['enable_hover'] === 'no' ? 'no' : 'yes';
$new_input['hover_bg_color'] = isset( $input['hover_bg_color'] ) ? sanitize_hex_color( $input['hover_bg_color'] ) : '#eeeeee';
$new_input['hover_text_color'] = isset( $input['hover_text_color'] ) ? sanitize_hex_color( $input['hover_text_color'] ) : '#000000';
return $new_input;
}
// 3. Admin Settings Page HTML
function aeta_settings_page() {
$settings = get_option( 'aeta_settings', array(
'is_enabled' => 'yes',
'text' => 'I want to add tax',
'type' => 'percentage',
'percentage_value' => '18',
'fixed_value' => '10',
'bg_color' => '#f9f9f9',
'text_color' => '#333333',
'hover_bg_color' => '#eeeeee',
'hover_text_color' => '#000000',
'enable_hover' => 'yes'
) );
$logo_url = plugins_url( 'logo.gif', __FILE__ );
?>
Adding Extra Tax or Amount Settings
Developed by: Mehmet Balcı
session->get('aeta_apply_tax') ); ?>>
session->has_session() ) {
WC()->session->set_customer_session_cookie( true );
}
WC()->session->set( 'aeta_apply_tax', $_POST['apply_tax'] == 1 );
// Ensure totals are recalculated for the response
WC()->cart->calculate_totals();
}
wp_send_json_success();
}
// 6. Fee Calculation
add_action( 'woocommerce_cart_calculate_fees', 'aeta_add_custom_tax_fee', 20, 1 );
function aeta_add_custom_tax_fee( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
$settings = get_option( 'aeta_settings' );
if ( ($settings['is_enabled'] ?? 'yes') !== 'yes' ) {
return;
}
if ( WC()->session && WC()->session->get( 'aeta_apply_tax' ) ) {
$type = !empty($settings['type']) ? $settings['type'] : 'percentage';
$label = !empty($settings['text']) ? $settings['text'] : 'Extra Fee';
if ( $type === 'percentage' ) {
$value = floatval( !empty($settings['percentage_value']) ? $settings['percentage_value'] : 0 );
$fee = ($cart->get_subtotal() * $value) / 100;
} else {
$value = floatval( !empty($settings['fixed_value']) ? $settings['fixed_value'] : 0 );
$fee = $value;
}
if ( $fee > 0 ) {
$cart->add_fee( $label, $fee, true );
}
}
}
// 7. Add Order Note when the option is selected
add_action( 'woocommerce_checkout_create_order', 'aeta_add_selection_to_order_note', 10, 2 );
function aeta_add_selection_to_order_note( $order, $data ) {
if ( WC()->session && WC()->session->get( 'aeta_apply_tax' ) ) {
$settings = get_option( 'aeta_settings' );
$label = !empty($settings['text']) ? $settings['text'] : 'Extra Tax/Amount';
// Add a private note to the order for the admin to see
$note = sprintf( __( 'Kullanıcı seçeneği işaretledi: %s', 'adding-extra-tax-or-amount' ), $label );
$order->add_order_note( $note );
}
}