脚本宝典收集整理的这篇文章主要介绍了php – WooCommerce在结帐页面上使用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请求:
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',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',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); }
以上是脚本宝典为你收集整理的php – WooCommerce在结帐页面上使用ajax添加自定义费用全部内容,希望文章能够帮你解决php – WooCommerce在结帐页面上使用ajax添加自定义费用所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。