ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

php – WooCommerce在结帐页面上使用ajax添加自定义费用

2019-07-08 09:29:58  阅读:150  来源: 互联网

标签:wordpress php ajax woocommerce woothemes


我正在尝试使用当用户更改送货地址选择下拉菜单时,它使用ajax动态地向购物车总额添加费用.我可以获得值但是当选择另一个状态时它不会更新总数.

我的ajax请求:

jQuery(document).ready(function () {
    jQuery('#shipping_state').change(function () {
        var data = {
            action: 'woocommerce_custom_fee',
            security: wc_checkout_params.update_order_review_nonce,
            add_order_fee: 'state',
            post_data: jQuery('form.checkout').serialize()
        };
        jQuery.ajax({
            type: 'POST',
            url: wc_checkout_params.ajax_url,
            data: data,
            success: function (code) {
                var result = '';
                result = jQuery.parseJSON(code);
                if (result.result === 'success') {

                    jQuery('body').trigger('update_checkout');
                }
            },
            dataType: 'html'
        });
        return false;
    });
})

并在functions.php中

add_action('woocommerce_cart_calculate_fees', 'woo_add_cart_fee');

function woo_add_cart_fee() {
global $woocommerce;
$destsuburb = $woocommerce->customer->get_shipping_state();

/*Then I use $destsuburb as a variable to API and get a shipping cost returning $shipping_cost*/

$woocommerce->cart->add_fee('Shipping and Handling:', $shipping_cost);
}

我根据州获得不同的运费,但它没有通过add_fee()改变前端值

解决方法:

最后我发现了一个使用会话变量来存储Ajax值和add_fee()的解决方案

我的ajax请求:

jQuery(document).ready(function () {

    jQuery('#State').click(function () {
        if (jQuery('#ship-to-different-address-checkbox').is(':checked')) {
            var state = jQuery('#shipping_state').val();
            var post_code = jQuery('#shipping_postcode').val();
        } else {
            var state = jQuery('#billing_state').val();
            var post_code = jQuery('#billing_postcode').val();

        }
        console.log(state + post_code);
        var data = {
            action: 'woocommerce_apply_state',
            security: wc_checkout_params.apply_state_nonce,
            state: state,
            post_code: post_code
        };

        jQuery.ajax({
            type: 'POST',
            url: wc_checkout_params.ajax_url,
            data: data,
            success: function (code) {
                console.log(code);
//                jQuery('.woocommerce-error, .woocommerce-message').remove();

                if (code === '0') {
//                    $form.before(code);
                    jQuery('body').trigger('update_checkout');
                }
            },
            dataType: 'html'
        });

        return false;
    });

});

并在functions.php中

wp_enqueue_script('neemo_state', get_template_directory_uri() . '/js/state_test.js', array('jquery'));
wp_localize_script('neemo_state', 'wc_checkout_params', array('ajaxurl' => admin_url('admin-ajax.php')));

add_action('wp_ajax_woocommerce_apply_state', 'calculate', 10);
add_action('wp_ajax_nopriv_woocommerce_apply_state', 'calculate', 10);

function calculate() {
    if (isset($_POST['state'])) {
        global $woocommerce;
        $weight = WC()->cart->cart_contents_weight;
        $state = $_POST['state'];
        if ($state === "VIC") {
            $val = 1;
        } else {
            $val = 2;
        }
        session_start();
        $_SESSION['val'] = $val;
    }
}

add_action('woocommerce_cart_calculate_fees', 'woo_add_cart_fee');

function woo_add_cart_fee() {
    session_start();
    $extracost = $_SESSION['val'];
    WC()->cart->add_fee('Shipping & Handling:', $extracost);
}

标签:wordpress,php,ajax,woocommerce,woothemes
来源: https://codeday.me/bug/20190708/1400676.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有