在 WooCommerce Checkout 中为特定类别启用百分比折扣的复选框
Checkbox enabling a percentage discount in WooCommerce Checkout for specific category
以下修改后的代码基于Add a checkout checkbox field that enable a percentage fee in Woocommerce。我已经适应但希望只针对特定类别,因此当学生在结帐时勾选方框,定义他们是学生时,它将从他们的总数中扣除 15%,但仅限于特定类别范围 - 即 'online seminars'。这超出了我的范围,所以希望得到一些帮助。
// 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 文件。已测试并有效。
以下修改后的代码基于Add a checkout checkbox field that enable a percentage fee in Woocommerce。我已经适应但希望只针对特定类别,因此当学生在结帐时勾选方框,定义他们是学生时,它将从他们的总数中扣除 15%,但仅限于特定类别范围 - 即 'online seminars'。这超出了我的范围,所以希望得到一些帮助。
// 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 文件。已测试并有效。