Reputation: 153
My order has information that I want to call to view on my order page. But this information is not stored as metadata, is there a way to get this information anyway?
Information that I want to get:
Where the information is stored in the code:
I tried some things but I am getting errors all the time, this is what I tried:
// ADDING 2 NEW COLUMNS WITH THEIR TITLES (keeping "Total" and "Actions" columns at the end)
add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column_another', 20 );
function custom_shop_order_column_another($columns)
{
$reordered_columns = array();
// Inserting columns to a specific location
foreach( $columns as $key => $column){
$reordered_columns[$key] = $column;
if( $key == 'order_status' ){
// Inserting after "Status" column
$reordered_columns['my-column3'] = __( 'Wefact email','theme_domain');
$reordered_columns['my-column4'] = __( 'Wefact status','theme_domain');
}
}
return $reordered_columns;
}
// Adding custom fields meta data for each new column (example)
add_action( 'manage_shop_order_posts_custom_column' , 'custom_orders_list_column_Another_content', 20, 2 );
function custom_orders_list_column_Another_content( $column, $post_id )
{
switch ( $column )
{
case 'my-column3' :
// Get custom post meta data
$my_var_one = get_post_meta( $post_id, 'WeFact_email', true );
if(!empty($my_var_one))
echo $my_var_one;
// Testing (to be removed) - Empty value case
else
echo '<small>(<em>no value</em>)</small>';
break;
case 'my-column4' :
// Get custom post meta data
$wfInvoiceID = (int) get_post_meta( $post_id, '_wefact_invoice_id', true);
if (isset($wfInvoiceID)) :
$invoice = new WeFactInvoice();
$wfInvoice = $invoice->showByID($wfInvoiceID);
if ($wfInvoice['status'] == 'success') :
$status = [
"0" => "Concept factuur",
"1" => "Wachtrij factuur",
"2" => "Verzonden",
"3" => "Deels betaald",
"4" => "Betaald",
"8" => "Creditfactuur",
"9" => "Vervallen",
];
if(!empty($wfInvoiceID))
echo $status[$wfInvoice['invoice']['Status']];
// Testing (to be removed) - Empty value case
else
echo '<small>(<em>no value</em>)</small>';
break;
endif;
}
}
Upvotes: 2
Views: 264
Reputation: 254492
Try the following revisited code (using WC_Data
method get_meta()
on the WC_Order
Object):
// ADDING 2 NEW COLUMNS WITH THEIR TITLES (keeping "Total" and "Actions" columns at the end)
add_filter( 'manage_edit-shop_order_columns', 'add_custom_wefact_shop_order_columns', 20 );
function add_custom_wefact_shop_order_columns( $columns ) {
$reordered_columns = array();
// Inserting columns to a specific location
foreach( $columns as $key => $column ){
$reordered_columns[$key] = $column;
if( $key == 'order_status' ){
// Inserting after "Status" column
$reordered_columns['wefact-email'] = __( 'Wefact email', 'theme_domain');
$reordered_columns['wefact-status'] = __( 'Wefact status', 'theme_domain');
}
}
return $reordered_columns;
}
// Adding custom fields meta data for each new column
add_action( 'manage_shop_order_posts_custom_column' , 'custom_wefact_shop_order_columns_content', 20, 2 );
function custom_wefact_shop_order_columns_content( $column, $post_id ) {
global $post, $the_order;
$order = is_a($the_order, 'WC_Order') ? $the_order : wc_get_order($post_id);
if ( 'wefact-email' === $column ) {
$wefact_email = $order->get_meta('WeFact_email'); // Get order custom field
echo empty($wefact_email) ? '<small>(<em>no value</em>)</small>' : $wefact_email;
}
elseif( 'wefact-status' === $column ) {
$wfInvoiceID = $order->get_meta('_wefact_invoice_id'); // Get order custom field
$wfInvoiceID = empty($wfInvoiceID) ? $order->get_meta('_order_wefact_invoice_id') : $wfInvoiceID; // Get order custom field
$value_output = '';
if ( ! empty($wfInvoiceID) && class_exists('WeFactInvoice') ) {
$invoice = new WeFactInvoice();
$wfInvoice = $invoice->showByID($wfInvoiceID);
if ($wfInvoice['status'] == 'success') {
$status = [
"0" => "Concept factuur",
"1" => "Wachtrij factuur",
"2" => "Verzonden",
"3" => "Deels betaald",
"4" => "Betaald",
"8" => "Creditfactuur",
"9" => "Vervallen",
];
if( isset($wfInvoice['invoice']['Status']) && isset($status[$wfInvoice['invoice']['Status']]) ) {
$value_output = $status[$wfInvoice['invoice']['Status']];
}
}
}
echo empty($value_output) ? '<small>(<em>no value</em>)</small>' : $value_output;
}
}
Code goes in functions.php file of the active child theme (or active theme). It should work.
Upvotes: 2