Skip to content

Instantly share code, notes, and snippets.

@xlplugins
Created September 13, 2024 12:43
Show Gist options
  • Save xlplugins/b9788e545b2c3276a6ad172509aa2d1a to your computer and use it in GitHub Desktop.
Save xlplugins/b9788e545b2c3276a6ad172509aa2d1a to your computer and use it in GitHub Desktop.
Funnelkit Checkout: Add custom fee based on the checkbox selected
class Add_Custom_Fee_On_Change_Checkbox {
private $field_lable = 'Shipping Way Fee';
private $checkout_fields = [
'shipping_way_0' => [
'label' => 'Fast shipping',
'price' => 10
],
'shipping_way_1' => [
'label' => 'Deliver in evening',
'price' => 8
],
'shipping_way_2' => [
'label' => 'Call before delivery',
'price' => 1
],
];
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 < 3; $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