根据 WooCommerce 结帐中的复选框添加或删除固定费用

Add or remove a fixed Fee based on a checkbox in WooCommerce checkout

使用 答案代码,我设法在复选框更改时添加了新费用:

add_action('woocommerce_after_checkout_billing_form', 'checkout_shipping_form_packing_addition', 20);
function checkout_shipping_form_packing_addition()
{
    $domain = 'woocommerce';

    echo '<tr class="packing-select"><th>' . __('EMBRULHO?', $domain) . '</th><td>';

    $chosen   = WC()->session->get('chosen_packing');

    // Add a custom checkbox field
    woocommerce_form_field('chosen_packing', array(
        'type'      => 'checkbox',
        'class'     => array('form-row-wide packing'),
        'label'         => __('', 'woocommerce'),
        'required'  => false,
    ), $chosen);

    echo '</td></tr>';
}

// jQuery - Ajax script
add_action('wp_footer', 'checkout_shipping_packing_script');
function checkout_shipping_packing_script()
{
    // Only checkout page
    if (is_checkout() && !is_wc_endpoint_url()) :

        WC()->session->__unset('chosen_packing');
?>
        <script type="text/javascript">
            jQuery(function($) {
                $('form.checkout').on('change', 'input#chosen_packing', function() {
                    var p = $(this).prop('checked');    
                    console.log(p);
                    $.ajax({
                        type: 'POST',
                        url: wc_checkout_params.ajax_url,
                        data: {
                            'action': 'woo_get_ajax_data',
                            'packing': p,
                        },
                        success: function(result) {
                            $('body').trigger('update_checkout');
                            console.log('response: ' + result); // just for testing | TO BE REMOVED
                        },
                        error: function(error) {
                            console.log(error); // just for testing | TO BE REMOVED
                        }
                    });
                });
            });
        </script>
<?php
    endif;
}

// Php Ajax (Receiving request and saving to WC session)
add_action('wp_ajax_woo_get_ajax_data', 'woo_get_ajax_data');
add_action('wp_ajax_nopriv_woo_get_ajax_data', 'woo_get_ajax_data');
function woo_get_ajax_data()
{
    if (isset($_POST['packing'])) {
        $packing = sanitize_key($_POST['packing']);
        WC()->session->set('chosen_packing', $packing);
        echo json_encode($packing);
    }
    die(); // Alway at the end (to avoid server error 500)
}

// Add a custom dynamic packaging fee
add_action('woocommerce_cart_calculate_fees', 'add_packaging_fee', 20, 1);
function add_packaging_fee($cart)
{
    if (is_admin() && !defined('DOING_AJAX'))
        return;

    $domain      = "woocommerce";

    if(WC()->session->get('chosen_packing')){
        $label = __('Embrulho', $domain);
        $cost = 5;
    }
    if (isset($cost))
        $cart->add_fee($label, $cost);
}

未选中时如何取消费用?

这是一个礼品包装选项,如果选择,将在结帐时向总费用中添加额外费用。如果未选中,则应删除费用并更新结帐。

您只需对代码进行一些小改动,例如:

add_action('woocommerce_after_checkout_billing_form', 'checkout_shipping_form_packing_addition', 20);
function checkout_shipping_form_packing_addition()
{
    $domain = 'woocommerce';

    echo '<tr class="packing-select"><th>' . __('EMBRULHO?', $domain) . '</th><td>';

    $chosen   = WC()->session->get('chosen_packing');

    // Add a custom checkbox field
    woocommerce_form_field('chosen_packing', array(
        'type'      => 'checkbox',
        'class'     => array('form-row-wide packing'),
        'label'         => __('', 'woocommerce'),
        'required'  => false,
    ), $chosen);

    echo '</td></tr>';
}

// jQuery - Ajax script
add_action('wp_footer', 'checkout_shipping_packing_script');
function checkout_shipping_packing_script()
{
    // Only checkout page
    if (is_checkout() && !is_wc_endpoint_url()) :
    ?>
    <script type="text/javascript">
        jQuery(function($) {
            $('form.checkout').on('change', 'input#chosen_packing', function() {
                $.ajax({
                    type: 'POST',
                    url: wc_checkout_params.ajax_url,
                    data: {
                        'action': 'woo_get_ajax_data',
                        'packing': $(this).prop('checked') ? 1 : 0,
                    },
                    success: function(result) {
                        $('body').trigger('update_checkout');
                        console.log('response: ' + result); // just for testing | TO BE REMOVED
                    },
                    error: function(error) {
                        console.log(error); // just for testing | TO BE REMOVED
                    }
                });
            });
        });
    </script>
    <?php
    else:
    WC()->session->__unset('chosen_packing');
    endif;
}

// Php Ajax (Receiving request and saving to WC session)
add_action('wp_ajax_woo_get_ajax_data', 'woo_get_ajax_data');
add_action('wp_ajax_nopriv_woo_get_ajax_data', 'woo_get_ajax_data');
function woo_get_ajax_data()
{
    if (isset($_POST['packing'])) {
        $packing = sanitize_key($_POST['packing']);
        WC()->session->set('chosen_packing', $packing);
        echo json_encode($packing);
    }
    die(); // Alway at the end (to avoid server error 500)
}

// Add a custom dynamic packaging fee
add_action('woocommerce_cart_calculate_fees', 'add_packaging_fee', 20, 1);
function add_packaging_fee($cart)
{
    if (is_admin() && !defined('DOING_AJAX'))
        return;

    if( WC()->session->get('chosen_packing') ){
        $label = __('Embrulho', 'woocommerce');
        $cost = 5;
        $cart->add_fee($label, $cost);
    }
}

代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并有效。