问题描述
以下修改后的代码基于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 = ' <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' );
解决方法
更新3
使用以下重新访问的代码以及其他自定义条件功能,该功能可以检查商品是否仅来自特定产品类别,并且还可以给出来自特定商品类别的商品的非折扣小计
您将必须在该函数中设置数组中正确的产品类别,并且可以使用术语名称,段或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 = ' <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文件中。经过测试,可以正常工作。