I am using WooCommerce Customer/Order CSV Export and have a snippet in my functions file (obtained from WooCommerce.
function sv_wc_csv_export_reorder_columns( $column_headers ) {
// remove order total from the original set of column headers, otherwise it will be duplicated
unset( $column_headers['column_1'] );
unset( $column_headers['delivery_date'] );
$new_column_headers = array();
foreach ( $column_headers as $column_key => $column_name ) {
$new_column_headers[ $column_key ] = $column_name;
if ( 'shipping_company' == $column_key ) {
// add order total immediately after order_number
$new_column_headers['column_1'] = 'Contact Name';
}
}
return $new_column_headers;
}
add_filter( 'wc_customer_order_csv_export_order_headers', 'sv_wc_csv_export_reorder_columns' );
The problem as is with the code, it adds column_1 directly after shipping_company (the first field) - how can I set it so that it appears before this column or set it as the first column?
You need simply the following to set or add 'column_1' as first column:
add_filter( 'wc_customer_order_csv_export_order_headers', 'sv_wc_csv_export_reorder_columns', 10, 2 );
function sv_wc_csv_export_reorder_columns( $column_headers, $csv_generator ) {
// Save "column_1" in a new array
$column1 = array('column_1' => __("Contact Name") );
// Remove "column_1" to be reordered
unset( $column_headers['column_1'] );
// Remove unnecessary "delivery_date"
unset( $column_headers['delivery_date'] );
// Set "column_1" as first column merging arrays
return $column1 + $column_headers;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Try this code
function array_insert_after( array $array, $key, array $new ) {
$keys = array_keys( $array );
$index = array_search( $key, $keys );
$pos = false === $index ? count( $array ) : $index + 1;
return array_merge( array_slice( $array, 0, $pos ), $new, array_slice( $array, $pos ) );
}
function sv_wc_csv_export_reorder_columns( $column_headers ) {
// remove order total from the original set of column headers, otherwise it will be duplicated
unset( $column_headers['column_1'] );
unset( $column_headers['delivery_date'] );
$new_column_headers = array();
$new_column_headers['column_1'] = 'Contact Name';
$column_headers = array_insert_after($column_headers, 'shipping_company', $new_column_headers);
return $column_headers;
}
add_filter( 'wc_customer_order_csv_export_order_headers', 'sv_wc_csv_export_reorder_columns' );
Related
I'm trying to sort posts on the Manage Posts page by word count. I have the column set up and the count is displayed correct, stripped of all tags, html, etc. However, once I sort it, the posts-orderby filter still includes the tags. So if I have one post that is 1000 words without tags, but 1100 with tags, the sorting function views it as bigger that the post that is 1050 without tags and 1060 with tags. Is there any way to apply all of the code stripping used in the "//Strips content and counts words" section to the final orderby logic?
// Add Length Column
add_filter('manage_post_posts_columns', function ( $columns )
{
$_columns = [];
foreach( (array) $columns as $key => $label )
{
$_columns[$key] = $label;
if( 'title' === $key )
$_columns['ryu_post_content_length'] = __( 'Length' );
}
return $_columns;
} );
//Strips content and counts words
function fsj_count_content_words( $content ) {
$decode_content = html_entity_decode( $content );
$filter_shortcode = strip_shortcodes( $decode_content );
//removes embedded video and images code
$remove_videos = preg_replace('/<figure class="wp-block-embed is-type-video is-provider-tiktok">(.*?)<\/figure>/s', '', $filter_shortcode);
$remove_images = preg_replace('/<div class="wp-block-chroma-blocks-media-upload none">(.*?)<\/div>/s', '', $filter_shortcode);
//strips the html tags
$strip_tags = wp_strip_all_tags( $remove_images, true );
//counts the words
$count = str_word_count( strip_shortcodes($strip_tags) );
return $count;
}
// Fill Column With Word Counts
add_action( 'manage_post_posts_custom_column', function ( $column_name, $post_id )
{
if ( $column_name == 'ryu_post_content_length')
echo fsj_count_content_words(get_post( $post_id )->post_content);
//echo str_word_count( wp_strip_all_tags( get_post( $post_id )->post_content ) );
}, 10, 2 );
// Make Column Orderable
add_filter( 'manage_edit-post_sortable_columns', function ( $columns )
{
$columns['ryu_post_content_length'] = 'ryu_post_content_length';
return $columns;
} );
// Order Through Proper Filter
add_filter( 'posts_orderby', function( $orderby, WP_Query $q )
{
$_orderby = $q->get( 'orderby' );
$_order = $q->get( 'order' );
if(
is_admin()
&& $q->is_main_query()
&& 'ryu_post_content_length' === $_orderby
&& in_array( strtolower( $_order ), [ 'asc', 'desc' ] )
) {
global $wpdb;
$orderby = " LENGTH( {$wpdb->posts}.post_content ) " . $_order . " ";
}
return $orderby;
}, 10, 2 );
I am using Woocommerce + Gravity forms with the WooCommerce Gravity Forms Product Add-Ons. My form contains a Nested Gravity Perks Form. I need to add the Woocommerce Order number to both the parent and the child gravity form. I have accomplished this with the following code:
add_action( 'woocommerce_checkout_order_processed', 'wc_add_order_id' );<br>function wc_add_order_id( $order_id ) {
$order = wc_get_order( $order_id );
foreach ( $order->get_items() as $item ){
$product_id = $item['product_id'];
if ( has_term( 'camp', 'product_cat', $product_id ) ) { //my woocommerce category
$meta_data = $item->get_formatted_meta_data();
$meta_data_items = $item->get_meta_data();
foreach ( $meta_data_items as $meta ) {
$entry_id = $meta->value['_gravity_form_linked_entry_id'];
$entry = GFAPI::get_entry( $entry_id );
$entry['40'] = $order_id ; //40 is the id of a single field reserved for the Order #
$result = GFAPI::update_entry( $entry );
return $result;
}
}
}
};
I then tried the following to add the order number to my child form (this works if I manually update the parent form entry. It does not work automatically from the update_entry trigger above:
add_action( 'gform_after_update_entry_82', function ( $form, $entry_id ) { //82 is the parent form id<br>
$entry = GFAPI::get_entry( $entry_id );
$entry_child_order = rgar( $entry, '2' ); //2 is the field that contains the entry id of the child
$order_id = rgar( $entry, '40' ); //this has the Woocommerce Order #
$entry2 = GFAPI::get_entry( $entry_child_order);
$entry2['147'] = $order_id ; //147 is the single field reserved for the order number
$result = GFAPI::update_entry( $entry2 );
return $result;
}, 10, 2 );
Does anyone know why "GFAPI::update_entry( $entry );" doesn't automatically trigger the second half of this code?
Ok. I figured it out. I was able to put everything into the same function and it is working.
//add order number to gravity forms
add_action( 'woocommerce_checkout_order_processed', 'wc_add_order_id' );
function wc_add_order_id( $order_id ) {
$order = wc_get_order( $order_id );
$cart_reg = $order->get_items();
$entry_id = array();
$entry_id2 = array();
$entry_id[] = $linked_entry;
$entry_id2[] = $linked_nested_value;
foreach( $cart_reg as $key => $value) {
$linked_entry=$value->get_meta( '_gravity_forms_history')["_gravity_form_linked_entry_id"];
$entry_id = $linked_entry;
$entry = GFAPI::get_entry( $entry_id );
$entry['40'] = $order_id; //40 is my field number that will contain orede number
$result = GFAPI::update_entry( $entry );
$linked_nested_value=$value->get_meta( '_gravity_forms_history')["_gravity_form_lead"]['2'];
if(!$linked_nested_value == ''){
$nested_value_array = preg_split ("/\,/", $linked_nested_value); //array of child entries
$child_entry_amt = substr_count($linked_nested_value, ",") + 1;
}//child entries
if ($child_entry_amt > 0){
for ($n = 0; $n < $child_entry_amt; $n++) {
$entry_id2=$nested_value_array[$n];
$entry2 = GFAPI::get_entry( $entry_id2 );
$entry2['147'] = $order_id;//this is my field number on my child form that will contain order number
$result2 = GFAPI::update_entry( $entry2 );
}
}
}
};
I'm currently trying to modify the name column in the WordPress admin dashboard. I've tried this code here but it's not working:
add_action('manage_users_custom_column', 'modify_users_column_content', 10, 3 );
function modify_users_column_content( $value, $column_name, $user_id ) {
if ( $column_name === 'name' ) {
$value .= '<span> |</span>';
}
return $value;
}
When I error_log the column_name parameter, I get only the last two columns from the user management plugin UltimateMember:
The first columns are not within the array. I've tried to understand it but no chance. I don't get it.
The first columns are not within the array. I've tried to understand
it but no chance. I don't get it.
Because the manage_users_custom_column filter is meant to be used to generate the output of a custom column and not default columns like the "Name" column.
However, you can achieve what you want by replacing the default "Name" column (keyed name) with a custom one like so:
add_filter( 'manage_users_columns', function( $columns ){
$columns2 = [];
// We could do $columns['name2'] = 'Name'; - but we are replacing a column.
foreach ( $columns as $key => $label ) {
if ( 'name' === $key )
$columns2['name2'] = 'Name';
else
$columns2[ $key ] = $label;
}
return $columns2;
} );
And then use the manage_users_custom_column filter to generate the output which is displayed in the custom column (name2):
add_filter( 'manage_users_custom_column', function( $output, $column_name, $user_id ){
if ( 'name2' === $column_name ) {
$user_object = get_userdata( $user_id );
$name = trim( $user_object->first_name . ' ' . $user_object->last_name );
$output = $name ? $name . '<span> |</span>' : '—'; // the custom output
}
return $output;
}, 10, 3 );
I'm trying to add meta data to each product when an order has been created by using the woocommerce_checkout_create_order_line_item.
However, I can't seem to access the ID of the order.
I've used print_r($order) and can see the order details in there but I can't see the ID of the order within the object. Is this because it hasn't been generated yet?
add_action('woocommerce_checkout_create_order_line_item', array($this, 'ticket_meta_to_line_item'), 20, 4 );
function ticket_meta_to_line_item( $item, $cart_item_key, $values, $order )
{
$_p = $item->get_product();
$key = 'Draw #';
$order_id = $order->id;
error_log( print_r( $order, true ) );
if ( false !== ( $value = $_p->get_meta( $key, true ) ) )
{
$numbers = $this->add_tickets_to_order_meta($order_id, $order->get_user_id(), $_p->id);
error_log( print_r( $numbers, true ) );
$item->add_meta_data( $key , 1 , true );
}
}
If you wondering to add meta data, then there is no need to find the Order_ID, from below code you can easily do so.
function _woocommerce_add_order_item_meta_new_ver($item,$cart_key,$values) {
//HERE product_meta is just a random key I have used here, you have to use your key here
if (isset ( $values ['product_meta'] )) {
foreach ( $values ['product_meta'] as $key => $val ) {
$order_val = stripslashes( $val );
if($val) {
if($key == 'your_cart_item_key') {
$item->add_meta_data('Your Key',$order_val);
}
}
}
}
}
//This will add "Your Key" in your order_item_meta, just make sure you have used the same key "your_cart_item_key" in your cart_item_meta key too.
You can access order id using below code.
$order_id = $order->get_order_number();
Tested and works well
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 );