0
votes

Based on Add a checkout checkbox field that enable a percentage fee in Woocommerce answer code I created a checkbox on the checkout page.

When it is checked, it applies a 15% freight forwarding fee.

// Add a custom checkbox fields before order notes
add_action( 'woocommerce_before_order_notes', 'add_custom_checkout_checkbox', 20 );
function add_custom_checkout_checkbox(){

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


// 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=forwarding_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 15% 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 = 15;

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

Currently, this fee is calculated from the subtotal and added up to the order total value.

I need a solution where this fee is calculated from a sum of subtotal + shipping AND IS NOT added to the order total value.

I will rename "fee" to "deposit".


Please see a screenshot:

enter image description here

1
So its a fee that you dont pay ? Since its not in the total ? - Martin Mirchev
Yes. I will rename it to "deposit". - Rudolfs

1 Answers

0
votes

Since you don't want to add it to the total, you can add a custom table row to the woocommerce-checkout-review-order-table instead of a cart fee.

Which then show/hide the percentage based on if the checkbox is checked.

Explanation via one-line comments, added to my answer.

So you get:

function action_woocommerce_before_order_notes( $checkout ) {
    // Add field
    woocommerce_form_field( 'my_id', array(
        'type'          => 'checkbox',
        'class'         => array( 'form-row-wide' ),
        'label'         => __( '15% and some other text', 'woocommerce' ),
        'required'      => false,  
    ), $checkout->get_value( 'my_id' ));
}
add_action( 'woocommerce_before_order_notes', 'action_woocommerce_before_order_notes', 10, 1 );
 
function action_woocommerce_before_order_total() {
    // Initialize
    $percent = 15;
    
    // Get subtotal & shipping total
    $subtotal       = WC()->cart->subtotal;
    $shipping_total = WC()->cart->get_shipping_total();
    
    // Total
    $total = $subtotal + $shipping_total;
    
    // Result
    $result = ( $total / 100 ) * $percent;
    
    // The Output
    echo '<tr class="my-class">
        <th>' . __( 'My text', 'woocommerce' ) . '</th>
        <td data-title="My text">' . wc_price( $result ) . '</td>
    </tr>';
}
add_action( 'woocommerce_review_order_before_order_total', 'action_woocommerce_before_order_total', 10, 0 );
    
// jQuery
function action_wp_footer() {
    // Only on checkout
    if ( is_checkout() && ! is_wc_endpoint_url() ) :
    ?>
    <script type="text/javascript">
    jQuery( function($){
        // Selector
        var my_input = 'input[name=my_id]';
        var my_class = '.my-class';
        
        // Show or hide
        function show_or_hide() {
            if ( $( my_input ).is(':checked') ) {
                return $( my_class ).show();
            } else {
                return $( my_class ).hide();               
            }           
        }
        
        // Default
        $( document ).ajaxComplete(function() {
            show_or_hide();
        });
        
        // On change
        $( 'form.checkout' ).change(function() {
            show_or_hide();
        });
    });
    </script>
    <?php
    endif;
}
add_action( 'wp_footer', 'action_wp_footer', 10, 0 );