On the WooCommerce Checkout page, I have added an extra field the customer must enter to checkout.
I want to access this field’s value in the woocommerce_cart_calculate_fees action hook.
I have tried several ways by using woocommerce->customer data and order data but cannot get the value. Any assistance is greatly appreciated.
/* WooCommerce Add Extra Fees */
add_action( 'woocommerce_cart_calculate_fees','endo_handling_fee' );
function endo_handling_fee() {
global $woocommerce, $post;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Get the order ID
$order = new WC_Order($post->ID);
// to escape # from order id
$order_id = trim(str_replace('#', '', $order->get_order_number()));
// Here is where I want to get the field value
$orderFee = get_post_meta( $order_id, 'decedents_name_field', true );
}
I ran into the same problem. I had a delivery fee that needed to be applied if fewer than a certain number of products were in the cart AND the user wanted a delivery (my custom field). What I ended up doing to solve this was sending the data to the server via AJAX like so:
Then take that data and put it in a SESSION variable
From there, your server now has access to whatever value your custom field has.
As helgatheviking pointed out, the order hasn’t been submitted yet and doesn’t exist. The only way I can see you getting the data you need is by sending it to the server via AJAX.
If this is confusing, check out the source I used for more information.
Found solution that don’t require AJAX:
The field values are stored in:
WC()->checkout()->get_value(‘post_data’)
In my case I had to add a fee of 2% of on payments (2nd payment and up);
I added a custom field to billing fileds, note use of class update_totals_on_change to trigger cart recalculation to add the fee.
Then I get the number of payments selected by parsing (WC()->checkout()->get_value(‘post_data’)