添加一个结帐复选框字段,在Woocommerce中启用百分比费用

问题描述 投票:0回答:1

在Woocommerce结帐页面中,我试图在结帐页面添加一个复选框并将其命名为“分期付款”。

选中复选框以添加总金额的3%。

例如,如果总金额是100$,它将在总金额之前添加额外的部分,并将其称为分期付款(3% = $3)并将该金额添加到总金额中。

php jquery ajax woocommerce checkout
1个回答
1
投票

基于“Dynamic shipping fee based on custom radio buttons in Woocommerce”,这是一个轻量级版本,在结算字段后面有一个复选框,当选中复选框时,将支付3%的费用:

// 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) );
}

代码位于活动子主题(或活动主题)的function.php文件中。经过测试和工作。


结算结帐字段后显示的字段:

enter image description here


仅在选中复选框时才会激活百分比“分期付款”费用:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.