Displaying data in WooCommerce admin orders list custom columns - php

I'm adding the following custom columns and they both appear:
function add_order_new_column_header( $columns ) {
$new_columns = array();
foreach ( $columns as $column_name => $column_info ) {
$new_columns[ $column_name ] = $column_info;
if ( 'order_total' === $column_name ) {
$new_columns['order_shipping'] = __( 'Tarnemeetod', 'my-textdomain' );
$new_columns['order_payment'] = __( 'Maksemeetod', 'my-textdomain' );
}
}
return $new_columns;
Then I'm adding the following to generate data:
add_filter( 'manage_edit-shop_order_columns', 'add_order_new_column_header', 20);
add_action( 'manage_shop_order_posts_custom_column', 'add_wc_order_admin_list_column_content' );
function add_wc_order_admin_list_column_content( $column ) {
global $post;
if ( 'order_shipping' === $column ) {
$order = wc_get_order( $post->ID );
echo $order->get_shipping_method();
if ( 'order_payment' === $column ) {
$order = wc_get_order( $post->ID );
echo $order->get_payment_method_title();
}
}
}
order_shipping is showing correct shipping method, but order_payment is empty.
I'm not using multiple languages on the site. WooCommerce 4.1.1, PHP 7.3.19
view from WooCommerce orders admin panel:
I've tried both get_payment_method and get_payment_method_list. I have orders with BACS mostly and a few Cash on Pickup which is disabled for now. What am I missing here?

There are some mistakes and missing things in your code, try the following instead:
add_filter( 'manage_edit-shop_order_columns', 'add_custom_columns_to_admin_orders', 20);
function add_custom_columns_to_admin_orders( $columns ) {
$new_columns = array();
foreach ( $columns as $column_name => $column_info ) {
$new_columns[ $column_name ] = $column_info;
if ( 'order_total' === $column_name ) {
$new_columns['order_shipping'] = __( 'Tarnemeetod', 'my-textdomain' );
$new_columns['order_payment'] = __( 'Maksemeetod', 'my-textdomain' );
}
}
return $new_columns;
}
add_action( 'manage_shop_order_posts_custom_column', 'custom_columns_content_in_admin_orders' );
function custom_columns_content_in_admin_orders( $column ) {
global $post, $the_order;
if ( 'order_shipping' === $column ) {
echo $the_order->get_shipping_method();
}
if ( 'order_payment' === $column ) {
echo $the_order->get_payment_method_title();
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Related

Dynamically added custom fields not displayed on WooCommerce email notifications

I have a different kind of scenario then the typical custom fields (I suppose). I am not getting custom values (fields) from user in the form rather I have an implementation which adds:
ColorName
Size
City
These are from a custom product flow which adds custom attributes to the cart, here is how I am doing that:
add_action('wp_ajax_wdm_add_user_custom_data_options', 'wdm_add_user_custom_data_options_callback');
add_action('wp_ajax_nopriv_wdm_add_user_custom_data_options', 'wdm_add_user_custom_data_options_callback');
function wdm_add_user_custom_data_options_callback()
{
// print_r($_POST);
$productIDM = $_POST['product_id'];
// case swith
switch ($productIDM) {
case "Ace Dura Silk":
$productID = 3254;
break;
case "Ace Matt Finish":
$productID = 3232;
break;
case "Ace Plastic Emulsion":
$productID = 3276;
break;
case "Ace Weather Defender":
$productID = 2991;
break;
case "Overall Plasticoat":
$productID = 3112;
break;
}
$colorname = $_POST['colorname'];
$cityname = $_POST['cityname'];
$size = $_POST['size'];
$price = $_POST['price'];
global $woocommerce;
$woocommerce->cart->add_to_cart( $productID, 1 );
// die();
// echo 'I am in...';
$result = array(
'status' => true
);
echo json_encode($result);
}
add_filter('woocommerce_add_cart_item_data','wdm_add_item_data',1,10);
function wdm_add_item_data($cart_item_data, $product_id) {
global $woocommerce;
$new_value = array();
$new_value['_custom_options'] = $_POST['custom_options'];
if(empty($cart_item_data)) {
return $new_value;
} else {
return array_merge($cart_item_data, $new_value);
}
}
add_filter('woocommerce_get_cart_item_from_session', 'wdm_get_cart_items_from_session', 1, 3 );
function wdm_get_cart_items_from_session($item,$values,$key) {
if (array_key_exists( '_custom_options', $values ) ) {
$item['_custom_options'] = $values['_custom_options'];
}
return $item;
}
add_filter('woocommerce_cart_item_name','add_usr_custom_session',1,3);
function add_usr_custom_session($product_name, $values, $cart_item_key ) {
$return_string = $product_name . "<div class='cart_subitems_custom'><br /><small>".$values['_custom_options']['colorname']."</small><br /><small>".$values['_custom_options']['cityname']."</small><br /><small>".$values['_custom_options']['size']."</small></div>" ; //. "<br />" . print_r($values['_custom_options']);
return $return_string;
}
add_action('woocommerce_add_order_item_meta','wdm_add_values_to_order_item_meta',1,2);
function wdm_add_values_to_order_item_meta($item_id, $values) {
global $woocommerce,$wpdb;
wc_add_order_item_meta($item_id,'_colorname',$values['_custom_options']['colorname']);
wc_add_order_item_meta($item_id,'_cityname',$values['_custom_options']['cityname']);
wc_add_order_item_meta($item_id,'_size',$values['_custom_options']['size']);
}
add_action( 'woocommerce_before_calculate_totals', 'update_custom_price', 1, 1 );
function update_custom_price( $cart_object ) {
foreach ( $cart_object->cart_contents as $cart_item_key => $value ) {
// Version 2.x
//$value['data']->price = $value['_custom_options']['custom_price'];
// Version 3.x / 4.x
if($value['_custom_options']['price'] == null){
echo"";
}else{
$value['data']->set_price($value['_custom_options']['price']);
}
}
}
I am getting these custom values almost everywhere except Email Notification.
Here is what normal product order edit shows:
Here is how I am getting the custom product in order edit page:
I have tried all the solution I can possibly find (filter & action hooks) but nothing works for me.
I have tried first answer from this:
add_action( 'woocommerce_checkout_create_order_line_item', 'custom_checkout_create_order_line_item', 20, 4 );
function custom_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) {
if( isset( $values['colorname'] ) )
$item->add_meta_data( __('DCM Shade'), $values['_colorname'] );
}
Also the common method I found everywhere:
function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
// Get meta
$color = $order->get_meta( 'colorname', true );
// NOT empty
if( ! empty( $color ) ) {
$fields['colorname'] = array(
'label' => __( 'Shade' ),
'value' => $color,
);
}
// Get (other) meta
$shipping_email = $order->get_meta( '_cityname', true );
// NOT empty
if ( ! empty( $shipping_email ) ) {
$fields['_cityname'] = array(
'label' => __( 'City' ),
'value' => $shipping_email,
);
}
return $fields;
}
add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 );
But I can't get the custom fields.
What am I doing wrong can please anyone please guide me.

Display used coupons description on WooCommerce admin orders list

i want to display used coupons on WooCommerce admin orders list with the coupon description. So i'm using Display used coupons on WooCommerce admin orders list answer code and the used coupon code is shown, but can't include the used coupon description.
How is that possible?
You can coupon object by WC_Coupon class and use $coupon->get_description() to get coupon description. try the below code.
function woo_customer_order_coupon_column_for_orders( $columns ) {
$new_columns = array();
foreach ( $columns as $column_key => $column_label ) {
if ( 'order_total' === $column_key ) {
$new_columns['order_coupons'] = __('coupons', 'woocommerce');
}
$new_columns[$column_key] = $column_label;
}
return $new_columns;
}
add_filter( 'manage_edit-shop_order_columns', 'woo_customer_order_coupon_column_for_orders' );
function woo_display_customer_order_coupon_in_column_for_orders( $column ) {
global $the_order;
if( $column == 'order_coupons' ) {
if( $coupons = $the_order->get_used_coupons() ) {
foreach( $coupons as $coupon_code ){
$coupon = new WC_Coupon($coupon_code);
if( $coupon ){
echo "<span class='coupon-name'><b>".$coupon->code."</b></span>";
echo "<p class='coupon-description'>".$coupon->get_description()."</p>";
}
}
} else {
echo '<small><em>'. __('No Coupon') . '</em></small>';
}
}
}
add_action( 'manage_shop_order_posts_custom_column' , 'woo_display_customer_order_coupon_in_column_for_orders' );
Tested and works

Add a custom "Sale Price" column to admin products list in Woocommerce

I am trying to add a Sale Price column to admin Products list in Woocommerce.
Here is my code:
add_filter( 'manage_edit-product_columns', 'onsale_product_column', 10);
function onsale_product_column($columns){
$new_columns = [];
foreach( $columns as $key => $column ){
$new_columns[$key] = $columns[$key];
if( $key == 'product_cat' ) {
$new_columns['onsale'] = __( 'Sale Price','woocommerce');
}
}
return $new_columns;
}
add_action( 'manage_product_posts_custom_column',
'onsale_product_column_content', 10, 2 );
function onsale_product_column_content( $column, $product_id ){
global $post;
if( $column == 'onsale' ){
if( $product_id->is_on_sale() ) {
echo $product_id->get_sale_price();
}
echo '-';
}
}
But it doesn't work. What I am doing wrong?
There is some mistakes in your codeā€¦ Try the following instead:
add_filter( 'manage_edit-product_columns', 'onsale_product_column', 10);
function onsale_product_column($columns){
$new_columns = [];
foreach( $columns as $key => $column ){
$new_columns[$key] = $columns[$key];
if( $key == 'product_cat' ) {
$new_columns['onsale'] = __( 'Sale Price','woocommerce');
}
}
return $new_columns;
}
add_action( 'manage_product_posts_custom_column', 'onsale_product_column_content', 10, 2 );
function onsale_product_column_content( $column, $post_id ){
if( $column == 'onsale' ){
global $post, $product;
// Excluding variable and grouped products
if( is_a( $product, 'WC_Product' ) && ! $product->is_type('grouped') &&
! $product->is_type('variable') && $product->is_on_sale() ) {
echo strip_tags( wc_price( $product->get_sale_price() ) );
}
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and work.

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

Categories