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>';
}
Related
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…
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.
Hi I am trying to add an AfterShip tracking button or link to my Admin Order list in the backend. I have succesfully created a new column that displays the tracking number for each order. However, I would like to make the tracking number clickable. Or, as an alternative, create an action button that opens a new tab and tracks the number in the Tracking Number column.
The URL format I need is as follows:
https://track.aftership.com/LS325245095CN?
Notice that there is a question mark appended to the tracking number. I would need to do this with the action, since the question mark symbol is not used when entering the tracking number.
Here is the code I am using for displaying the tracking number column in the admin orders list in the backend:
//Start Add Tracking Number to Admin Orders List
//Start Add Header to List
add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column',
12, 1 );
function custom_shop_order_column($columns)
{
// Set "Actions" column after the new colum
$action_column = $columns['order_actions']; // Set the title in a variable
unset($columns['order_actions']); // remove "Actions" column
//add the new column "New Tracking Number"
$columns['order_astracking'] = '<span>'.__( 'Tracking Number','woocommerce').'</span>'; // title
// Set back "Actions" column
$columns['order_actions'] = $action_column;
return $columns;
}
//END Add Header to List
//START Add Tracking Number Data to List
add_action( 'manage_shop_order_posts_custom_column' ,
'custom_order_list_column_content', 10, 2 );
function custom_order_list_column_content( $column, $post_id )
{
// HERE get the data from your custom field (set the correct meta key below)
$astracking = get_post_meta( $post_id, '_aftership_tracking_number', true );
if( empty($astracking)) $astracking = '';
switch ( $column )
{
case 'order_astracking' :
echo '<span>'.$astracking.'</span>'; // display the data
break;
}
}
//END Add Tracking Number Data to List
//START Make Tracking Number Data Searchable in Admin Orders List
add_filter( 'woocommerce_shop_order_search_fields',
'astracking_search_fields', 10, 1 );
function astracking_search_fields( $meta_keys ){
$meta_keys[] = '_aftership_tracking_number';
return $meta_keys;
}
//END Make Tracking Number Data Searchable in Admin Orders List
//END Add Tracking Number to Admin Orders List
I got this code here on Stackoverflow.. awesome resource.
Add custom columns to admin orders list in WooCommerce backend
Any help or suggestions you could provide, would be greatly appreciated. Thanks in advance!
New Update for WC 3.3+: Custom action button in admin orders list on Woocommerce 3.3+
Here is the way to add an action button in admin order list with a custom link related to tracking (opening the link in a new window as requested):
// Add your custom order action button
add_action( 'woocommerce_admin_order_actions_end', 'add_custom_order_actions_button', 100, 1 );
function add_custom_order_actions_button( $order ) {
// Get the tracking number
$traking_number = get_post_meta( $order->get_id(), '_aftership_tracking_number', true );
if( empty($traking_number) ) return;
// Prepare the button data
$url = esc_url('https://track.aftership.com/'.$traking_number.'?');
$name = esc_attr( __('Tracking', 'woocommerce' ) );
$action = esc_attr( 'view tracking' ); // keep "view" class for a clean button CSS
// Set the action button
printf( '<a class="button tips %s" href="%s" data-tip="%s" target="_blank">%s</a>', $action, $url, $name, $name );
}
// The icon of your action button (CSS)
add_action( 'admin_head', 'add_custom_order_actions_button_css' );
function add_custom_order_actions_button_css() {
echo '<style>.view.tracking::after { font-family: woocommerce; content: "\e005" !important; }</style>';
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works. You will get something like:
Now To make your tracking number clickable you will replace this function in your code:
add_action( 'manage_shop_order_posts_custom_column', 'custom_order_list_column_content', 10, 2 );
function custom_order_list_column_content( $column, $post_id )
{
// HERE get the data from your custom field (set the correct meta key below)
$astracking = get_post_meta( $post_id, '_aftership_tracking_number', true );
if( empty($astracking)) $astracking = '';
switch ( $column )
{
case 'order_astracking' :
echo '<span>'.$astracking . '</span>'; // display the data
break;
}
}
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
I'm completely new to PHP and WooCommerce, so this may be a very simple question, however I am trying to have the 'WooCommerce PDF Watermark' plugin print the item quantity from an order on my WordPress site. I cannot get the code right for this. The documentation that comes with the plugin provides the following example that is given below of how to add a template tag to the plugin. Any assistance with this would be hugely appreciated!
function wc_pdf_watermark_extend_template_tags( $parsed_text, $unparsed_text, $order, $product ) {
// Look for {product_title} in text and replace it with the product title
$parsed_text = str_replace( '{product_title}', $product->get_title(), $parsed_text );
return $parsed_text;
}
add_filter( 'woocommerce_pdf_watermark_parse_template_tags', 'wc_pdf_watermark_extend_template_tags' );
All order related details in $order variable.
function wc_pdf_watermark_extend_template_tags( $parsed_text, $unparsed_text, $order, $product ) {
var_dump($order);
wp_die();
return $parsed_text;
}
add_filter( 'woocommerce_pdf_watermark_parse_template_tags', 'wc_pdf_watermark_extend_template_tags' );
Once you get a variable dump you will get all details extract and return as parsed_text.