Woocommerce使用jQuery和ajax更改订单总数

我在woocommerce中使用插件添加信用卡付款方式。
我已经定制了插件,我添加了一个下拉列表作为支付字段,客户可以在其中选择istallments。 分期付款具有利率,所以我希望每次客户选择他想要的分期付款数量时,它都会相应地更新订单总额。

我使用了一段带有jqueryajax的代码来发布下拉列表的值…

虽然结账字段稍后更新,但价格不会改变,因为$_POST参数似乎没有获得任何值。

如何解决这个问题并获得价格更新。

ajax和php函数在同一个文件中,不知道是否必须做任何事情。 这是jquery:

 jQuery(document).ready(function () { jQuery('#installments').change(function () { var billing_district = jQuery('#installments').val(); console.log(installments); var data = { action: 'woocommerce_apply_district', security: wc_checkout_params.apply_district_nonce, district: billing_district }; jQuery.ajax({ type: 'POST', url: wc_checkout_params.ajax_url, data: data, success: function (code) { console.log(code); if (code === '0') { //$form.before(code); jQuery('body').trigger('update_checkout'); } }, dataType: 'html' }); return false; }); }); 

这是截图:

截图

(编辑)

这是php代码

 cart->total; if ($price > 15) { $installments = static::getInstallments($price); $table = ""; $table =''; $table.=''; $table.='Χωρίς Δόσεις'; /* $table .= 'francotecnologia_wc_parcpagseg_table_with_variation_id_' . ($variationId > 0 ? $variationId : '0') . '" '; $table .= ($variationDisplay === false ? 'style="display:none"' : ''); $table .= '>'; // $tableColspan = (2 + (static::$showColumnTotal?1:0)) *static::$numberOfTableColumns; // if (static::$tableHeaderMessage != '') { // $table .= '' . static::$tableHeaderMessage . ' '; // } // $table .= ''; // $table .= str_repeat('' . static::$language['Installments'] . '' . static::$language['Amount'] . '' // . (static::$showColumnTotal ? '' . static::$language['Total'] . '':''), static::$numberOfTableColumns); // $table .= ''; // $table .= ''; */ $tdCounter = 0; foreach (range(1, $installments) as $parcel) { $calc = static::calculateInstallment($price, $parcel); $tdCounter = 1 + $tdCounter % static::$numberOfTableColumns; // if ($tdCounter == 1) { // $table .= ''; //}.$parcel.' x '.wc_price($calc->price).' = '. wc_price($calc->total). $table.= ''.$parcel.''; //$table .= '' . $parcel . '' . wc_price($calc->price) . '' . (static::$showColumnTotal ? '' . wc_price($calc->total) . '' : ''); // if ($tdCounter == static::$numberOfTableColumns) { // $table .= ''; //} } // if (substr( $table, -5 ) != '') { // $table .= ''; // } // $table .= ''; //if (static::$tableFooterMessage != '') { // $table .= '' . static::$tableFooterMessage . ''; // } $table .= ''; echo $table; } else { return ''; } } add_action('wp_ajax_woocommerce_apply_district', 'calculate', 10); add_action('wp_ajax_nopriv_woocommerce_apply_district', 'calculate', 10); wp_enqueue_script('district', get_template_directory_uri() . '/js/district.js', array('jquery')); wp_localize_script('district', 'wc_checkout_params', array('ajaxurl' => admin_url('admin-ajax.php'))); function calculate() { if (isset($_POST['installments'])){ global $woocommerce; $district = $_POST['installments']; if ($district == 1) { $val = 100; } elseif ($district == 2){ $val = 200; } session_start(); $_SESSION['val'] = $val; } } add_action('woocommerce_cart_calculate_fees', 'wpi_add_ship_fee'); function wpi_add_ship_fee() { @session_start(); $customshipcost = $_SESSION['val']; WC()->cart->add_fee('Υποσύνολο με τόκους ', $customshipcost); } ?> 

我在自定义字段的教程中找到了这个。

http://phpwpinfo.com/how-to-update-shipping-cost-in-cart-dynamically-based-on-a-custom-field-in-woocommerce/

有一个链接,显示如何制作自定义字段,但我使用纯PHP!