WooCommerce coupon codes in order overview HPOS

You are correct that using WC()->session->set('jne_custom_shipping_cost', $shipping_cost); only stores the shipping cost in the session without actually modifying the order data. To include the custom shipping cost into the actual order total when the order is placed, you need to use specific WooCommerce hooks :

//Function 1 : Add the custom shipping cost to the cart total
add_action('woocommerce_cart_calculate_fees', 'add_custom_shipping_cost', 10, 1);
function add_custom_shipping_cost($cart) {
    if (is_admin() && !defined('DOING_AJAX')) {
        return;
    }

    if (did_action('woocommerce_cart_calculate_fees') >= 2) {
        return;
    }

    $shipping_cost = WC()->session->get('jne_custom_shipping_cost');

    if (!empty($shipping_cost)) {
        $cart->add_fee(__('Custom Shipping Cost', 'your-text-domain'), $shipping_cost);
    }
}

//Function 2 : Save the custom shipping cost into the order meta
add_action('woocommerce_checkout_create_order', 'save_custom_shipping_cost_order_meta', 20, 1);
function save_custom_shipping_cost_order_meta($order) {
    $shipping_cost = WC()->session->get('jne_custom_shipping_cost');

    if (!empty($shipping_cost)) {
        $order->set_shipping_total($shipping_cost);
    }
}

//Function 3 : Display the custom shipping cost in the admin order screen
add_action('woocommerce_admin_order_data_after_shipping_address', 'display_custom_shipping_cost_in_admin', 10, 1);
function display_custom_shipping_cost_in_admin($order){
    $shipping_cost = $order->get_shipping_total();
    if (!empty($shipping_cost)) {
        echo '<p><strong>'.__('Custom Shipping Cost', 'your-text-domain').':</strong> ' . wc_price($shipping_cost) . '</p>';
    }
}

WooCommerce provides specific hooks to manage the addition of custom fees and their persistence through the cart and order lifecycle. By using these hooks, such as woocommerce_cart_calculate_fees for adding fees to the cart and woocommerce_checkout_create_order for saving them in the order, you ensure that custom shipping costs are accurately calculated and retained.

Unlike cart items, cart fees are non-persistent, this means
third-party plugins which add fees need to be constantly hooked into
the cart calculations, adding their fees each time the cart totals are
calculated. To do that WooCommerce provides the woocommerce_cart_calculate_fees hook, which third parties can use to add their fees.

Source : https://woocommerce.com/document/subscriptions/develop/recurring-cart-fees/#section-1

techhipbettruvabetnorabahisbahis forumutaraftarium24eduseduseduedueduedueduseduedusedus