Display and save formated dimensions of the product everywhere - php

Inspired by Display the weight of cart and order items in WooCommerce #LoicTheAztec's answer, making some changes by replacing product weight by product dimensions, I am able to show the items dimensions everywhere on cart items and order items.
The problem is that I don't know how to show the labels or adjectives:
Longitud cm x Ancho cm x Alto cm for a Spanish site.
I really appreciate any help.
// Display the cart item dimensions in cart and checkout pages
add_filter( 'woocommerce_get_item_data', 'display_custom_item_data_dimensions', 10, 2 );
function display_custom_item_data_dimensions( $cart_item_data, $cart_item ) {
if ( $cart_item['data']->has_dimensions() > 0 ){
$cart_item_data[] = array( 'name' => __( 'Dimensions', 'woocommerce' ), 'value' => $cart_item['data']->get_dimensions() . ' ' . get_option('woocommerce_dimensions_unit') );
}
return $cart_item_data;
}
// Save and Display the order item dimensions (everywhere)
add_action( 'woocommerce_checkout_create_order_line_item', 'display_order_item_data_dimensions', 20, 4 );
function display_order_item_data_dimensions( $item, $cart_item_key, $values, $order ) {
if ( $values['data']->has_dimensions() > 0 ){
$item->update_meta_data( __( 'Dimensions', 'woocommerce' ), $values['data']->get_dimensions() . ' ' . get_option('woocommerce_dimensions_unit') );
}
}

First the method get_dimensions() has an option to display formatted dimensions, so you can change dimension formatting using the following, to add dimension labels as desired:
add_filter( 'woocommerce_format_dimensions', 'custom_formated_product_dimentions', 10, 2 );
function custom_formated_product_dimentions( $dimension_string, $dimensions ){
$text_domain = 'woocommerce';
$dimension_unit = get_option('woocommerce_dimensions_unit');
if ( empty( $dimension_string ) )
return __( 'N/A', $text_domain );
// Set here your new array of dimensions based on existing keys/values
$new_dimensions = array(
__('width', $text_domain) => $dimensions['width'],
__('length', $text_domain) => $dimensions['length'],
__('height', $text_domain) => $dimensions['height'],
);
$dimensions = array_filter( array_map( 'wc_format_localized_decimal', $new_dimensions ) );
// Loop through new dimensions array adding spanish labels
foreach( $dimensions as $key => $dimention ){
$dimensions[$key] = ucfirst($key) . ' ' . $dimention. ' ' . $dimension_unit;
}
return implode( ' × ', $dimensions);
}
For Spanish language, you can translate strings with Loco plugin or you can replace:
$new_dimensions = array(
__('width', $text_domain) => $dimensions['width'],
__('length', $text_domain) => $dimensions['length'],
__('height', $text_domain) => $dimensions['height'],
);
by:
$new_dimensions = array(
__('longitud', $text_domain) => $dimensions['width'],
__('ancho', $text_domain) => $dimensions['length'],
__('alto', $text_domain) => $dimensions['height'],
);
Now on your code you will use get_dimensions( true ) method to display formatted dimensions as follow:
// Display the cart item dimensions in cart and checkout pages
add_filter( 'woocommerce_get_item_data', 'display_custom_item_data_dimensions', 10, 2 );
function display_custom_item_data_dimensions( $cart_item_data, $cart_item ) {
$product = $cart_item['data'];
if ( $product->has_dimensions() ){
$cart_item_data[] = array(
'name' => __( 'Dimensions', 'woocommerce' ),
'value' => $product->get_dimensions( true ),
);
}
return $cart_item_data;
}
// Save and Display the order item dimensions (everywhere)
add_action( 'woocommerce_checkout_create_order_line_item', 'display_order_item_data_dimensions', 10, 4 );
function display_order_item_data_dimensions( $item, $cart_item_key, $values, $order ) {
$product = $values['data'];
if ( $product->has_dimensions() ){
$item->update_meta_data( __( 'Dimensions', 'woocommerce' ), $product->get_dimensions( true ) );
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Note: As has_dimensions() is a conditional method returning a boolean value, it doesns't need anything else as a condition.
Similar: Rename Length to Diameter in WooCommerce formatted product dimensions output

Related

Add product weight and dimensions as order item meta data on WooCommerce admin manual orders

based on Save product custom-field as custom order item metadata for WooCommerce admin manual orders
I have these codes that show the weight and dimensions of the product for orders placed from the frontend, but I can't get them to work for orders that are placed manually from the backend. When I add the products I do not see the weight and dimensions and they are not shown on the page to pay the order, in order-received and email.
That is my code:
Show and Save the weight of the product everywhere.
// Display the cart item weight in cart and checkout pages
add_filter( 'woocommerce_get_item_data', 'display_custom_item_data', 10, 2 );
function display_custom_item_data( $cart_item_data, $cart_item ) {
if ( $cart_item['data']->get_weight() > 0 ){
$cart_item_data[] = array(
'name' => __( 'Weight', 'woocommerce' ),
'value' => $cart_item['data']->get_weight() . ' ' . get_option('woocommerce_weight_unit')
);
}
return $cart_item_data;
}
// Save and Display the order item weight (everywhere)
add_action( 'woocommerce_checkout_create_order_line_item', 'display_order_item_data', 20, 4 );
function display_order_item_data( $item, $cart_item_key, $values, $order ) {
if ( $values['data']->get_weight() > 0 ){
$item->update_meta_data( __( 'Weight', 'woocommerce' ), $values['data']->get_weight() . ' ' . get_option('woocommerce_weight_unit') );
}
}
Show and Save product dimensions everywhere.
// Display the cart item weight in dimensions and checkout pages
add_filter( 'woocommerce_get_item_data', 'display_custom_item_data1', 10, 2 );
function display_custom_item_data1( $cart_item_data, $cart_item ) {
if ( $cart_item['data']->has_dimensions() > 0 ){
$cart_item_data[] = array(
'name' => __( 'Dimensions', 'woocommerce' ),
'value' => $cart_item['data']->get_dimensions() . ' ' . get_option('woocommerce_dimensions_unit')
);
}
return $cart_item_data;
}
// Save and Display the order item dimensions (everywhere)
add_action( 'woocommerce_checkout_create_order_line_item', 'display_order_item_data1', 20, 4 );
function display_order_item_data1( $item, $cart_item_key, $values, $order ) {
if ( $values['data']->has_dimensions() > 0 ){
$item->update_meta_data( __( 'Dimensions', 'woocommerce' ), $values['data']->get_dimensions() . ' ' . get_option('woocommerce_dimensions_unit') );
}
}
add_filter( 'woocommerce_format_dimensions', 'custom_formated_product_dimentions', 10, 2 );
function custom_formated_product_dimentions( $dimension_string, $dimensions ){
$text_domain = 'woocommerce';
$dimension_unit = get_option('woocommerce_dimensions_unit');
if ( empty( $dimension_string ) )
return __( 'N/A', $text_domain );
// Set here your new array of dimensions based on existing keys/values
$new_dimentions = array(
__('Largo:', $text_domain) => $dimensions['length'],
__('Ancho:', $text_domain) => $dimensions['width'],
__('Alto:', $text_domain) => $dimensions['height'],
);
$dimensions = array_filter( array_map( 'wc_format_localized_decimal', $new_dimentions ) );
foreach( $dimensions as $key => $dimention ){
$dimensions[$key] = ucfirst($key) . ' ' . $dimention. ' ' . get_option( 'woocommerce_dimension_unit' );
}
return implode( ' × ', $dimensions);
}
To display product weight and dimensions as custom order item meta data on manual backend orders, you will use woocommerce_before_save_order_item dedicated action hook as follow:
add_action( 'woocommerce_before_save_order_item', 'action_before_save_order_item_callback' );
function action_before_save_order_item_callback( $item ) {
// 1. WEIGHT
$weight = $item->get_meta('Weight');
// If custom meta data is not saved as order item
if ( empty($weight) ) {
// Get the WC_Product Object
$product = $item->get_product();
// Get custom meta data from the product
$weight = $product->get_weight();
$weight_unit = get_option('woocommerce_weight_unit');
// Save it as custom order item (if defined for the product)
if ( $weight ) {
$item->update_meta_data( 'Weight', $weight . ' ' . $weight_unit );
}
}
// 2. DIMENSIONS
$dimensions = $item->get_meta('Dimensions');
// If custom meta data is not saved as order item
if ( empty($dimensions) ) {
// Get the WC_Product Object
$product = $item->get_product();
// Get custom meta data from the product
$dimensions = $product->get_dimensions();
$dim_unit = get_option('woocommerce_dimensions_unit');
// Save it as custom order item (if defined for the product)
if ( $dimensions ) {
$item->update_meta_data( 'Dimensions', $dimensions . ' ' . $dim_unit );
}
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Save custom metadata from subscription product variation to order

I need to be able to add admin data to subscription product variations (Woo Subscriptions extension) in order to pass this data on to fulfilment centres when an order is placed.
The issue I have is getting these values out from the product and passing them into the order and email notifications such that they can be properly exported, added to shipping labels and emailed via the various woo notifications.
I have custom meta fields added to variations on a subscription product. I can add data into these and they save data properly into the database on the correct variation ID. No problem there.
I can show custom test data on the cart and checkout page and also save this data into the order as you'll see in the code.
For brevity, the code is reduced below but there are in fact 6 text fields:
// Add custom field inputs to each product variation
add_action( 'woocommerce_variation_options_pricing', 'add_custom_field_to_variations', 10, 3 );
function add_custom_field_to_variations( $loop, $variation_data, $variation ) {
woocommerce_wp_text_input(
array(
'id' => '_dose_customer_field[' . $loop . ']',
'label' => __( 'Dose (Customer)', 'woocommerce' ),
'placeholder' => __('Enter the treatment dosage','woocommerce' ),
'desc_tip' => 'true',
'description' => __( 'Enter dosage info for this variation', 'woocommerce' ),
'class' => '',
'value' => get_post_meta( $variation->ID, '_dose_customer_field', true )
)
);
}
// Save custom field on product variation save
add_action( 'woocommerce_save_product_variation', 'save_custom_field_variations', 10, 2 );
function save_custom_field_variations( $variation_id, $i ) {
$_dose_customer_field = $_POST['_dose_customer_field'][$i];
if ( isset( $_dose_customer_field ) ) update_post_meta( $variation_id, '_dose_customer_field', esc_attr( $_dose_customer_field ) );
}
// Add metadata to cart item post.
add_filter( 'woocommerce_add_cart_item_data', 'add_custom_fields_cart_item_data', 10, 2 );
function add_custom_fields_cart_item_data( $cart_item_data, $product_id ){
$cart_item_data['custom_data']['_dose_customer_field'] = 'TEST DATA' /* HOW TO GET DATA FROM CUSTOM FIELD HERE */
$cart_item_data['custom_data']['unique_key'] = md5( microtime().rand() );
WC()->session->set( 'custom_data', $cart_item_data['custom_data'] );
return $cart_item_data;
}
// Display custom meta data on cart and checkout page.
add_filter( 'woocommerce_get_item_data', 'display_custom_fields_cart_item_data', 10, 2 );
function display_custom_fields_cart_item_data($item_data, $cart_item){
if( ! array_key_exists( 'custom_data', $cart_item ) )
return $item_data;
if( array_key_exists( '_dose_customer_field', $cart_item['custom_data'] ) )
$item_data[] = array(
'key' => __('Dose (Customer)', 'woocommerce'),
'value' => $cart_item['custom_data']['_dose_customer_field']
);
return $item_data;
}
// Add metadata to the order.
add_action('woocommerce_checkout_create_order_line_item', 'save_custom_fields_as_order_item_meta', 20, 4);
function save_custom_fields_as_order_item_meta($item, $cart_item_key, $values, $order) {
if( ! isset($values['custom_data']) )
return;
$text_domain ='woocommerce';
if( array_key_exists('_dose_customer_field', $values['custom_data']) ){
$item->update_meta_data( __('Dose (Customer)', $text_domain), $values['custom_data']['_dose_customer_field'] );
}
}
// Frontend & emails: Display data on notifications
add_action( 'woocommerce_order_item_meta_start', 'vp_order_item_display_commodity_code', 10, 4 );
function vp_order_item_display_commodity_code( $item_id, $item, $order, $plain_text ) {
// Not on admin
//if( is_admin() ) return;
if( $value = $item->get_meta('_dose_customer_field') ) {
$value = '<strong>' . __("Dose (Customer)", "woocommerce") . ':</strong> ' . esc_attr( $value );
// On orders
if( is_wc_endpoint_url() )
echo '<div class="vp-ccode"><small>' . $value . '</small></div>';
// On Emails
else
echo '<div>' . $value . '</div>';
}
}
So everything is working except getting the data from the custom fields. It seems odd that I have to create a new array (here called 'custom_data') and add it to the cart_item_data, when the data is already available on the product variation, albeit private.
My question is how do get the metadata within add_custom_fields_cart_item_data()? Is this even the correct approach?
Many thanks
As this custom data exist for the product, you can get it easily and save it as order item custom data (so it will be displayed everywhere):
The complete code (changed the 2 last functions):
// Add custom field inputs to each product variation
add_action( 'woocommerce_variation_options_pricing', 'add_custom_field_to_variations', 10, 3 );
function add_custom_field_to_variations( $loop, $variation_data, $variation ) {
woocommerce_wp_text_input(
array(
'id' => '_dose_customer_field[' . $loop . ']',
'label' => __( 'Dose (Customer)', 'woocommerce' ),
'placeholder' => __('Enter the treatment dosage','woocommerce' ),
'desc_tip' => 'true',
'description' => __( 'Enter dosage info for this variation', 'woocommerce' ),
'class' => '',
'value' => get_post_meta( $variation->ID, '_dose_customer_field', true )
)
);
}
// Save custom field on product variation save
add_action( 'woocommerce_save_product_variation', 'save_custom_field_variations', 10, 2 );
function save_custom_field_variations( $variation_id, $i ) {
$_dose_customer_field = $_POST['_dose_customer_field'][$i];
if ( isset( $_dose_customer_field ) ) update_post_meta( $variation_id, '_dose_customer_field', esc_attr( $_dose_customer_field ) );
}
// Display custom meta data on cart and checkout page.
add_filter( 'woocommerce_get_item_data', 'display_custom_fields_cart_item_data', 10, 2 );
function display_custom_fields_cart_item_data( $item_data, $cart_item ) {
if( $value = $cart_item['data']->get_meta( '_dose_customer_field' ) )
$item_data[] = array(
'key' => __('Dose (Customer)', 'woocommerce'),
'value' => $value
);
return $item_data;
}
// Add metadata to the order item (and display it everywhere).
add_action('woocommerce_checkout_create_order_line_item', 'save_custom_fields_as_order_item_meta', 10, 4);
function save_custom_fields_as_order_item_meta( $item, $cart_item_key, $values, $order ) {
if( $meta_value = $values['data']->get_meta( '_dose_customer_field' ) )
$item->update_meta_data( __('Dose (Customer)', 'woocommerce'), $meta_value );
}
Code goes in functions.php file of your active child theme (or active theme). It should work.

Display Custom Field Value in Order View Column WooCommerce

I have this code which creates a drop-down on the product page. It saves the data chosen and it displays on the order. What I am need help with is displaying the value in the custom column.
Here's the code:
add_action( 'woocommerce_product_options_general_product_data', 'costcenter_management_group' );
function costcenter_management_group() {
global $post;
echo '<div class="options_group">';
woocommerce_wp_select( array(
'id' => '_cost_center',
'label' => __( 'Cost Center', 'woocommerce' ),
'options' => array(
'One' => __( 'Office One', 'woocommerce' ),
'Two' => __( 'Office Two', 'woocommerce' ),
)));
echo '</div>';
}
// save cost center settings
add_action( 'woocommerce_process_product_meta', 'costcenter_management_group_save' );
function costcenter_management_group_save( $post_id ){
if( isset( $_POST['_cost_center'] ) )
update_post_meta( $post_id, '_cost_center', esc_attr( $_POST['_cost_center'] ) );
}
// display on order
add_action('woocommerce_checkout_create_order_line_item', 'costcenter_management_group_display', 20, 4);
function costcenter_management_group_display($item, $cart_item_key, $values, $order) {
if ( $cost_center = $values['data']->get_meta('_cost_center') ) {
$item->update_meta_data( 'Cost Center', $cost_centre );
}}
// add cost center column on order view (WC admin)
add_filter('manage_edit-shop_order_columns', 'costcenter_management_group_column', 10, 1 );
function costcenter_management_group_column( $columns ) {
$actions_key = isset($columns['wc_actions']) ? 'wc_actions' : 'order_actions';
$order_actions = $columns[$actions_key];
unset($columns[$actions_key]);
$columns['cost_center'] = __("Cost Center", "woocommerce");
$columns[$actions_key] = $order_actions;
return $columns;
}
I know I can use echo but I don't know which value I'm supposed to use?
To display this custom order item metadata in your custom column use the following:
// Display data to custom column in admin orders list
add_action( 'manage_shop_order_posts_custom_column' , 'display_enclosed_invoice_order_column_data' );
function display_enclosed_invoice_order_column_data( $column ) {
global $the_order, $post;
if( $column == 'cost_center' ) {
$values = []; // Initializing
// Loop through order items
foreach ( $the_order->get_items() as $item ) {
if( $cost_centre = $item->get_meta( 'Cost Center' ) ) {
$values[] = $cost_centre;
}
}
// Display the value(s)
if( sizeof( $values ) > 0 ) {
echo implode( ', ', $values); // Convert the array to a coma separated string
}
}
}
Code goes on function.php file of your active child theme (or active theme). It should works.

Displaying custom field of product variations within Woocommerce orders and emails

I successfully added a custom field for product variations within the backend of WooCommerce and was able to display its value. I'd like to contain this value within order and emails as well.
//Display Fields in admin on product edit screen
add_action( 'woocommerce_product_after_variable_attributes', 'woo_variable_fields', 10, 3 );
//Save variation fields values
add_action( 'woocommerce_save_product_variation', 'save_variation_fields', 10, 2 );
// Create new fields for variations
function woo_variable_fields( $loop, $variation_data, $variation ) {
echo '<div class="variation-custom-fields">';
// Text Field
woocommerce_wp_text_input(
array(
'id' => '_text_field['. $loop .']',
'label' => __( 'additional fees (e.g. monthly fee)', 'woocommerce' ),
'placeholder' => '',
//'desc_tip' => true,
'wrapper_class' => 'form-row form-row-first',
//'description' => __( 'Enter the custom value here.', 'woocommerce' ),
'value' => get_post_meta($variation->ID, '_text_field', true)
)
);
echo "</div>";
}
/** Save new fields for variations */
function save_variation_fields( $variation_id, $i) {
// Text Field
$text_field = stripslashes( $_POST['_text_field'][$i] );
update_post_meta( $variation_id, '_text_field', esc_attr( $text_field ) );
}
// Custom Product Variation
add_filter( 'woocommerce_available_variation', 'custom_load_variation_settings_products_fields' );
function custom_load_variation_settings_products_fields( $variations ) {
$variations['variation_custom_field'] = get_post_meta( $variations[ 'variation_id' ], '_text_field', true );
return $variations;
}
I'm using the following to display the custom field's value within the cart.
add_filter( 'woocommerce_get_item_data', 'display_custom_field_as_item_data', 20, 2 );
function display_custom_field_as_item_data( $cart_data, $cart_item ) {
if( $value = get_post_meta( $cart_item['data']->get_id(), '_text_field', true ) ){
$cart_data[] = array(
'name' => __( 'Additional Monthly Fee', 'woocommerce' ),
'value' => sanitize_text_field( $value )
);
}
return $cart_data;
}
Further as for now I'm displaying its value below the product price / total of each item within cart/checkout template with the following
echo get_post_meta( $_product->get_id(), '_text_field', true );
How about displaying the value of this field within orders and emails?
Updated: To display that in order pages and email notifications try the following additional functions (I have made some changes in the woocommerce_get_item_data hooked function, so you need to replace it with the following one):
// Save custom field value in cart item
add_filter( 'woocommerce_add_cart_item_data', 'save_custom_field_in_cart_object', 30, 3 );
function save_custom_field_in_cart_object( $cart_item_data, $product_id, $variation_id ) {
// Get the correct Id to be used
$the_id = $variation_id > 0 ? $variation_id : $product_id;
if( $value = get_post_meta( $the_id, '_text_field', true ) )
$cart_item_data['custom_data'] = sanitize_text_field( $value );
return $cart_item_data;
}
// Display on cart and checkout pages
add_filter( 'woocommerce_get_item_data', 'display_custom_field_as_item_data', 20, 2 );
function display_custom_field_as_item_data( $cart_data, $cart_item ) {
if( isset( $cart_item['custom_data'] ) ){
$cart_data[] = array(
'name' => __( 'Additional Monthly Fee', 'woocommerce' ),
'value' => $cart_item['custom_data']
);
}
return $cart_data;
}
// Save custom field value in order items meta data
add_action( 'woocommerce_add_order_item_meta', 'add_custom_field_to_order_item_meta', 20, 3 );
function add_custom_field_to_order_item_meta( $item_id, $values, $cart_item_key ) {
if( isset( $values['custom_data'] ) )
wc_add_order_item_meta( $item_id, __( 'Additional Monthly Fee', 'woocommerce' ), $values['custom_data'] );
}
Code goes in function.php file of your active child theme (or theme). Tested and works.
It will display the custom field label and value in all Order pages and in email notifications too.

Adding a notice on Cart and checkout page based on item custom field highest value

I have a a custom field value for each product. This custom field is inserted with the code below :
<?php
// Insert a Custom Admin Field
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
function woo_add_custom_general_fields() {
echo '<div class="options_group">';
woocommerce_wp_text_input( array(
'id' => 'days_manufacture',
'label' => __( 'Days for Manufacture', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Insert here', 'woocommerce' ),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '1'
),
) );
echo '</div>';
}
// Save the field
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields_save( $post_id ){
$woocommerce_number_field = $_POST['days_manufacture'];
if( !empty( $woocommerce_number_field ) )
update_post_meta( $post_id, 'days_manufacture', esc_attr( $woocommerce_number_field ) );
}
// Store custom field
add_action( 'woocommerce_add_cart_item_data', 'save_days_field', 10, 2 );
function save_days_field( $cart_item_data, $product_id ) {
$special_item = get_post_meta( $product_id , 'days_manufacture',true );
if(!empty($special_item)) {
$cart_item_data[ 'days_manufacture' ] = $special_item;
// below statement make sure every add to cart action as unique line item
$cart_item_data['unique_key'] = md5( microtime().rand() );
WC()->session->set( 'days_manufacture', $special_item );
}
return $cart_item_data;
}
// Render meta on cart and 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();
// Woo 2.4.2 updates
if( !empty( $cart_data ) ) {
$custom_items = $cart_data;
}
if( isset( $cart_item['days_manufacture'] ) ) {
$custom_items[] = array( "name" => __( "Days", "woocommerce" ), "value" => $cart_item['days_manufacture'] );
}
return $custom_items;
} ?>
This code works perfectly.
Now I would like on Cart and checkout (before coupon notice) pages to display the highest value of this custom field (days_manufacture) on a custom message, when multiple items are in Cart, like in this screenshot.
How can I achieve this?
Thanks.
Here is a custom function that will display a message on cart and checkout (before coupon notice) pages with the highest number of days of manufacture…
Here is the code:
add_action('woocommerce_before_cart', 'days_of_manufacture');
add_action('woocommerce_before_checkout_form', 'days_of_manufacture', 5);
function days_of_manufacture() {
$day_txt = ' ' . __('day', 'your_theme_domain_slug' );
$days_txt = ' ' . __('days', 'your_theme_domain_slug' );
$text = __('Your Order will be produced in: ', 'your_theme_domain_slug' );
$max_days = 0;
foreach( WC()->cart->get_cart() as $cart_item )
if($cart_item['days_manufacture'] > $max_days)
$max_days = $cart_item['days_manufacture'];
if($max_days != 0) {
if ($max_days == 1)
$days_txt = $day_txt;
$output = $text . $max_days . $days_txt;
echo "<div class='woocommerce-info'>$output</div>";
}
}
This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works.
Store the variable in the $_SESSION, then loop thru them and pick the highest, so you can use it later anywhere in your code

Categories