我正在尝试创建一个自定义结帐单选按钮,以百分比计算餐厅小费。
对于单选按钮,静态值工作正常。
但是,我想获得小计并计算自定义单选按钮点击的特定百分比。
这是我的代码
add_action( 'woocommerce_after_checkout_billing_form', 'add_box_option_to_checkout' );
function add_box_option_to_checkout( $checkout ) {
$chosen = WC()->session->get( 'tip' );
$chosen = empty( $chosen ) ? WC()->checkout->get_value( 'tip' ) : $chosen;
$chosen = empty( $chosen ) ? '0' : $chosen;
$total = WC()->cart->get_subtotal();
$fivetip = $total * 0.05;
woocommerce_form_field( 'tip', array(
'type' => 'radio',
'class' => array( 'form-row-wide', 'update_totals_on_change' ),
'options' => array(
$fivetip => '5%',
'10.00' => '10%',
'15.00' => '15%',
),
), $chosen );
woocommerce_form_field( 'add_tip', array(
'type' => 'text',
'class' => array('add_tipform-row-wide'),
'placeholder' => __('Enter Custom Tip Amount')
), $checkout->get_value( 'add_tip' ));
}
add_action( 'woocommerce_cart_calculate_fees', 'checkout_tip_fee', 20, 1 );
function checkout_tip_fee( $cart ) {
if ( $radio = WC()->session->get( 'tip' ) ) {
$cart->add_fee( 'Tip', $radio );
}
}
add_action( 'woocommerce_checkout_update_order_review', 'checkout_tip_choice_to_session' );
function checkout_tip_choice_to_session( $posted_data ) {
parse_str( $posted_data, $output );
if ( isset( $output['tip'] ) ){
WC()->session->set( 'tip', $output['tip'] );
}
}
最佳答案
你可以这样得到它:
所以:
// add custom fields to checkout
add_action( 'woocommerce_after_checkout_billing_form', 'add_box_option_to_checkout' );
function add_box_option_to_checkout( $checkout ) {
woocommerce_form_field( 'tip', array(
'type' => 'radio',
'class' => array( 'form-row-wide', 'update_totals_on_change' ),
'options' => array(
'5' => '5%',
'10' => '10%',
'15' => '15%',
),
));
woocommerce_form_field( 'add_tip', array(
'type' => 'text',
'class' => array('add_tipform-row-wide', 'update_totals_on_change'),
'placeholder' => __('Enter Custom Tip Amount')
));
}
现在,jQuery脚本将在选择单选按钮时清空文本字段,反之亦然:// uncheck radio buttons when text field is changed
add_action( 'wp_footer', 'uncheck_radio_buttons_when_text_field_is_changed' );
function uncheck_radio_buttons_when_text_field_is_changed() {
if ( is_checkout() && ! is_wc_endpoint_url() ) {
?>
<script type="text/javascript">
// deselect radio buttons when an amount is entered in the text field
jQuery('#add_tip').keyup(function(){
jQuery('#tip_field input[name=tip]').prop('checked', false);
});
// clears the text field when a percentage is selected
jQuery('input[name=tip]').change(function(){
jQuery('#add_tip').val('');
});
</script>
<?php
}
}
最后,它根据选定的值 计算费用金额。按小计计算 (不含税)的购物车。Make sure you use the same name for the fee to add to the cart in order to overwrite the same and display only one.
// adds a custom fee based on the field valued
add_action( 'woocommerce_cart_calculate_fees', 'add_custom_fee', 10, 1 );
function add_custom_fee( $cart ) {
// get the subtotal (excluding taxes)
$subtotal = WC()->cart->subtotal_ex_tax;
if ( ! $_POST || is_admin() || ! is_ajax() ) {
return;
}
if ( isset( $_POST['post_data'] ) ) {
parse_str( $_POST['post_data'], $post_data );
} else {
$post_data = $_POST;
}
// adds the fee based on the checked percentage
if ( isset( $post_data['tip'] ) ) {
$percentage = $post_data['tip'];
$fee = $subtotal * $percentage / 100;
if ( $fee > 0 ) {
WC()->cart->add_fee( 'Tip', $fee, true );
}
}
// adds the fee based on the amount entered
if ( isset( $post_data['add_tip'] ) ) {
$fee = (float) str_replace( ',', '.', $post_data['add_tip'] );
if ( $fee > 0 ) {
WC()->cart->add_fee( 'Tip', $fee, true );
}
}
}
该代码已经过测试并且可以正常工作。将它添加到您的事件主题的functions.php。
关于php - 根据 WooCommerce 中的自定义结帐单选按钮和文本字段设置动态费用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66176490/