添加一个结帐复选框字段,以在 Woocommerce 中启用百分比费用 [英] Add a checkout checkbox field that enable a percentage fee in Woocommerce

查看:21
本文介绍了添加一个结帐复选框字段,以在 Woocommerce 中启用百分比费用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Woocommerce 结帐页面中,我尝试在结帐页面上添加一个复选框并将其称为分期付款".

勾选复选框以添加3%总金额.

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

解决方案

基于"

<小时>

仅在选中复选框时激活的分期付款"百分比费用:

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.

解决方案

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:

这篇关于添加一个结帐复选框字段,以在 Woocommerce 中启用百分比费用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆