在WooCommerce Checkout页面中,我添加了一个下拉自定义域,其中有许多"Card type"选项.有些是免费的,有些是0.99美元(费用).在 checkout 页面上计算的费用是可以的,但当下订单时,费用不会添加到订单中(对于商业"卡类型"选项).

费用代码:

add_action('woocommerce_cart_calculate_fees', 'add_card_fee');

function add_card_fee() {
    if (is_admin() && !defined('DOING_AJAX')) {
        return; // Do not add the fee when in the admin area
    }
    if (is_checkout()) {
        if (isset($_POST['post_data']) && !(strpos($_POST['post_data'], 'Bestwishes') !== false || strpos($_POST['post_data'], 'nocard') !== false || strpos($_POST['post_data'], 'happybirthday') !== false)) {
            $extra_charge = 0.99; 
            WC()->cart->add_fee(__('Greeting Card Fee', 'woocommerce'), $extra_charge);
        } else {
            return;
        }
    }
}

下拉自定义域的代码(在签出中):

function add_custom_field_to_checkout($fields) {
    $fields['shipping']['card_type'] = array(
        'type' => 'select',
        'label' => __('Greeting Card', 'woocommerce'),
        'options' => array(
            '' => __('Select an option', 'woocommerce'),
            'nocard' => __('No Card', 'woocommerce'),
            'Bestwishes' => __('Best Wishes (Free)', 'woocommerce'),
            'congratulations' => __('Congratulations ($0.99)', 'woocommerce'),
            'happybirthday' => __('Happy birthday (Free)', 'woocommerce'),
            'happyanniversary' => __('Happy Anniversary ($0.99)', 'woocommerce'),
        ),
        'required' => true,
        'class' => array('form-row-wide'),
        'clear' => true
    );

    return $fields;
}

在 checkout 页面上,一切看起来都很好,比如在订单总额中添加费用,以及从订单总额中删除费用.

推荐答案

您的代码中有一些错误,它无法按原样工作.它需要AJAX和一个WooCommerce会话变量.

以下是重新访问的完整代码(remove before all your existing related code):

// Displaying a checkout "select" custom field
add_action('woocommerce_checkout_fields', 'add_custom_select_field_to_checkout');
function add_custom_select_field_to_checkout($fields) {
    $fields['shipping']['card_type'] = array(
        'type' => 'select',
        'label' => __('Greeting Card', 'woocommerce'),
        'options' => array(
            '' => __('Select an option', 'woocommerce'),
            'nocard' => __('No Card', 'woocommerce'),
            'bestwishes' => __('Best Wishes (Free)', 'woocommerce'),
            'congratulations' => __('Congratulations ($0.99)', 'woocommerce'),
            'happybirthday' => __('Happy birthday ($0.99)', 'woocommerce'),
            'happyanniversary' => __('Happy Anniversary ($0.99)', 'woocommerce'),
        ),
        'required' => true,
        'class' => array('form-row-wide'),
        'clear' => true
    );

    return $fields;
}

// jQuery - Ajax script
add_filter( 'wp_footer' , 'checkout_delivery_script' );
function checkout_delivery_script() {
    // Only on checkout page
    if( ! ( is_checkout() && ! is_wc_endpoint_url() ) ) return;
    
    WC()->session->__unset('selected_card_type'); // Reset 'selected_card_type' session variable
    
    // Jquery / Ajax
    wc_enqueue_js( "jQuery(function($){
        if (typeof wc_checkout_params === 'undefined')
            return false;
        
        // Ajax 
        $('form.checkout').on('change', 'select#card_type', function(){
            var selected = $(this).val();
                console.log(selected); // just for testing | TO BE REMOVED

            $.ajax({
                type: 'POST',
                url: wc_checkout_params.ajax_url,
                data: {
                    'action':             'selected_card_type',
                    'selected_card_type': selected,
                },
                success: function (result) {
                    $('body').trigger('update_checkout');
                    console.log(result); // just for testing | TO BE REMOVED
                }
            });
        });
    });" );
}

// Get Ajax request and saving to WC session
add_action( 'wp_ajax_selected_card_type', 'wc_get_selected_card_type_ajax_data' );
add_action( 'wp_ajax_nopriv_selected_card_type', 'wc_get_selected_card_type_ajax_data' );
function wc_get_selected_card_type_ajax_data() {
    if ( isset($_POST['selected_card_type']) ){
        $selected_card_type = esc_attr($_POST['selected_card_type']);
        
        // Updating 'selected_card_type' WooCommerce Session variable
        WC()->session->set('selected_card_type', $selected_card_type );
        
        echo $selected_card_type; // Return the value to jQuery
    }
    die(); // Always Exit silently
}

// Add a Fee conditionally in checkout
add_action('woocommerce_cart_calculate_fees', 'add_selected_card_type_fee');
function add_selected_card_type_fee() {
    if ( is_admin() && !defined('DOING_AJAX') ) {
        return; // Do not add the fee when in the admin area
    }
    
    // Only on checkout page
    if( ! ( is_checkout() && ! is_wc_endpoint_url() ) ) {
        return;
    }
        
    // Get 'selected_card_type' WooCommerce Session variable
    $selected_card_type = WC()->session->get('selected_card_type' );
    
    if (isset($selected_card_type) && ! in_array( $selected_card_type, array('bestwishes', 'nocard') ) ) {
        $extra_charge = 0.99; 
        WC()->cart->add_fee( __('Greeting Card Fee', 'woocommerce'), $extra_charge );
    }
}

代码放在活动子主题(或活动主题)的函数.php文件中.经过测试,效果良好.

Php相关问答推荐

产品ACF值在WooCommerce我的账户订单的附加列中为空

在函数内部获取一致的单选按钮值以更新WordPress用户数据

多行PHP属性声明中是否允许单行注释

闭包绑定$This和垃圾收集器

使注册字段UserMeta值对于WooCommerce中的每个用户是唯一的

FlySystem v3:使用本地适配器时出现问题

移动应用:在PHP中忽略Cookie?

在WooCommerce Admin产品列表自定义列中显示来自元数据的统计数据

$this->;db->;update();CodIgnitor中的功能不工作

如何在 apache Laragon - laravel 10 中允许方法 DELETE?

在具有Angular 的 Laravel 应用程序中,不显示图像

将可编辑的计费自定义字段添加到 WooCommerce 管理单个订单

服务器升级到新的mysql版本8.0.34后查询错误

后退按钮不起作用 laravel ajax crud

Laravel 路由参数中的 ":" 是什么?

PHP 合并/合并多维数组

Eloquent 的模型更新了它应该 laravel php 的更多行

为什么 PDO 允许使用带命名占位符的索引数组,但仅在禁用仿真时才允许?

将参数传递给 laravel 9 工厂

如何将文件直接上传到 Cloudinary 而不先保存在 public 文件夹中