What am I doing wrong with this code? I am not able to retrieve the post_meta value. I know it is saved correctly because the values are displayed in the order edit page. I am just not able to retrieve in this function:
add_action( 'woocommerce_checkout_create_order', 'rev_change_total_on_checkout', 20, 1 );
function rev_change_total_on_checkout( $order ) {
$new_total_price = get_post_meta( $order->id, '_rev_fee_estimate', true);
$order->set_total($new_total_price );
}
Basically, I am trying to change the total order value after the checkout is submitted based on the values programmatically generated in a custom field value I added to the checkout form. If I hardcode the value of $new_total_price, I am able to achieve this, so I know this function works. I only need to retrieve the saved value of the custom field to finish this.
In think that _rev_fee_estimate is not saved to database yet in this hook as it's a custom field. So you can get anything related.
Instead you need to get the posted value. I will use the key _rev_fee_estimate but it can be something else (you can check for it, inspecting the generated html code for this custom field on checkout page. The necessary key is the attribute "name" value in this field)…
The code:
// Save an additional coverstart value in in the order post meta dat
add_action( 'woocommerce_checkout_create_order', 'initial_coverstart_custom_field_save', 20, 1 );
function initial_coverstart_custom_field_save( $order ) {
if( ! isset($_POST['_rev_fee_estimate']) ) return;
if( ! empty($_POST['_rev_fee_estimate']) )
{
$new_total_price = (float) sanitize_text_field( $_POST['_rev_fee_estimate'] )
$order->set_total( $new_total_price );
}
}
Code goes in function.php file of your active child theme (or active theme). It should work once you will have checked that you have the correct key for this custom field.
Related
I'd like to print custom taxonomy, season on Woocommerce admin quick order preview. I found following code that prints the meta data in Stackoverflow but can't find one that print custom taxonomy.
Any help would be appreciated.
// Add custom order meta data to make it accessible in Order preview template
add_filter( 'woocommerce_admin_order_preview_get_order_details',
'admin_order_preview_add_custom_meta_data', 10, 2 );
function admin_order_preview_add_custom_meta_data( $data, $order ) {
// Replace '_custom_meta_key' by the correct postmeta key
if( $custom_value = $order->get_meta('pa_season') )
$data['custom_key'] = $custom_value; // <= Store the value in the data array.
return $data;
}
// Display custom values in Order preview
add_action( 'woocommerce_admin_order_preview_end', 'custom_display_order_data_in_admin' );
function custom_display_order_data_in_admin(){
// Call the stored value and display it
echo '<div>Value: {{data.custom_key}}</div><br>';
}
In WooCommerce, I would like to add a new custom field to order details. I now that I can use use the code below to create a new custom field 'referenceNumber' and adds in it "ordercreated" value:
update_post_meta($order_id, 'referenceNumber', 'ordercreated']);
What I would like is to make that through checkout once an order is placed.
But it doesn't work it doesn't add a new custom field to order details page and don't add the value 'ordercreated', as you can see in this screenshot:
So the question is how to add a custom field when an order is placed in WooCommerce?
To add a custom field to an order you can use:
WordPress update_post_meta() function (from an order id):
$order_id = $order->get_id(); // If needed
update_post_meta($order_id, 'referenceNumber', 'ordercreated'); // add and save the custom field
WooCommerce WC_Data update_meta_data() method (from the order object or the order id):
$order = wc_get_order( $order_id ); // If needed: Get the WC_Order object from the order Id
update_meta_data('referenceNumber', 'ordercreated'); // Add the custom field
$order->save(); // Save the data
Where referenceNumber is the meta key and ordercreated is the meta value. Both works.
Now to add a custom field to an order when customer place an order, you can use:
woocommerce_checkout_create_order action hook (before order data is saved - used to adjust order data before it's saved):
add_action( 'woocommerce_checkout_create_order', 'add_custom_field_on_placed_order', 10, 2 );
function add_custom_field_on_placed_order( $order, $data ){
$order->update_meta_data( 'referenceNumber', 'ordercreated' );
}
woocommerce_checkout_update_order_meta action hook (order already exist - used to add custom meta data):
add_action( 'woocommerce_checkout_create_order', 'add_custom_field_on_placed_order', 10, 2 );
function add_custom_field_on_placed_order( $order_id, $data ){
$order->update_meta_data( 'referenceNumber', 'ordercreated' );
}
woocommerce_checkout_order_created action hook (order already exist - to trigger an action or also to add custom meta data):
add_action( 'woocommerce_checkout_order_created', 'add_custom_field_on_placed_order', 10, 2 );
function add_custom_field_on_placed_order( $order_id, $data ){
$order->update_meta_data( 'referenceNumber', 'ordercreated' ); // Add the custom field
$order->save(); // Save data (as order exist yet)
}
Or:
add_action( 'woocommerce_checkout_order_created', 'add_custom_field_on_placed_order' );
function add_custom_field_on_placed_order( $order ){
update_post_meta($order->get_id(), 'referenceNumber', 'ordercreated');
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Hi i had a similar requirement few days ago, as i needed to add new field at checkout. Following article helped me. You can check it too. Here is the link. Basically you will have a write a function which will use woo commerce hook "woocommerce_default_address_fields"
https://wisdmlabs.com/blog/add-custom-fields-woocommerce-checkout-page/
I would like to add some custom data to the end of the preview order in Woocommerce order listing page.
For that I have tried the hook 'woocommerce_admin_order_preview_end'. But no way to pass any arguments to that action.
add_action( 'woocommerce_admin_order_preview_end', 'custom_display_order_data_in_admin' );
function custom_display_order_data_in_admin( $order ){
//$order is empty here
}
Does anybody have an idea on this? I'm stuck on this.
You can't get the order object as it's a template that loads specific data via Ajax and there is no arguments for woocommerce_admin_order_preview_end action hook.
Instead the filter hook woocommerce_admin_order_preview_get_order_details will allow you first to add some custom data that you will be able to call and display it after in woocommerce_admin_order_preview_end action hook.
The code:
// Add custom order meta data to make it accessible in Order preview template
add_filter( 'woocommerce_admin_order_preview_get_order_details', 'admin_order_preview_add_custom_meta_data', 10, 2 );
function admin_order_preview_add_custom_meta_data( $data, $order ) {
// Replace '_custom_meta_key' by the correct postmeta key
if( $custom_value = $order->get_meta('_custom_meta_key') )
$data['custom_key'] = $custom_value; // <= Store the value in the data array.
return $data;
}
// Display custom values in Order preview
add_action( 'woocommerce_admin_order_preview_end', 'custom_display_order_data_in_admin' );
function custom_display_order_data_in_admin(){
// Call the stored value and display it
echo '<div>Value: {{data.custom_key}}</div><br>';
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Note: You can also use woocommerce_admin_order_preview_start hook if needed…
In Woocommerce, I am trying to display Email & Phone input fields as a custom field above billing address section.
This is what I did, but failed:
I created some custom fields and displayed them above the billing section. Also, I set the default Email and Phone fields as optional, and removed them. Woo saves order email as a meta into wp_postmenta database table. When I save custom fields, I try to overwrite these meta values with $order->update_meta_data() function, but instead of overwriting it creates a new meta with same meta_key.
function save_extra_checkout_fields( $order, $data ){
if( isset( $data['some_field'] ) ) {
$order->delete_meta_data('_billing_email');
$order->update_meta_data( '_billing_email', sanitize_email( $data['some_field'] ) );
}
}
add_action( 'woocommerce_checkout_create_order', 'save_extra_checkout_fields', 10, 2 );
As you can see even if I try to delete default meta first a duplication occurs.
It is possible to achieve this customization with hooks and filters without hacking the fields in the database, or do you have any idea how to overwrite meta value?
Any help please is welcome.
As billing email is a default field and not custom checkout field, you need to change your code a bit using set_billing_email() WC_Order method instead:
add_action( 'woocommerce_checkout_create_order', 'change_billing_email_checkout_field_value', 10, 2 );
function change_billing_email_checkout_field_value( $order, $data ){
if( isset( $data['some_field'] ) && ! empty( $data['some_field'] ) )
$order->set_billing_email( sanitize_email( $data['some_field'] ) );
}
Code goes in function.php file of your active child theme (or active theme). It should works.
On woocommerce, I am using the code below to render some product custom fields, on cart and on checkout:
add_filter( 'woocommerce_get_item_data', 'rendering_meta_field_on_cart_and_checkout', 10, 2 );
function rendering_meta_field_on_cart_and_checkout( $cart_data, $cart_item ) {
$custom_items = array();
if( !empty( $cart_data ) ) {
$custom_items = $cart_data;
}
if( isset( $cart_item['wccpf_enter_product_id'] ) ) {
$diamond = $cart_item['wccpf_enter_product_id'];
$pacolor = get_the_terms($diamond, 'pa_color');
foreach ( $pacolor as $pacolor ) {
$color= $pacolor ->name;
}
$custom_items[] = array( "name" => "color", "value" => $color);
}
return $custom_items;
}
How can I display that custom product fields wccpf_enter_product_id' value in orders?
Thanks.
You can use a custom function hooked in woocommerce_add_order_item_meta action hook to achieve this.
You will need first to add an attribute in your products to get a "readable clean label" for your custom field value that is going to appear as order items meta data.
For that you have to create an attribute first and then set it in your product with any value (as you will replace this value in the code below).
See HERE some more explanations about that process…
You will have to replace in my code the 'custom_field_key' by your specific custom key that you will find on wp_woocommerce_order_itemmeta MySQL table for the corresponding item ID for your specific Order ID.
To find the corresponding item ID for the order, you can search in wp_woocommerce_order_items MySQL table with the Order ID…
You will have also to set your correct attribute slug instead of 'pa_your_attribute' to display in your orders the correct label text for this custom field value.
(see below other similar answers references).
So your code will be something like this:
// ADD THE INFORMATION AS META DATA SO THAT IT CAN BE SEEN AS PART OF THE ORDER
add_action('woocommerce_add_order_item_meta','add_and_update_values_to_order_item_meta', 1, 3 );
function add_and_update_values_to_order_item_meta( $item_id, $item_values, $item_key ) {
// Getting your custom product ID value from order item meta
$custom_value = wc_get_order_item_meta( $item_id, 'custom_field_key', true );
// Here you update the attribute value set in your simple product
wc_update_order_item_meta( $item_id, 'pa_your_attribute', $custom_value );
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This should work
Related answers:
Adding user custom field value to order items details
Add custom Product data dynamically as item meta data on the Order
Displaying custom product data in Order items view