Add a checkout checkbox field that enable a percen

2020-02-16 02:10发布

问题:

In Woocommerce checkout page, I'am trying to add a check box on checkout page and call it "Installment".

When checkbox is ticked to add 3 percent total amount.

For example, if the total amount is 100$ it will add an extra section before the total amount and call it Installment fee (3% = $3) and add that amount to the total.

回答1:

Based on "Dynamic shipping fee based on custom radio buttons in Woocommerce", Here it's a light changed version, with a checkbox located after billing fields, that will enable a 3% fee when checkbox is checked:

// Add a custom checkbox fields after billing fields
add_action( 'woocommerce_after_checkout_billing_form', 'add_custom_checkout_checkbox', 20 );
function add_custom_checkout_checkbox(){

    // Add a custom checkbox field
    woocommerce_form_field( 'installment_fee', array(
        'type'  => 'checkbox',
        'label' => __(' Installment 3% fee'),
        'class' => array( 'form-row-wide' ),
    ), '' );
}

// Remove "(optional)" label on "Installement checkbox" field
add_filter( 'woocommerce_form_field' , 'remove_order_comments_optional_fields_label', 10, 4 );
function remove_order_comments_optional_fields_label( $field, $key, $args, $value ) {
    // Only on checkout page for Order notes field
    if( 'installment_fee' === $key && is_checkout() ) {
        $optional = '&nbsp;<span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';
        $field = str_replace( $optional, '', $field );
    }
    return $field;
}

// jQuery - Ajax script
add_action( 'wp_footer', 'checkout_fee_script' );
function checkout_fee_script() {
    // Only on Checkout
    if( is_checkout() && ! is_wc_endpoint_url() ) :

    if( WC()->session->__isset('enable_fee') )
        WC()->session->__unset('enable_fee')
    ?>
    <script type="text/javascript">
    jQuery( function($){
        if (typeof wc_checkout_params === 'undefined') 
            return false;

        $('form.checkout').on('change', 'input[name=installment_fee]', function(e){
            var fee = $(this).prop('checked') === true ? '1' : '';

            $.ajax({
                type: 'POST',
                url: wc_checkout_params.ajax_url,
                data: {
                    'action': 'enable_fee',
                    'enable_fee': fee,
                },
                success: function (result) {
                    $('body').trigger('update_checkout');
                },
            });
        });
    });
    </script>
    <?php
    endif;
}

// Get Ajax request and saving to WC session
add_action( 'wp_ajax_enable_fee', 'get_enable_fee' );
add_action( 'wp_ajax_nopriv_enable_fee', 'get_enable_fee' );
function get_enable_fee() {
    if ( isset($_POST['enable_fee']) ) {
        WC()->session->set('enable_fee', ($_POST['enable_fee'] ? true : false) );
    }
    die();
}

// Add a custom dynamic 3% fee
add_action( 'woocommerce_cart_calculate_fees', 'custom_percetage_fee', 20, 1 );
function custom_percetage_fee( $cart ) {
    // Only on checkout
    if ( ( is_admin() && ! defined( 'DOING_AJAX' ) ) || ! is_checkout() )
        return;

    $percent = 3;

    if( WC()->session->get('enable_fee') )
        $cart->add_fee( __( 'Installment fee', 'woocommerce')." ($percent%)", ($cart->get_subtotal() * $percent / 100) );
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.


The field displayed after billing checkout fields:


The Percentage "Installment" fee activated only when checkbox is checked: