Display custom data on Woocommerce admin order preview - php

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…

Related

prints custom taxonomy in Woocommerce admin quick order preview

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>';
}

Add a custom field to an order in WooCommerce 3+

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/

Read and update a custom field in woocommerce_product_on_backorder hook

I have a custom field in Woocommerce variation products and I try to update that field wherever there is a backorder. This is the code
add_action( 'woocommerce_product_on_backorder', 'reduce_second_stock' );
function reduce_second_stock( $array ){
$temp = get_post_meta($array['product']->ID, 'second_stock', true);
update_post_meta( $array['product']->ID, 'second_stock', $temp - $array['quantity'] );
}
The custom field is called second_stock which is just a number. What I try to do is to reduce that number based on the backorder quantity of that order.
However, even if the regular stock of the product is updated, my custom field stays the same.
With this hook, $array['product'] is the WC_Product Object, so to get the product Id you need to use the related method get_id()… Since WooCommerce 3 and CRUD Objects, you can use directly the WC_Data methods get_meta(), update_meta_data() and save() on the WC_Product Object like:
add_action( 'woocommerce_product_on_backorder', 'reduce_second_stock' );
function reduce_second_stock( $array ){
if( $original_stock = $array['product']->get_meta('second_stock') ) {
$array['product']->update_meta_data( 'second_stock', ( $original_stock - $array['quantity'] ) );
$array['product']->save();
}
}
Code goes in function.php file of your active child theme (or active theme). It should work.

Remove WooCommerce admin order action from order preview

I want to remove, for shop-managers, the ability to mark an order as completed. To do so, I used the following based on "Hide a specific action button conditionally in Woocommerce admin Orders list" answer in my theme's functions.php file:
add_filter( 'woocommerce_admin_order_actions', 'custom_admin_order_actions', 900, 2 );
function custom_admin_order_actions( $actions, $the_order ){
if(isset(wp_get_current_user()->roles[0]) && wp_get_current_user()->roles[0] == 'shop-manager')
unset($actions['complete']);
return $actions;
}
By this, I succesfully removed the complete button from the shop_order page. However, the shop-manager is still able to complete the order using the Complete button that appears in the order preview. To avoid this, I tried the next action after the previous one:
add_action( 'woocommerce_admin_order_preview_start', 'custom_display_order_data_in_admin' );
function custom_display_order_data_in_admin(){
// Call the stored value and display it
echo '<div>Class = "button hidden wc-action-button wc-action-button-complete complete"</div><br>';
}
However, this does not remove the button from the preview window because it does not substitute the line in the code.
Is there a way to remove this ability from the shop_order page and the order preview at once? If not, how can I hide this button from the preview window?
To remove the "complete" update order status button from admin order preview for "Shop manager" user role, use the following:
add_filter( 'woocommerce_admin_order_preview_actions', 'filter_admin_order_preview_actions', 10, 2 );
function filter_admin_order_preview_actions( $actions, $order ) {
if( current_user_can('shop-manager') && isset($actions['status']['actions']['complete']) ) {
unset($actions['status']['actions']['complete']);
}
return $actions;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.

Get custom field value in woocommerce_checkout_create_order hook

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.

Categories