Woocommerce CSV export not showing values - php

I've noticed a strange behaviour of one of my functions. Just take a look. Using first function I'm displaying some custom fields data in the backend:
add_filter( 'manage_edit-shop_order_columns', 'MY_COLUMNS_FUNCTION', 10 );
function MY_COLUMNS_FUNCTION( $columns ) {
$new_columns = ( is_array( $columns ) ) ? $columns : array();
unset( $new_columns['order_actions'] );
//edit this for you column(s)
//all of your columns will be added before the actions column
$new_columns['product_name'] = 'Product';
$new_columns['authors_income'] = 'Author';
$new_columns['order_actions'] = $columns['order_actions'];
return $new_columns;
}
add_action( 'manage_shop_order_posts_custom_column', 'MY_COLUMNS_VALUES_FUNCTION', 2 );
function MY_COLUMNS_VALUES_FUNCTION( $column ) {
global $post;
$order = new WC_Order( $post->ID );
$items = $order->get_items();
//start editing, I was saving my fields for the orders as custom post meta
//if you did the same, follow this code
if ( $column == 'authors_income' ) {
foreach ( $items as $item ) {
echo $item['PLN-dla-autora'];
echo ' zł';
}
}
if ( $column == 'product_name' ) {
foreach ( $items as $item ) {
echo $item['name'];
}
}
}
add_filter( "manage_edit-shop_order_sortable_columns", 'MY_COLUMNS_SORT_FUNCTION' );
function MY_COLUMNS_SORT_FUNCTION( $columns ) {
$custom = array(
//start editing
'authors_income' => 'PLN-dla-autora',
'product_name' => 'name'
//stop editing
);
return wp_parse_args( $custom, $columns );
Everything works there like a charm. Then I have second function, which somehow does the same, yet it will print the data into the CSV file. Unfortunatelly, this code doesnt work (it display only column header, no data included). I dont see any major differences between those functions so why is that?
function wc_csv_export_modify_column_headers( $column_headers ) {
$new_headers = array(
'author_income' => 'PLN dla autora',
// add other column headers here in the format column_key => Column Name
);
return array_merge( $column_headers, $new_headers );
}
add_filter( 'wc_customer_order_csv_export_order_headers', 'wc_csv_export_modify_column_headers' );
// set the data for each for custom columns
function wc_csv_export_modify_row_data( $order_data, $order ) {
$custom_data = array(
'author_income' => get_post_meta( $order->id, 'PLN dla autora', true ),
// add other row data here in the format column_key => data
);
return array_merge( $order_data, $custom_data );
}
add_filter( 'wc_customer_order_csv_export_order_row', 'wc_csv_export_modify_row_data', 10, 2 );

Related

How to add sortable shipping zone name to a new column in WooCommerce admin order list [duplicate]

I've added a custom column to the "Orders" section of WooCommerce for the shipping zip code. The column and its values appear correctly.
What I cannot figure out is how to make the sorting of this field work (clicking on the column header). Other code examples I could find mention using the hook "manage_edit-shop_order_sortable_columns", but this doesn't seem to be working for this field.
Note: I've seen the other StackOverflow issues about this, but none seem to have sorting working.
/**
* ADD ZIP CODE TO WOOCOMMERCE ORDERS LIST
*/
// Add column (working)
add_filter( 'manage_edit-shop_order_columns', 'custom_woo_columns_function' );
function custom_woo_columns_function( $columns ) {
$new_columns = ( is_array( $columns ) ) ? $columns : array();
unset( $new_columns[ 'order_actions' ] );
// all of your columns will be added before the actions column
$new_columns['zipcode'] = 'Zip Code';
//stop editing
$new_columns[ 'order_actions' ] = $columns[ 'order_actions' ];
return $new_columns;
}
// Change order of columns (working)
add_action( 'manage_shop_order_posts_custom_column', 'custom_woo_admin_value', 2 );
function custom_woo_admin_value( $column ) {
global $post;
$zip_value = get_post_meta($post->ID, '_shipping_postcode', true);
if ( $column == 'zipcode' ) {
echo ( isset( $zip_value ) ? $zip_value : '' );
}
}
// Sort by custom column (NOT WORKING)
add_filter( "manage_edit-shop_order_sortable_columns", 'custom_woo_admin_sort' );
function custom_woo_admin_sort( $columns )
{
$custom = array(
'zipcode' => '_shipping_postcode',
);
return wp_parse_args( $custom, $columns );
}
Update 2020:
You can try this too which make it sortable and searchable:
// Add a custom column
add_filter( 'manage_edit-shop_order_columns', 'add_custom_shop_order_column' );
function add_custom_shop_order_column( $columns ) {
$order_actions = $columns['order_actions'];
unset($columns[ 'order_actions' ]);
// add custom column
$columns['postcode'] = __('Zip Code', 'woocommerce');
// Insert back 'order_actions' column
$columns['order_actions'] = $order_actions;
return $columns;
}
// Custom column content
add_action( 'manage_shop_order_posts_custom_column', 'shop_order_column_meta_field_value' );
function shop_order_column_meta_field_value( $column ) {
global $post, $the_order;
if ( ! is_a( $the_order, 'WC_Order' ) ) {
$the_order = wc_get_order( $post->ID );
}
if ( $column == 'postcode' ) {
echo $the_order->get_shipping_postcode();
}
}
// Make custom column sortable
add_filter( "manage_edit-shop_order_sortable_columns", 'shop_order_column_meta_field_sortable' );
function shop_order_column_meta_field_sortable( $columns )
{
$meta_key = '_shipping_postcode';
return wp_parse_args( array('postcode' => $meta_key), $columns );
}
// Make sorting work properly (by numerical values)
add_action('pre_get_posts', 'shop_order_column_meta_field_sortable_orderby' );
function shop_order_column_meta_field_sortable_orderby( $query ) {
global $pagenow;
if ( 'edit.php' === $pagenow && isset($_GET['post_type']) && 'shop_order' === $_GET['post_type'] ){
$orderby = $query->get( 'orderby');
$meta_key = '_shipping_postcode';
if ('_shipping_postcode' === $orderby){
$query->set('meta_key', $meta_key);
$query->set('orderby', 'meta_value_num');
}
}
}
// Make metakey searchable in the shop orders list
add_filter( 'woocommerce_shop_order_search_fields', 'shipping_postcode_searchable_field' );
function shipping_postcode_searchable_field( $meta_keys ){
$meta_keys[] = '_shipping_postcode';
return $meta_keys;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
I figured it out. Will leave this up here for anyone else trying to add a sorted zip code column. Just add this additional action.
// Make sorting by custom column work properly
add_action('pre_get_posts', 'custom_zipcode_orderby');
function custom_zipcode_orderby($query)
{
if (!is_admin()) return;
$orderby = $query->get('orderby');
if ('_shipping_postcode' == $orderby) {
$query->set('meta_key', '_shipping_postcode');
$query->set('orderby', 'meta_value_num');
}
}
Column name order_actions doesn't exist anymore.
Instead of this use wc_actions.
I am using WooCommerce v3.6.2

How to create product category column in woocommerce orders panel?

How can I manage columns in wordpress for add new column getting product category in woocommerce orders panel. I already found a way to create new columns in entire panel but I don't know how to add.
//MANAGE COLUMNS
add_filter( 'manage_pagamento_posts_columns', 'green_filter_posts_columns' );
function green_filter_posts_columns( $columns ) {
$columns = array(
'cb' => $columns['cb'],
'title' => __( 'Title' ),
'pacote' => __( 'Pacote', 'green' ),
'temporada' => __( 'Temporada', 'green' )
);
return $columns;
}
I can't add comment to the answer above so I post a new one. The answer from doughfabris is almost working. I test on my dev-site and found the code will work after remove "$item_cats = array_unique($item_cats);" below "$order_data = $order->get_data();"
The code here tested and work fine for me. Thanks dougfabris!
BTW, how to make it only show the top level?
// ADDING A NEW COLUMN (keeping "Total" and "Actions" columns at the end)
add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column', 20 );
function custom_shop_order_column($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-column1'] = __( 'Categorias','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_content', 20, 2 );
function custom_orders_list_column_content( $column, $post_id )
{
switch ( $column )
{
case 'my-column1' :
$cats = '';
$cats_final = '';
$item_cats = array();
$order = wc_get_order( $post_id );
$order_data = $order->get_data();
foreach ($order->get_items() as $item_key => $item_values):
## Using WC_Order_Item methods ##
// Item ID is directly accessible from the $item_key in the foreach loop or
$item_id = $item_values->get_product_id();
$item_cat = get_the_terms($item_id, 'product_cat');
foreach ( $item_cat as $term ) {
array_push($item_cats, $term->name);
}
endforeach;
$item_cats = array_unique($item_cats);
foreach($item_cats as $item_final){
$cats_final .= $item_final.', ';
}
echo rtrim($cats_final,", ");
break;
}
}
I found a way to add columns in woocommerce orders panel. In this code I show how to add products categories in each order line filtering by product and removing repeated categories when it happen.
// ADDING A NEW COLUMN (keeping "Total" and "Actions" columns at the end)
add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column', 20 );
function custom_shop_order_column($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-column1'] = __( 'Categorias','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_content', 20, 2 );
function custom_orders_list_column_content( $column, $post_id )
{
switch ( $column )
{
case 'my-column1' :
$cats = '';
$cats_final = '';
$item_cats = array();
$order = wc_get_order( $post_id );
$order_data = $order->get_data();
foreach ($order->get_items() as $item_key => $item_values):
## Using WC_Order_Item methods ##
// Item ID is directly accessible from the $item_key in the foreach loop or
$item_id = $item_values->get_product_id();
$item_cat = get_the_terms($item_id, 'product_cat');
$item_cat = array_unique($item_cat);
foreach ( $item_cat as $term ) {
array_push($item_cats, $term->name);
}
endforeach;
$item_cats = array_unique($item_cats);
foreach($item_cats as $item_final){
$cats_final .= $item_final.', ';
}
echo rtrim($cats_final,", ");
break;
}
}

Add a sortable custom column in Woocommerce Admin Orders list

I've added a custom column to the "Orders" section of WooCommerce for the shipping zip code. The column and its values appear correctly.
What I cannot figure out is how to make the sorting of this field work (clicking on the column header). Other code examples I could find mention using the hook "manage_edit-shop_order_sortable_columns", but this doesn't seem to be working for this field.
Note: I've seen the other StackOverflow issues about this, but none seem to have sorting working.
/**
* ADD ZIP CODE TO WOOCOMMERCE ORDERS LIST
*/
// Add column (working)
add_filter( 'manage_edit-shop_order_columns', 'custom_woo_columns_function' );
function custom_woo_columns_function( $columns ) {
$new_columns = ( is_array( $columns ) ) ? $columns : array();
unset( $new_columns[ 'order_actions' ] );
// all of your columns will be added before the actions column
$new_columns['zipcode'] = 'Zip Code';
//stop editing
$new_columns[ 'order_actions' ] = $columns[ 'order_actions' ];
return $new_columns;
}
// Change order of columns (working)
add_action( 'manage_shop_order_posts_custom_column', 'custom_woo_admin_value', 2 );
function custom_woo_admin_value( $column ) {
global $post;
$zip_value = get_post_meta($post->ID, '_shipping_postcode', true);
if ( $column == 'zipcode' ) {
echo ( isset( $zip_value ) ? $zip_value : '' );
}
}
// Sort by custom column (NOT WORKING)
add_filter( "manage_edit-shop_order_sortable_columns", 'custom_woo_admin_sort' );
function custom_woo_admin_sort( $columns )
{
$custom = array(
'zipcode' => '_shipping_postcode',
);
return wp_parse_args( $custom, $columns );
}
Update 2020:
You can try this too which make it sortable and searchable:
// Add a custom column
add_filter( 'manage_edit-shop_order_columns', 'add_custom_shop_order_column' );
function add_custom_shop_order_column( $columns ) {
$order_actions = $columns['order_actions'];
unset($columns[ 'order_actions' ]);
// add custom column
$columns['postcode'] = __('Zip Code', 'woocommerce');
// Insert back 'order_actions' column
$columns['order_actions'] = $order_actions;
return $columns;
}
// Custom column content
add_action( 'manage_shop_order_posts_custom_column', 'shop_order_column_meta_field_value' );
function shop_order_column_meta_field_value( $column ) {
global $post, $the_order;
if ( ! is_a( $the_order, 'WC_Order' ) ) {
$the_order = wc_get_order( $post->ID );
}
if ( $column == 'postcode' ) {
echo $the_order->get_shipping_postcode();
}
}
// Make custom column sortable
add_filter( "manage_edit-shop_order_sortable_columns", 'shop_order_column_meta_field_sortable' );
function shop_order_column_meta_field_sortable( $columns )
{
$meta_key = '_shipping_postcode';
return wp_parse_args( array('postcode' => $meta_key), $columns );
}
// Make sorting work properly (by numerical values)
add_action('pre_get_posts', 'shop_order_column_meta_field_sortable_orderby' );
function shop_order_column_meta_field_sortable_orderby( $query ) {
global $pagenow;
if ( 'edit.php' === $pagenow && isset($_GET['post_type']) && 'shop_order' === $_GET['post_type'] ){
$orderby = $query->get( 'orderby');
$meta_key = '_shipping_postcode';
if ('_shipping_postcode' === $orderby){
$query->set('meta_key', $meta_key);
$query->set('orderby', 'meta_value_num');
}
}
}
// Make metakey searchable in the shop orders list
add_filter( 'woocommerce_shop_order_search_fields', 'shipping_postcode_searchable_field' );
function shipping_postcode_searchable_field( $meta_keys ){
$meta_keys[] = '_shipping_postcode';
return $meta_keys;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
I figured it out. Will leave this up here for anyone else trying to add a sorted zip code column. Just add this additional action.
// Make sorting by custom column work properly
add_action('pre_get_posts', 'custom_zipcode_orderby');
function custom_zipcode_orderby($query)
{
if (!is_admin()) return;
$orderby = $query->get('orderby');
if ('_shipping_postcode' == $orderby) {
$query->set('meta_key', '_shipping_postcode');
$query->set('orderby', 'meta_value_num');
}
}
Column name order_actions doesn't exist anymore.
Instead of this use wc_actions.
I am using WooCommerce v3.6.2

How to post a value to custom column in admin order panel via Woocommerce API?

In Woocommerce, I have created a custom column to admin orders list page.
<?php
add_filter( 'manage_edit-shop_order_columns', 'MY_COLUMNS_FUNCTION' );
function MY_COLUMNS_FUNCTION( $columns ) {
$new_columns = ( is_array( $columns ) ) ? $columns : array();
unset( $new_columns[ 'order_actions' ] );
$new_columns['MY_COLUMN_ID_1'] = 'MY_COLUMN_1_TITLE';
$new_columns[ 'order_actions' ] = $columns[ 'order_actions' ];
return $new_columns;
}
Then, custom field for this column
add_action( 'manage_shop_order_posts_custom_column', 'MY_COLUMNS_VALUES_FUNCTION', 2 );
function MY_COLUMNS_VALUES_FUNCTION( $column ) {
global $post;
$data = get_post_meta( $post->ID );
if ( $column == 'MY_COLUMN_ID_1' ) {
echo ( isset( $data[ 'MY_COLUMN_1_POST_META_ID' ] ) ? $data[ 'MY_COLUMN_1_POST_META_ID' ] : '' );
}
}
But I don't know how to post a string to the specific custom field via WooCommerce API?
I would tweak Andrew's answer just a little to start using the order object more and rely less on post meta. I'm pretty sure WooCommerce will move at least it's core meta into a custom table eventually.
function wc_add_custom_order_column_content( $column ) {
global $post, $the_order;
if ( empty( $the_order ) || $the_order->get_id() !== $post->ID ) {
$the_order = wc_get_order( $post->ID );
}
// Only continue if we have an order.
if ( empty( $the_order ) ) {
return;
}
if ( 'custom_column' === $column ) {
// Use order class getters to retrieve what you need.
echo $the_order->get_formatted_order_total();
// Or, if it's not a core field, it may be in meta.
// echo $the_order->get_meta('_some_meta_key');
}
}
add_action( 'manage_shop_order_posts_custom_column', 'wc_add_custom_order_column_content' );
Here's an example of how to add a custom column in the admin order screen. It will add your column after the Actions column and populate it with your custom field data. You'll need to replace my "custom_column" values with your own field names.
function wc_add_custom_order_column( $columns ) {
$new_columns = array();
foreach ( $columns as $column_name => $column_info ) {
$new_columns[ $column_name ] = $column_info;
if ( 'order_actions' === $column_name ) {
$new_columns['custom_column'] = __( 'Custom Column', 'my-textdomain' );
}
}
return $new_columns;
}
add_filter( 'manage_edit-shop_order_columns', 'wc_add_custom_order_column', 20 );
function wc_add_custom_order_column_content( $column ) {
global $post;
if ( 'custom_column' === $column ) {
$data = get_post_meta( $post->ID );
if( isset( $data['custom_column'] ) )
echo $data['custom_column'];
}
}
add_action( 'manage_shop_order_posts_custom_column', 'wc_add_custom_order_column_content' );

i want to show origin country shipping in the backend of WooCommerce (in Orders overview). How to do it

I am using WooCommerce in wordpress with ups shipping. I have 2 store with different address. I want to show origin country shipping in the backend.
#-----------------------------------------------------------------#
# Test show distro in admin order page
#-----------------------------------------------------------------#
//add a column
add_filter( 'manage_edit-shop_order_columns', 'MY_COLUMNS_FUNCTION' );
function MY_COLUMNS_FUNCTION($columns){
$new_columns = (is_array($columns)) ? $columns : array();
unset( $new_columns['order_actions'] );
//edit this for you column(s)
//all of your columns will be added before the actions column
$new_columns['MY_COLUMN_ID_1'] = 'Distro test';
//stop editing
$new_columns['order_actions'] = $columns['order_actions'];
return $new_columns;
}
// How can i do it here???????
add_action( 'manage_shop_order_posts_custom_column', 'MY_COLUMNS_VALUES_FUNCTION', 2 );
function MY_COLUMNS_VALUES_FUNCTION($column){
global $post;
$data = get_post_meta( $post->ID );
//start editing, I was saving my fields for the orders as custom post meta
//if you did the same, follow this code
if ( $column == 'MY_COLUMN_ID_1' ) {
echo (isset($data['MY_COLUMN_1_POST_META_ID']) ? $data['MY_COLUMN_1_POST_META_ID'] : '');
}
//stop editing
}
// make column can sort
add_filter( "manage_edit-shop_order_sortable_columns", 'MY_COLUMNS_SORT_FUNCTION' );
function MY_COLUMNS_SORT_FUNCTION( $columns ) {
$custom = array(
//start editing
'MY_COLUMN_ID_1' => 'MY_COLUMN_1_POST_META_ID'
//stop editing
);
return wp_parse_args( $custom, $columns );
}
anyone review mycode and tell me what should i do?
This is example
add_filter( 'manage_edit-shop_order_columns', 'MY_COLUMNS_FUNCTION' );
function MY_COLUMNS_FUNCTION($columns){
$new_columns = (is_array($columns)) ? $columns : array();
unset( $new_columns['order_actions'] );
//edit this for you column(s)
//all of your columns will be added before the actions column
$new_columns['zip_code'] = 'Zip Code';
//stop editing
$new_columns['order_actions'] = $columns['order_actions'];
return $new_columns;
}
add_action( 'manage_shop_order_posts_custom_column', 'MY_COLUMNS_VALUES_FUNCTION',10, 2 );
function MY_COLUMNS_VALUES_FUNCTION($column){
global $post, $the_order;
if ( empty( $the_order ) || $the_order->id != $post->ID ) {
$the_order = wc_get_order( $post->ID );
}
//start editing, I was saving my fields for the orders as custom post meta
//if you did the same, follow this code
if ( $column == 'zip_code' ) {
echo (isset($the_order->shipping_postcode) ? $the_order->shipping_postcode : '');
}
//stop editing
}
thanks 1way for this answer.

Categories