php - 根据 WooCommerce 中的自定义结帐单选按钮和文本字段设置动态费用

标签 php jquery ajax session woocommerce

我正在尝试创建一个自定义结帐单选按钮,以百分比计算餐厅小费。
对于单选按钮,静态值工作正常。
但是,我想获得小计并计算自定义单选按钮点击的特定百分比。
这是我的代码

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/

    相关文章:

    php - MVC [Zend 框架] : where to apply filtering and validation

    javascript - 将 php 变量中的文本转换为 javascript 函数

    php - 在带有 PHP5.3.6 的 LAMP 上启用 MsSQL PDO 驱动程序

    php - Eloquent 模型如何避免模型臃肿?

    javascript - 如何从 XMLHttpRequest.responseText 解析 HTML 属性值?

    javascript - 两个具有相同操作的提交按钮,其中一个为同一表单添加了功能/操作

    php - AJAX POST 到 PHP mySQL 查询

    javascript - jquery ajax方法请求体为空但postman工作正常

    javascript - Ajax调用、函数回调、javascript

    javascript - 如何运行通过ajax进程包含的javascript代码