jquery 在WooCommerce Checkout中为特定类别启用百分比折扣的复选框

ekqde3dh  于 2023-08-04  发布在  jQuery
关注(0)|答案(1)|浏览(98)

下面的修改代码基于Add a checkout checkbox field that enable a percentage fee in Woocommerce。我已经适应了,但希望只针对一个特定的类别,所以当一个学生在结账时打勾,定义他们是一个学生,它将扣除15%的总额,但只针对一个特定的类别范围-即“在线研讨会”。这超出了我的能力范围,所以我很感激一些帮助。

// 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( 'student_discount_fee', array(
        'type'  => 'checkbox',
        'label' => __(' Yes, I am a student lawyer studying law'),
        'class' => array( 'form-row-wide' ),
    ), '' );
}

// Remove "(optional)" label on "Student discount 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( 'student_discount_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=student_discount_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_percentage_fee', 20, 1 );
function custom_percentage_fee( $cart ) {
    // Only on checkout
    if ( ( is_admin() && ! defined( 'DOING_AJAX' ) ) || ! is_checkout() )
        return;

    $percent = -33.33333333333333;

    if( WC()->session->get('enable_fee') )
        $cart->add_fee( __( 'STUDENT LAWYER DISCOUNT', 'woocommerce')." ", ($cart->get_subtotal() * $percent / 100) );
}
// hide coupon field on cart page
function hide_coupon_field_on_cart( $enabled ) {

    if ( is_cart() ) {
        $enabled = false;
    }

    return $enabled;
}
add_filter( 'woocommerce_coupons_enabled', 'hide_coupon_field_on_cart' );

// hide coupon field on checkout page
function hide_coupon_field_on_checkout( $enabled ) {

    if ( is_checkout() ) {
        $enabled = false;
    }

    return $enabled;
}
add_filter( 'woocommerce_coupons_enabled', 'hide_coupon_field_on_checkout' );

字符串

dgtucam1

dgtucam11#

更新3

使用以下重新访问的代码和一个额外的自定义条件函数,该函数检查项目是否仅来自特定产品类别,并且还可以为特定产品类别的项目给予非折扣小计。
您必须在该功能中设置数组中正确的产品类别,您可以使用术语名称,slug或Id。
小计现在只计算您特定产品类别中的折扣项目,并且它是非折扣小计,因此您无需再隐藏优惠券字段
当至少有特定产品类别中的项目时,将显示该复选框。

// Custom conditional function that check for specific product categories (and calculate their subtotal)
function check_cart_items_for_specific_categories( $type = 'boolean') {
    $categories     = array('online-seminars'); //  <===  Here define your product category (name, slug or Id)
    $category_found = false; // Initializing
    $item_subtotal  = 0; // Initializing

    foreach( WC()->cart->get_cart() as $item ) {
        if ( has_term( $categories, 'product_cat', $item['product_id'] ) ) {
            $category_found = true;
            $item_subtotal += $item['line_total'];
        }
    }

    if ( $type === 'subtotal' ) {
        return $item_subtotal;
    } else {
        return $category_found;
    }
}

// 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(){
    if( ! check_cart_items_for_specific_categories() ) return; // Exit

    // Add a custom checkbox field
    woocommerce_form_field( 'student_discount_fee', array(
        'type'  => 'checkbox',
        'label' => __(' Yes, I am a student lawyer studying law'),
        'class' => array( 'form-row-wide' ),
    ), '' );
}

// Remove "(optional)" label on "Student discount 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( 'student_discount_fee' === $key && is_checkout() && check_cart_items_for_specific_categories() ) {
        $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() && check_cart_items_for_specific_categories() ) :

    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=student_discount_fee]', function() {
            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_percentage_fee', 20, 1 );
function custom_percentage_fee( $cart ) {
    // Only on checkout
    if ( ( is_admin() && ! defined( 'DOING_AJAX' ) ) || ! is_checkout() )
        return;

    if( WC()->session->get('enable_fee') && check_cart_items_for_specific_categories() ) {
        $percentage = -33.33333333333333; // Set the percentage discount (negative float number)
        $subtotal   = check_cart_items_for_specific_categories('subtotal'); // Related items subtotal
        $discount   = $subtotal * $percentage / 100;

        $cart->add_fee( strtoupper( __( 'Student lawyer discount', 'woocommerce') ), $discount );
    }
}

字符串
代码放在活动子主题(或活动主题)的functions.php文件中。经过测试,工作正常。

相关问题