We have a WooCommerce store where we charge the customer the Product weight variance fee initially and adjust it through the edit order page, recently, we added a fee for COD orders, the COD order is calculated using "product total + Variance fee" = Eligible total for Cash on delivery orders fee. e.g products subtotal is 220 + 33(15% Variance fee)= 253 * 3.5 =8.855 COD fee. This works on the checkout stage, the issue is if try to update the variance fee on the edit order page, the code doesn't get updated. The following is full code, but the second part is not working. add_action('woocommerce_cart_calculate_fees', 'add_separate_cod_fee_without_line_item'); function add_separate_cod_fee_without_line_item() { if (is_admin() && !defined('DOING_AJAX')) { return; // Don't apply fee in admin (unless AJAX). } $chosen_gateway = WC()->session->get('chosen_payment_method'); if ($chosen_gateway === 'cod') { $fee_percentage = 3.5; // COD fee percentage $cart_subtotal = WC()->cart->get_subtotal(); // Product subtotal // Get the variance fee (specifically for 'Product Weight Variance') $variance_fee = 0; for each (WC()->cart->get_fees() as $fee) { if (strpos($fee->name, 'Product Weight Variance') !== false) { $variance_fee += $fee->amount; } } // Calculate the total eligible for COD fee $total_eligible = $cart_subtotal + $variance_fee; // Calculate the COD fee (3.5% of the eligible total) $cod_fee = ($fee_percentage / 100) * $total_eligible; // Ensure the fee is not rounded $cod_fee = ceil($cod_fee * 100) / 100; // Save the COD fee in session for use later without adding it as a line item WC()->session->set('cod_fee', $cod_fee); } } // Update the COD fee in the order metadata when the order is placed, but don't display it separately add_action('woocommerce_checkout_update_order_meta', 'save_cod_fee_in_order_meta'); function save_cod_fee_in_order_meta($order_id) { $order = wc_get_order($order_id); $payment_method = $order->get_payment_method(); if ($payment_method === 'cod') { $cod_fee = WC()->session->get('cod_fee', 0); // Retrieve saved COD fee // Save the fee in order meta data $order->update_meta_data('_cod_fee', $cod_fee); $order->save(); } } // AJAX handler to update COD fee when the Product Weight Variance fee changes add_action('wp_ajax_update_cod_fee_on_weight_change', 'update_cod_fee_on_weight_change_ajax'); function update_cod_fee_on_weight_change_ajax() { $order_id = isset($_POST['order_id']) ? intval($_POST['order_id']) : 0; $weight_fee = isset($_POST['weight_fee']) ? floatval($_POST['weight_fee']) : 0; if ($order_id > 0) { $order = wc_get_order($order_id); // COD fee percentage (for example: 3.5%) $cod_fee_percentage = 3.5; // Get the subtotal of the order (total product cost) $order_subtotal = $order->get_subtotal(); // Calculate the total eligible for the COD fee (subtotal + weight-based fee) $total_eligible = $order_subtotal + $weight_fee; // Recalculate the COD fee (3.5% of the eligible total) $cod_fee = ($cod_fee_percentage / 100) * $total_eligible; // Round up the fee to the nearest cent $cod_fee = ceil($cod_fee * 100) / 100; // Update the COD fee line item in the order foreach ($order->get_items('fee') as $fee_item_id => $fee_item) { if (strpos($fee_item->get_name(), 'Cash on Delivery Fee') !== false) { wc_update_order_item_meta($fee_item_id, '_line_total', $cod_fee); wc_update_order_item_meta($fee_item_id, '_line_tax', 0); // Assuming no tax on COD fee } } // Save the updated order totals $order->calculate_totals(); $order->save(); // Return the updated COD fee to the front-end wp_send_json_success(array('new_cod_fee' => wc_price($cod_fee))); } else { wp_send_json_error('Invalid order ID'); } } // Add inline JavaScript to WooCommerce admin order edit page add_action('admin_footer', 'add_inline_js_for_cod_fee_update_on_weight_change'); function add_inline_js_for_cod_fee_update_on_weight_change() { global $post; // Only add the script on the WooCommerce order edit page if (get_post_type($post->ID) === 'shop_order') { ?> <script type="text/javascript"> jQuery(function($) { // Listen for changes in the "Product Weight Variance" input field $('input[name^="line_total"]').on('change', function() { var orderId = $('#post_ID').val(); // Get the updated Product Weight Variance fee var weightFeeInput = $(this); var feeName = weightFeeInput.closest('tr').find('.name .view').text(); // Check if the changed input is for the Product Weight Variance if (feeName.includes('Product Weight Variance')) { var weightFee = parseFloat(weightFeeInput.val()); // Debugging: log the new weight fee console.log('Weight Fee Updated:', weightFee); // AJAX request to update the COD fee based on the new weight fee $.ajax({ url: ajaxurl, type: 'POST', data: { action: 'update_cod_fee_on_weight_change', order_id: orderId, weight_fee: weightFee }, success: function(response) { if (response.success) { // Update the COD fee on the order page $('input[name="line_total[22500]"]').val(response.data.new_cod_fee); // Debugging: log the successful fee update console.log('COD Fee Updated Successfully:', response.data.new_cod_fee); } else { alert('Failed to update the COD fee.'); console.log('AJAX Error:', response); } }, error: function(xhr, status, error) { alert('Error updating the COD fee.'); console.log('AJAX Request Failed:', xhr, status, error); } }); } }); }); </script> <?php } } ``` This is not working to update the COD fee on the order edit page, I'm struggling to find out correct solution, anyone point me in the right direction? Thanks! [![desired result][1]][1] [![initial results][2]][2] [1]: https://i.sstatic.net/TMHVipGJ.png [2]: https://i.sstatic.net/4ozDMMLj.png