In the Woocommerce customer account, I'd like to remove the following columns:
Order number
Order total
And add a column to display attached files (thanks to the WooCommerce Attach Me! plugin). The developer gave me this information:
"To get all the order attachments use the following snippet:"
global $wcam_order_model;
$order_attachments = $wcam_order_model->get_attachments_metadata($order_id , array());
Where $order_id is the order id for which you want to retreive attachments. Then to get each attachment title use the following shippet:
foreach($order_attachments as $order_attachment)
$file_name = basename($order_attachment['absolute_path']);
I have found a tutorial here: https://www.skyverge.com/blog/adding-columns-woocommerce-orders-list/
But I can't get it work. As you imagine, I am not a developer :/
Thank you very much!
Put this in your functions.php
function sv_wc_add_my_account_orders_column( $columns ) {
$new_columns = array();
foreach ( $columns as $key => $name ) {
if( 'order-number' === $key || 'order-total' === $key ) {
echo ' name '.$name . ' key ' . $key ;
continue;
}
$new_columns[ $key ] = $name;
/*if( 'order-date' === $key) {
$new_columns['order-number'] = __( 'Order', 'textdomain' );
$new_columns['order-rental-period'] = __( 'Rental Period', 'textdomain' );
$new_columns['order-detail'] = __( 'Order or Quotation Detail', 'textdomain' );
}*/
}
add_filter( 'woocommerce_my_account_my_orders_columns', 'sv_wc_add_my_account_orders_column' );
return $new_columns;
}
Related
I created a custom field for the ecommerce admin order item meta. Everything is fine.
I would like to display the Custom Fields MetaValue on My Account's Order Details page. But nothing is being displayed. Based on Save Order item custom field in Woocommerce Admin order pages answer code, this is my attempt
function add_order_item_custom_field( $item_id, $item ) {
woocommerce_wp_text_input( array(
'id' => 'v_number'.$item_id,
'label' => __( 'V Number : ', 'ctxt' ),
'description' => __( 'Enter the title of your custom text field.', 'ctxt' ),
'desc_tip' => true,
'class' => 'v_number_class',
'value' => wc_get_order_item_meta( $item_id, '_v_number' ),
) );
}
add_action( 'woocommerce_before_order_itemmeta', 'add_order_item_custom_field', 10, 2 );
// Save the custom field value
function save_order_item_custom_field_value( $post_id, $post ){
$order = wc_get_order( $post_id );
foreach ( $order->get_items() as $item_id => $item ) {
if( isset( $_POST['v_number'.$item_id] ) ) {
$item->update_meta_data( '_v_number', sanitize_text_field( $_POST['v_number'.$item_id] ) );
$item->save();
}
}
$order->save();
}
add_action('save_post', 'save_order_item_custom_field_value' );
// Display meta my account view order page
printf (
'<p><a>V Number : <strong>' . $order->get_meta('_v_number') . '</strong></a></p>'
);
it should print your custom order meta value.
add_action( 'woocommerce_view_order', 'print_custom_order_meta' );
function print_custom_order_meta( $order_id ){
$order = wc_get_order($order_id);
foreach( $order->get_items() as $item ) {
echo 'V Number for '. $item->get_name() .' - ' . $item->get_meta( '_v_number', true ) . '<br>';
}
}
edit
for need to show metavalue after each product item you need to hook with a different action like this.
add_action('woocommerce_order_item_meta_end', 'show_order_meta', 11, 3);
function show_order_meta( $item_id, $item, $order ) {
echo '<br>V Number for '. $item->get_name() .' - ' . $item->get_meta( '_v_number', true ) . '<br>';
}
In WooCommerce, I would like to add a new column to the "My Account" orders table and show the order details.
I have this code, which adds the column, but the values don't show (e.g. get_formatted_meta_data).
Can anyone help rewrite the code to make it work?
function wc_add_my_account_orders_column( $columns ) {
$new_columns = array();
foreach ( $columns as $key => $name ) {
$new_columns[ $key ] = $name;
if ( 'order-status' === $key ) {
$new_columns['order-details'] = __( 'Order details', 'textdomain' );
}
}
return $new_columns;
}
add_filter( 'woocommerce_my_account_my_orders_columns', 'wc_add_my_account_orders_column' );
function wc_my_orders_order_details_column( $order ) {
$order_details = get_post_meta( $order->get_id(), 'order_details', true );
echo ! empty( $order_details ) ? $order_details : '–';
}
add_action( 'woocommerce_my_account_my_orders_column_order_details', 'wc_my_orders_order_details_column' );
The woocommerce_my_account_my_orders_columns filter has been deprecated since WooCommerce 2.6.0. So although it still functions you should use the woocommerce_account_orders_columns filter to add an additional column.
To populate the column with your data you can use the woocommerce_my_account_my_orders_column_ action which expects you to append the column ID of your custom column. So in your case that would be order-details. In your example you've used order_details (with an underscore instead of a hyphen). That is why your data doesn't show up.
Also it is best practice to retrieve order meta data via the internal WooCommerce getter function get_meta() instead of using get_post_meta().
add_filter( 'woocommerce_account_orders_columns', 'wc_add_my_account_orders_column', 10, 1 );
function wc_add_my_account_orders_column( $columns ) {
$new_columns = array();
foreach ( $columns as $key => $name ) {
$new_columns[ $key ] = $name;
if ( 'order-status' === $key ) {
$new_columns['order-details'] = __( 'Order details', 'textdomain' );
}
}
return $new_columns;
}
add_action( 'woocommerce_my_account_my_orders_column_order-details', 'wc_my_orders_order_details_column', 10, 1 );
function wc_my_orders_order_details_column( $order ) {
$item_meta = '';
foreach ( $order->get_items() as $item ) {
$item_meta .= wc_display_item_meta( $item, array( 'echo' => false ) );
}
echo !empty( $item_meta ) ? $item_meta : '-';
}
I am trying to find a function and shortcode to display the total amount of products sold as simply as possible on our website.
Don't want to show product names or any info just a count.
I found this function / shortcode that displays total orders. Just wondering fo anyone can help adapt it to display total products sold?
Thanks so much..
function display_woocommerce_order_count( $atts, $content = null ) {
$args = shortcode_atts( array(
'status' => 'completed',
), $atts );
$statuses = array_map( 'trim', explode( ',', $args['status'] ) );
$order_count = 0;
foreach ( $statuses as $status ) {
// if we didn't get a wc- prefix, add one
if ( 0 !== strpos( $status, 'wc-' ) ) {
$status = 'wc-' . $status;
}
$order_count += wp_count_posts( 'shop_order' )->$status;
}
ob_start();
echo number_format( $order_count );
return ob_get_clean();
}
add_shortcode( 'wc_order_count', 'display_woocommerce_order_count' );
I have added a custom column in Woocommerce admin side order listing page to display Ordered item Products name and total Quantity like this -> [http://prntscr.com/p11rdl] by using the following code:
add_filter('manage_edit-shop_order_columns', 'misha_order_items_column' );
function misha_order_items_column( $order_columns ) {
$order_columns['order_products'] = "Qty-Products Name-SKU";
return $order_columns;
}
add_action( 'manage_shop_order_posts_custom_column' , 'misha_order_items_column_cnt' );
function misha_order_items_column_cnt( $colname ) {
global $the_order; // the global order object
if( $colname == 'order_products' ) {
// get items from the order global object
$order_items = $the_order->get_items();
if ( !is_wp_error( $order_items ) ) {
foreach( $order_items as $order_item ) {
echo $order_item['quantity'] .' × '. $order_item['name'] .'<br />';
}
}
}
}
Now I also want to display ordered product SKU number after the product name. So anyone have solution for this then please provide help me.
Thank you,
Ketan
Just replace with follows code snippets to add SKU -
add_action( 'manage_shop_order_posts_custom_column' , 'misha_order_items_column_cnt' );
function misha_order_items_column_cnt( $colname ) {
global $the_order; // the global order object
if( $colname == 'order_products' ) {
// get items from the order global object
$order_items = $the_order->get_items();
if ( !is_wp_error( $order_items ) ) {
foreach( $order_items as $order_item ) {
$product = $order_item->get_product();
// product checking
$sku = ( $product && $product->get_sku() ) ? ' - ' . $product->get_sku() : '';
echo $order_item['quantity'] .' × '. $order_item['name'] . ''. $sku . '<br />';
}
}
}
}
Thank you so much I've found this thread extremely helpful, I couldn't thank the original poster enough as well the help from #itzmekhokan and various other contributors, this community is awesome.
What I'm needing now is to add product shelve location straight after SKU.
I have the below code for location fields but not sure how to amend to the code above to make it work. I'm new to coding in general, but have found snippets of code and made it work so far.
Your help and guidance would be greatly appreciated.
// Add Location field
add_action( 'woocommerce_product_options_stock_fields', 'product_shelf' );
function product_shelf() {
global $woocommerce, $post;
woocommerce_wp_text_input(
array(
'id' => 'product_shelf',
'placeholder' => 'Enter product shelf location here',
'label' => 'Shelf Location',
'description' => 'Shelf location of product',
'desc_tip' => 'true',
)
);
}
// Save location data
add_action( 'woocommerce_process_product_meta', 'product_shelf_data' );
function product_shelf_data( $post_id ) {
// grab the location from $_POST
$product_shelf = isset( $_POST[ 'product_shelf' ] ) ? sanitize_text_field( $_POST[ 'product_shelf' ] ) : '';
// grab the product
$product = wc_get_product( $post_id );
// save the location using WooCommerce built-in functions
$product->update_meta_data( 'product_shelf', $product_shelf_data );
$product->save();
}
I am using the following code to add new stock statuses in WooCommerce 4+
The new statuses are:
Preorder
Contact us
function add_custom_stock_type() {
?>
<script type="text/javascript">
jQuery(function(){
jQuery('._stock_status_field').not('.custom-stock-status').remove();
});
</script>
<?php
woocommerce_wp_select( array( 'id' => '_stock_status', 'wrapper_class' => 'custom-stock-status', 'label' => __( 'Stock status', 'woocommerce' ), 'options' => array(
'instock' => __( 'Available', 'woocommerce' ), //changed the name
'outofstock' => __( 'Sold out', 'woocommerce' ), //changed the name
'onbackorder' => __( 'Preorder : Pending Distributor release', 'woocommerce' ), //changed the name
'contact' => __( 'Contact us for Availability', 'woocommerce' ), //added new one
'preorder' => __( 'On Preorder: Pending Distributor release', 'woocommerce' ), //added new one
), 'desc_tip' => true, 'description' => __( 'Controls whether or not the product is listed as "in stock" or "out of stock" on the frontend.', 'woocommerce' ) ) );
}
add_action('woocommerce_product_options_stock_status', 'add_custom_stock_type');
function save_custom_stock_status( $product_id ) {
update_post_meta( $product_id, '_stock_status', wc_clean( $_POST['_stock_status'] ) );
}
add_action('woocommerce_process_product_meta', 'save_custom_stock_status',99,1);
function woo_add_custom_general_fields_save_two( $post_id ){
// Select
$woocommerce_select = $_POST['_stock_status'];
if( !empty( $woocommerce_select ) )
update_post_meta( $post_id, '_stock_status', esc_attr( $woocommerce_select ) );
else
update_post_meta( $post_id, '_stock_status', '' );
}
function woocommerce_get_custom_availability( $data, $product ) {
switch( $product->stock_status ) {
case 'instock':
$data = array( 'availability' => __( 'Available', 'woocommerce' ), 'class' => 'in-stock' ); //changed name
break;
case 'outofstock':
$data = array( 'availability' => __( 'Sold Out', 'woocommerce' ), 'class' => 'out-of-stock' ); //changed name
break;
case 'onbackorder':
$data = array( 'availability' => __( 'Preorder : Pending Distributor release', 'woocommerce' ), 'class' => 'onbackorder' ); //changed name
break;
case 'contact':
$data = array( 'availability' => __( 'Contact us for Availability', 'woocommerce' ), 'class' => 'contact' ); //added new one
break;
case 'preorder':
$data = array( 'availability' => __( 'On Preorder : Pending Distributor release', 'woocommerce' ), 'class' => 'preorder' ); //added new one
break;
}
return $data;
}
add_action('woocommerce_get_availability', 'woocommerce_get_custom_availability', 10, 4);
Works:
Backend: The new status is added in the dropdown menu, I can select the status I want
Does not work:
Front end: on single product page is not showing the correct status
Backend: Display the new status on the admin products list table
Someone who can assist me with this?
Last update: 04/22 - Tested in WordPress 5.9.2 & WooCommerce 6.3.1
Code goes in functions.php file of the active child theme (or active theme).
Use woocommerce_product_stock_status_options
instead of woocommerce_product_options_stock_status.
This way you can immediately add a status instead of replace the existing dropdown
Also use woocommerce_get_availability_text & woocommerce_get_availability_class
opposite woocommerce_get_availability.
This way you don't have to add the existing statuses again
// Add new stock status options
function filter_woocommerce_product_stock_status_options( $status ) {
// Add new statuses
$status['pre_order'] = __( 'Pre order', 'woocommerce' );
$status['contact_us'] = __( 'Contact us', 'woocommerce' );
return $status;
}
add_filter( 'woocommerce_product_stock_status_options', 'filter_woocommerce_product_stock_status_options', 10, 1 );
// Availability text
function filter_woocommerce_get_availability_text( $availability, $product ) {
// Get stock status
switch( $product->get_stock_status() ) {
case 'pre_order':
$availability = __( 'Pre order', 'woocommerce' );
break;
case 'contact_us':
$availability = __( 'Contact us', 'woocommerce' );
break;
}
return $availability;
}
add_filter( 'woocommerce_get_availability_text', 'filter_woocommerce_get_availability_text', 10, 2 );
// Availability CSS class
function filter_woocommerce_get_availability_class( $class, $product ) {
// Get stock status
switch( $product->get_stock_status() ) {
case 'pre_order':
$class = 'pre-order';
break;
case 'contact_us':
$class = 'contact-us';
break;
}
return $class;
}
add_filter( 'woocommerce_get_availability_class', 'filter_woocommerce_get_availability_class', 10, 2 );
Use woocommerce_admin_stock_html to display the new stock status on the admin products list table
// Admin stock html
function filter_woocommerce_admin_stock_html( $stock_html, $product ) {
// Simple
if ( $product->is_type( 'simple' ) ) {
// Get stock status
$product_stock_status = $product->get_stock_status();
// Variable
} elseif ( $product->is_type( 'variable' ) ) {
foreach( $product->get_visible_children() as $variation_id ) {
// Get product
$variation = wc_get_product( $variation_id );
// Get stock status
$product_stock_status = $variation->get_stock_status();
/*
Currently the status of the last variant in the loop will be displayed.
So from here you need to add your own logic, depending on what you expect from your custom stock status.
By default, for the existing statuses. The status displayed on the admin products list table for variable products is determined as:
- Product should be in stock if a child is in stock.
- Product should be out of stock if all children are out of stock.
- Product should be on backorder if all children are on backorder.
- Product should be on backorder if at least one child is on backorder and the rest are out of stock.
*/
}
}
// Stock status
switch( $product_stock_status ) {
case 'pre_order':
$stock_html = '<mark class="pre-order" style="background:transparent none;color:#33ccff;font-weight:700;line-height:1;">' . __( 'Pre order', 'woocommerce' ) . '</mark>';
break;
case 'contact_us':
$stock_html = '<mark class="contact-us" style="background:transparent none;color:#cc33ff;font-weight:700;line-height:1;">' . __( 'Contact us', 'woocommerce' ) . '</mark>';
break;
}
return $stock_html;
}
add_filter( 'woocommerce_admin_stock_html', 'filter_woocommerce_admin_stock_html', 10, 2 );
Optional: if desired, the custom stock status can be used in hooks where you already have access to the $product object or you can use global $product.
1) No access to the $product object, use global $product as is the case for example with the woocommerce_shop_loop_item_title or the woocommerce_single_product_summary hook
function woocommerce_my_callback() {
// An example based on global $product
// Get the global product object
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Get stock status
$product_stock_status = $product->get_stock_status();
// Use this line during testing, delete afterwards!
echo '<p style="color:red;font-size:20px;">DEBUG INFORMATION = ' . $product_stock_status . '</p>';
// Compare
if ( $product_stock_status == 'My custom stock status' ) {
// Etc..
}
}
}
add_action( 'woocommerce_shop_loop_item_title', 'woocommerce_my_callback', 10 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_my_callback', 10 );
2) Access to the $product object, because it's passed by default to the callback function. As is the case for example with the woocommerce_get_price_html hook
function filter_woocommerce_get_price_html( $price, $product ) {
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Get stock status
$product_stock_status = $product->get_stock_status();
// Use this line during testing, delete afterwards!
echo '<p style="color:red;font-size:20px;">DEBUG INFORMATION = ' . $product_stock_status . '</p>';
// Compare
if ( $product_stock_status == 'My custom stock status' ) {
// Etc..
// $price .= ' my text';
}
}
return $price;
}
add_filter( 'woocommerce_get_price_html', 'filter_woocommerce_get_price_html', 10, 2 );
In addition to filters provided by 7uc1f3r woocommerce_product_export_product_column_stock_status filter is required to display custom stock status in exported products CSV file:
function add_custom_stock_csv_data( $_, $product ) {
$status = $product->get_stock_status( 'edit' );
switch( $status ) {
case 'pre_order':
case 'contact_us':
return $status;
case 'onbackorder':
return 'backorder';
case 'instock':
return 1;
default:
return 0;
}
}
add_filter( 'woocommerce_product_export_product_column_stock_status', 'add_custom_stock_csv_data', 10, 2 );