WooCommerce: Moving column position in admin product list - php

I have created a custom column "Cost" in Backend WooCommerce Products list panel using a hook in functions.php file.
I want to move that column after "Price" column:
How can I achieve this?

There is many ways to achieve this. I have chosen the simplest way to do it. You can use the code snippet below to change "Cost" column location before "price" column, in product post_type backend list:
// ADDING A CUSTOM COLUMN TITLE TO ADMIN PRODUCTS LIST AFTER "Price" COLUMN
add_filter( 'manage_edit-product_columns', 'custom_product_column',11);
function custom_product_column($columns)
{
$new_columns = array();
foreach( $columns as $key => $column ){
$new_columns[$key] = $columns[$key];
if( $key === 'price' )
$new_columns['cost'] = __( 'Cost','woocommerce');
}
return $new_columns;
}
// ADDING THE DATA FOR EACH PRODUCTS BY COLUMN (EXAMPLE)
add_action( 'manage_product_posts_custom_column' , 'custom_product_list_column_content', 10, 2 );
function custom_product_list_column_content( $column, $product_id )
{
global $post;
switch ( $column )
{
case 'cost' :
// Get and display the value for each row
echo get_post_meta( $product_id, '_cost', true );
break;
}
}
Tested and works.

Use the add_filter(manage_edit-posttype_columns, yourFunction) where posttype is your post type. Then list your columns in yourFunction the order you want. The best tutorial I've seen on this is here.

Related

Display the transaction ID from order notes in a custom column on Woocommerce admin Orders list

Based on "Display back Order Notes in Admin orders list on WooCommerce 3.3" answer code, that adds in a custom column the order notes in a tooltip, I am trying to figure out how to get the correct data from the "Order Notes" where it shows the transaction ID to display it in a custom column in admin orders list.
Because what this linked answer code is displaying a little icon in a column that when you hover over it shows you the last order note.
All i'm wanting to populate in the column is the transaction ID from the order note.
Updated - You can simply display the transaction ID using the WC_Order get_transaction_id() method like:
echo $the_order->get_transaction_id();
So your code hooked functions will be:
// Add a custom column before "actions" last column
add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column', 100 );
function custom_shop_order_column( $columns ){
$ordered_columns = array();
foreach( $columns as $key => $column ){
$ordered_columns[$key] = $column;
if( 'order_date' == $key ){
$ordered_columns['transaction_id'] = __( 'Transaction id', 'woocommerce');
}
}
return $ordered_columns;
}
add_action( 'manage_shop_order_posts_custom_column', 'custom_shop_order_list_column_content', 10, 1 );
function custom_shop_order_list_column_content( $column ) {
global $post, $the_order;
if ( 'transaction_id' === $column ) {
echo $the_order->get_transaction_id();
}
}
Code goes in function.php file of your active child theme (or active theme). It should work.

Displaying product custom fields values in the order once processed

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

Add columns to admin orders list in WooCommerce

I am using WooCommerce plugin for one of my ecommerce WordPress websites. I want to add some columns to my order listing page in the WooCommerce admin area. I am not able to find out where to add that.
Can anyone advise which template page I need to amend in order to meet my requirement?
Updated: 2018-03-30 - added positioning feature to the new columns
So you if you want to add some columns in the orders Admin list page (in backend):
ADDING COLUMNS IN WOOCOMMERCE ADMIN ORDERS LIST
In the example below, we add 2 new custom columns, before existing "Total" and "Actions" columns.
// 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', 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'] = __( 'Title1','theme_domain');
$reordered_columns['my-column2'] = __( 'Title2','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' :
// Get custom post meta data
$my_var_one = get_post_meta( $post_id, '_the_meta_key1', 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-column2' :
// Get custom post meta data
$my_var_two = get_post_meta( $post_id, '_the_meta_key2', true );
if(!empty($my_var_two))
echo $my_var_two;
// Testing (to be removed) - Empty value case
else
echo '<small>(<em>no value</em>)</small>';
break;
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Related answer (for products): Add custom columns to admin producs list in WooCommerce backend

Woocommerce Admin Order Details - Show custom data on order details page

I'm searching and trying it for 2 days with no success, please help.
I want to filter woocommerce orders to add additional details from db to order details page based on product attribute but I can't find the right woocommerce action/filter hook for this task.
Here suppose I've variable $is_customized = false;
If $is_customized == true then I need to add custom data from database to orders detail page.
NOTE: I don't want to add additional meta box instead I want to change order detail table for:
Replacing the default Product image with the image stored in database
and,
Adding a div containing custom attributes below product name.
I've all these values in my variables but I can't figure out which action hook should I use.
I've attached an image for clarification.
Just need to know if I can change / filter these order results and how ?
I appreciate for your time and help.
Thanks
Here's a start on how to display some extra data on the woocommerce_before_order_itemmeta hook:
add_action( 'woocommerce_before_order_itemmeta', 'so_32457241_before_order_itemmeta', 10, 3 );
function so_32457241_before_order_itemmeta( $item_id, $item, $_product ){
echo '<p>bacon</p>';
}
I don't know how you are saving your data, so I can't make more a more precise suggestion. Keep in mind that immediately following that hook, anything you've saved as order item meta will automatically display.
Filtering the image is more difficult. I've found this gist as a start, but it requires some custom conditional logic as you don't want to filter the thumbnail everywhere, but only in orders.
Edit: Currently the best I can do for filtering the item thumbnails:
add_filter( 'get_post_metadata', 'so_32457241_order_thumbnail', 10, 4 );
function so_32457241_order_thumbnail( $value, $post_id, $meta_key, $single ) {
// We want to pass the actual _thumbnail_id into the filter, so requires recursion
static $is_recursing = false;
// Only filter if we're not recursing and if it is a post thumbnail ID
if ( ! $is_recursing && $meta_key === '_thumbnail_id' ) {
$is_recursing = true; // prevent this conditional when get_post_thumbnail_id() is called
$value = get_post_thumbnail_id( $post_id );
$is_recursing = false;
$value = apply_filters( 'post_thumbnail_id', $value, $post_id ); // yay!
if ( ! $single ) {
$value = array( $value );
}
}
return $value;
}
add_filter( 'post_thumbnail_id', 'so_custom_order_item_thumbnail', 10, 2 );
function so_custom_order_item_thumbnail( $id, $post_id ){
if( is_admin() ){
$screen = get_current_screen();
if( $screen->base == 'post' && $screen->post_type == 'shop_order' ){
// this gets you the shop_order $post object
global $post;
// no really *good* way to check post item, but could possibly save
// some kind of array in the order meta
$id = 68;
}
}
return $id;
}

Add a Custom Field to WooCommerce Order Items

Currently, WooCommerce Order Items within an order, have the following columns:
Item
Tax Class
Qty
Totals
Tax
See screenshot
What I'm aiming to do is add an extra column (or meta) which has a dropdown field for Product Status.
Does anyone have any ideas on how I might accomplish this?
In case someone else stumbles upon this, I needed to add some custom order meta to the order items meta box on the edit order screen. In 2017 this is how I solved this dilemma.
The file class-wc-meta-box-order-items.php, found under includes/admin/meta-boxes has changed slightly since 2014 but it does include the template file html-order-items.php.
It's in that last file that you will find two undocumented hooks, woocommerce_admin_order_item_headers, which you would use to add your custom column heading text and has access to the $order object and woocommerce_admin_order_item_values, which places your custom content right before the Cost column and has access to $product, $item and $item_id.
So to add a custom column it would look something like this.
add_action( 'woocommerce_admin_order_item_headers', 'pd_admin_order_items_headers' );
function pd_admin_order_items_headers($order){
?>
<th class="line_customtitle sortable" data-sort="your-sort-option">
Custom Title
</th>
<?php
}
where your-sort-option depends on what data you are wanting to sort. I used string-ins in my situation.
Than for the content in each line item you would have.
add_action( 'woocommerce_admin_order_item_values', 'pd_admin_order_item_values' );
function pd_admin_order_item_values( $product, $item, $item_id ) {
//Get what you need from $product, $item or $item_id
?>
<td class="line_customtitle">
<?php //your content here ?>
</td>
<?php
}
There are a few other hooks and filters in that template file that are definitely worth looking at if you need content in different places within that meta-box.
I am in the middle of some major adjustments to this table as well, and I haven't figured it all out yet, but I know this much, if you review the class-wc-meta-box-order-items.php within the plugin directory, you will find this snippet of code:
// List order items
$order_items = $order->get_items( apply_filters( 'woocommerce_admin_order_item_types', array( 'line_item', 'fee' ) ) );
foreach ( $order_items as $item_id => $item ) {
switch ( $item['type'] ) {
case 'line_item' :
$_product = $order->get_product_from_item( $item );
$item_meta = $order->get_item_meta( $item_id );
include( 'views/html-order-item.php' );
break;
case 'fee' :
include( 'views/html-order-fee.php' );
break;
}
do_action( 'woocommerce_order_item_' . $item['type'] . '_html', $item_id, $item );
}
DO NOT EDIT THIS SNIPPET!!!
From here you can begin to see what makes up that table. I believe (from what I know of WooCommerce so far) is you can create a function that hooks in at the filter there: woocommerce_admin_order_item_types();
That's what I would do something like this:
# Admin Panel Updates
add_filter( 'woocommerce_add_order_item_meta', array( $this, 'display_order_item_meta' ), 10, 2 );
public function display_order_item_meta( $order_items ) {
array_push($order_items,'event'); //Not sure how to manipulate this yet
return $order_items;
}
Also it looks like we would need to do something with the action hook (woocommerce_order_item_' . $item['type'] . '_html', $item_id, $item), perhaps something along these lines:
add_filter( 'woocommerce_order_item_event_html', array( $this, 'display_event_item_meta' ), 10, 2 );
public function display_event_item_meta( $item_id, $item) {
switch ( $item['type'] ) {
case 'event' :
include( 'views/html-order-event-item.php' );
break;
}
}
That's my best guess so far, but I'm positive this is the right snippet of code to be dissecting.

Categories