【问题标题】:Checkbox field that add / remove a custom fee in WooCommerce在 WooCommerce 中添加/删除自定义费用的复选框字段
【发布时间】:2019-09-18 20:06:00
【问题描述】:

需要根据表单复选框元素向购物车添加自定义费用。当前基于购物车中产品类别的操作效果很好,并增加了自定义送货费,但客户可以选择免费取货。选中复选框是否可以触发 WooCommerce 添加自定义费用操作?

使用“对 html 表单上的复选框选中或未选中事件执行操作”中的示例可以 .change(function) function delivery(id){ 如果(this.checked){ add_action 自定义购物车费用?

 add_action( 'woocommerce_cart_calculate_fees', 'add_a_custom_fee', 10, 1 );
function add_a_custom_fee( $cart ) {
        $amount = 25;
        $cart->add_fee( __('Custom fee'), $amount );
}

选中复选框时,预计自定义购物车费用将显示在购物车中。

【问题讨论】:

  • 嘿@tmprojects 你在找javascript代码吗?

标签: php jquery ajax woocommerce cart


【解决方案1】:

以下代码将在结帐页面上显示一个复选框字段,用于启用/禁用自定义费用:

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

    woocommerce_form_field( 'custom_fee', array(
        'type'  => 'checkbox',
        'label' => __(' Custom fee'),
        'class' => array( 'form-row-wide' ),
    ), '' );
}

// Remove "(optional)" label on 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( 'custom_fee' === $key && is_checkout() ) {
        $optional = '&nbsp;<span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';
        $field = str_replace( $optional, '', $field );
    }
    return $field;
}

// Ajax / jQuery script
add_action( 'wp_footer', 'custom_fee_script' );
function custom_fee_script() {
    // On checkoutpage
    if( ( is_checkout() && ! is_wc_endpoint_url() ) ) :
    ?>
    <script type="text/javascript">
    jQuery( function($){
        if (typeof woocommerce_params === 'undefined')
            return false;

        console.log('defined');

        $('input[name=custom_fee]').click( function(){
            var fee = $(this).prop('checked') === true ? '1' : '';

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

// Get the ajax request and set value to WC session
add_action( 'wp_ajax_custom_fee', 'get_ajax_custom_fee' );
add_action( 'wp_ajax_nopriv_custom_fee', 'get_ajax_custom_fee' );
function get_ajax_custom_fee() {
    if ( isset($_POST['custom_fee']) ) {
        WC()->session->set('custom_fee', ($_POST['custom_fee'] ? '1' : '0') );
        echo WC()->session->get('custom_fee');
    }
    die();
}

// Add / Remove a custom fee
add_action( 'woocommerce_cart_calculate_fees', 'add_remove_custom_fee', 10, 1 );
function add_remove_custom_fee( $cart ) {
    // Only on checkout
    if ( ( is_admin() && ! defined( 'DOING_AJAX' ) ) || is_cart() )
        return;

    $fee_amount = 25;

    if( WC()->session->get('custom_fee') )
        $cart->add_fee( __( 'Custom fee', 'woocommerce'), $fee_amount );
}

代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

【讨论】:

  • 这段代码在过去一个月里运行良好,但现在已经失去了识别“食品”类别项目的能力。仅当“食品”类别的商品在购物车中时才会出现交货复选框,但现在无论如何都会出现。仍然很棒的代码只是好奇为什么该功能会禁用。
  • @tmprojects 此代码在上一个 WooCommerce 3.6.4 上仍然可以完美运行……此线程中没有与“食品”类别相关的内容,所以您可能会感到困惑。
  • @stemon 抱歉,这行得通!刚刚在店面主题下的最后一个 WC/WP 版本上进行了测试。
  • 这是一段很棒的代码,效果很好。
  • 我正在使用此代码并且效果很好,但是我发现如果您检查该字段(因此它会增加费用),然后您在页面上进行刷新,重新加载时似乎保留字段上的选中值(即使它在视觉上显示为未选中)并且费用保持不变。该值需要在刷新时重置,以便在重新加载时移除费用。希望这是有道理的。
猜你喜欢
  • 2018-06-17
  • 2015-06-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-09
  • 2021-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多