Skip to content

Instantly share code, notes, and snippets.

@xlplugins
Created July 2, 2026 10:26
Show Gist options
  • Select an option

  • Save xlplugins/e894bfc41f051afaddb086d099b9501d to your computer and use it in GitHub Desktop.

Select an option

Save xlplugins/e894bfc41f051afaddb086d099b9501d to your computer and use it in GitHub Desktop.
Dropdown field with pricing
class Add_Custom_Fee_On_Change_Radio{
private $radio_fields = [
'vin_choice' => [ //field ID need to create
'confirm-part-fitment-using-my-vin' => [ // value of the option
'label' => 'Confirm part fitment using my VIN', //frontend value of the option
'price' => 10
],
'i-do-not-wish-to-provide-my-vin' => [
'label' => 'I do not wish to provide my VIN',
'price' => 20
],
'vin-s-not-apply-to-my-products' => [
'label' => 'VIN s not apply to my products',
'price' => 30
]
]
];
public function __construct(){
add_action( 'wfacp_after_checkout_page_found', [ $this, 'wfacp_add_script' ] );
add_action( 'woocommerce_cart_calculate_fees', [$this,'custom_fee_based_on_cart_total'], 10, 1 );
}
function wfacp_add_script() {
add_action( 'wp_footer', [ $this, 'wfacp_add_fee_script' ] );
}
function wfacp_add_fee_script(){
$field_name=$this->radio_fields;
$fields = json_encode( $this->radio_fields );
?>
<script>
jQuery(document).ready( function($) {
var fields=<?php echo $fields;?>;
$.each(fields, function (field, values) {
$(document.body).on('change','select', function(){
$('body').trigger('update_checkout');
});
});
});
</script>
<?php
}
function custom_fee_based_on_cart_total($cart){
if ( is_ajax() && !empty( $_POST['post_data'] ) ) {
parse_str( $_POST['post_data'], $post_data );
}else {
$post_data = $_POST;
}
foreach($this->radio_fields as $field => $value){
if(!empty($post_data[$field])){
$fee_name=$post_data[$field];
$option_keys=array_keys($value);
if(in_array($fee_name, $option_keys) && !empty($value[$fee_name]['price']) && is_numeric($value[$fee_name]['price'])){
$amount=$value[$fee_name]['price'];
$fee_label=$value[$fee_name]['label'];
$cart->add_fee( $fee_label, $amount, false );
}
}
}
}
}
new Add_Custom_Fee_On_Change_Radio();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment