Created
July 2, 2026 10:20
-
-
Save xlplugins/bd30e268b852d6a8a43eba4a2c5b1301 to your computer and use it in GitHub Desktop.
Add price with a custom checkbox
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
| class Add_Custom_Fee_On_Change_Checkbox { | |
| private $field_lable = 'Shipping Way Fee'; | |
| private $checkout_fields = [ | |
| 'shipping_way_0' => [ | |
| 'label' => 'Your label here', | |
| 'price' => 10 | |
| ], | |
| ]; | |
| public function __construct() { | |
| add_filter( 'wfacp_advanced_fields', [ $this, 'add_field' ], 20 ); | |
| add_filter( 'wfacp_html_fields_wfacp_shipping_option_fee', '__return_false' ); | |
| add_action( 'process_wfacp_html', [ $this, 'display_field' ], 10, 3 ); | |
| add_action( 'woocommerce_cart_calculate_fees', [ $this, 'custom_fee_based_on_cart_total' ], 10, 1 ); | |
| add_action( 'wfacp_internal_css', [ $this, 'wfacp_add_fee_script' ] ); | |
| } | |
| public function add_field() { | |
| $fields['wfacp_shipping_option_fee'] = [ | |
| 'type' => 'wfacp_html', | |
| 'class' => [ 'wfacp-col-full', 'wfacp-form-control-wrapper', 'wfacp_shipping_option_fee' ], | |
| 'id' => 'wfacp_shipping_option_fee', | |
| 'field_type' => 'wfacp_shipping_option_fee', | |
| 'label' => __( 'Shipping Options', 'woofunnels-aero-checkout' ), | |
| ]; | |
| return $fields; | |
| } | |
| public function display_field( $field, $key ) { | |
| if ( empty( $key ) || 'wfacp_shipping_option_fee' !== $key ) { | |
| return ''; | |
| } | |
| $args = array( | |
| 'id' => 'shipping_way', | |
| 'label' => $this->field_lable, | |
| 'data_label' => $this->field_lable, | |
| 'placeholder' => '', | |
| 'cssready' => [ | |
| 'wfacp-col-full ' | |
| ], | |
| 'type' => 'checkbox', | |
| 'required' => false, | |
| 'options' => [], | |
| 'default' => '', | |
| 'show_custom_field_at_thankyou' => false, | |
| 'show_custom_field_at_email' => false, | |
| 'is_wfacp_field' => true, | |
| 'field_type' => 'advanced', | |
| 'allow_delete' => true, | |
| 'class' => [ | |
| 'wfacp-form-control-wrapper', | |
| 'wfacp-col-full', | |
| 'wfacp_checkbox_field' | |
| ], | |
| 'input_class' => [ 'wfacp-form-control' ], | |
| 'label_class' => [], | |
| ); | |
| ?> | |
| <div class="wfacp_shipping_option_fee" id="wfacp_shipping_option_fee"> | |
| <?php | |
| for ( $i = 0; $i < 1; $i ++ ) { | |
| $args['id'] = 'shipping_way' . '_' . $i; | |
| $args['label'] = $this->checkout_fields[ 'shipping_way_' . $i ]['label']; | |
| $args['data_label'] = $this->checkout_fields[ 'shipping_way_' . $i ]['label']; | |
| woocommerce_form_field( $args['id'], $args ); | |
| } | |
| ?> | |
| </div> | |
| <?php | |
| } | |
| public function wfacp_add_fee_script() { | |
| $field_name = $this->checkout_fields; | |
| $fields = json_encode( $this->checkout_fields ); | |
| ?> | |
| <style> | |
| body #wfacp-e-form #wfacp_shipping_option_fee { | |
| clear: both; | |
| } | |
| body #wfacp-e-form #wfacp_shipping_option_fee * { | |
| line-height: 1.5 !important; | |
| } | |
| body #wfacp-e-form #wfacp_shipping_option_fee span.woocommerce-input-wrapper { | |
| display: block; | |
| } | |
| </style> | |
| <script> | |
| window.addEventListener('load', function () { | |
| (function ($) { | |
| var fields =<?php echo $fields;?>; | |
| $.each(fields, function (field, values) { | |
| $(document.body).on('change', 'input[type=checkbox][name=' + field + ']', function () { | |
| $('body').trigger('update_checkout'); | |
| }); | |
| }); | |
| })(jQuery); | |
| }); | |
| </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->checkout_fields as $field => $value ) { | |
| if ( isset( $post_data[ $field ] ) && ! empty( $post_data[ $field ] ) ) { | |
| $fee_name = $field; | |
| if ( ! empty( $value['price'] ) && is_numeric( $value['price'] ) ) { | |
| $amount = $value['price']; | |
| $fee_label = $value['label']; | |
| $cart->add_fee( $fee_label, $amount, false ); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| new Add_Custom_Fee_On_Change_Checkbox(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment