Woocommerce: Disable Date on Edit Orders

WooCommerce: Disable Date on Edit Orders

This worked for me... Create a new js file and equeue it admin panel

/**
* Enqueue a script in the WordPress admin, excluding edit.php.
*
* @param int $hook Hook suffix for the current admin page.
*/
function wpdocs_selectively_enqueue_admin_script( $hook ) {
// if ( 'edit.php' != $hook ) {
// return;
// }
wp_enqueue_script( 'my_custom_woocommerce_script', get_template_directory_uri() . '/woocommerce/assets/meta-boxes-order.js', array(), '1.0' );
}
add_action( 'admin_enqueue_scripts', 'wpdocs_selectively_enqueue_admin_script' );

In the js file add this below code

jQuery(document).ready(function(){
alert("ok"); // only for testing purpose that this file is loaded
jQuery(".order_data_column_container .order_data_column p:eq(0) input").attr('disabled', true);
});

Sample Image

Enable and disable 'edit' on orders for shop manager (woocommerce)

Use get_current_user(), this way you can get user object then check user's role from the object and if it is shop manager then apply your code as you mentioned.

add_filter( 'woocommerce_register_post_type_shop_order','your_function_name' );
function your_function_name($fields) {
$user = wp_get_current_user();
if ( in_array( 'shop_manager', (array) $user->roles ) ) {
$fields['capabilities'] = array(
'create_posts' => false,
);
return $fields;
} else {
return $fields;
}
}

How to lock WooCommerce date created on administration area

I solved my problem with this simple CSS to disable that date on order backend...

.date-picker {pointer-events: none}

Disabling the editing of the order shipping details by admin in WooCommerce backend

There is not immediately a filter to adjust this, so you could use some jQuery, to hide the edit icon.

  • Only on order edit page
  • Checks for user role, administrator
  • Based on one or more order statuses

Important: Because no direct distinction is made between "billing details" and "shipping details" contains the H3 selector a part of the title

$( "h3:contains('Shipping') .edit_address" );

Where 'shipping' may need to be replaced by the title in the language that you use.

  • :contains() Selector

So you get:

function action_admin_footer () {
global $pagenow;

// Only on order edit page
if ( $pagenow != 'post.php' || get_post_type( $_GET['post'] ) != 'shop_order' ) return;

// Get current user
$user = wp_get_current_user();

// Safe usage
if ( ! ( $user instanceof WP_User ) ) {
return;
}

// In array, administrator role
if ( in_array( 'administrator', $user->roles ) ) {
// Get an instance of the WC_Order object
$order = wc_get_order( get_the_id() );

// Is a WC_Order
if ( is_a( $order, 'WC_Order' ) ) {
// Get order status
$order_status = $order->get_status();

// Status in array
if ( in_array( $order_status, array( 'pending', 'on-hold', 'processing' ) ) ) {
?>
<script>
jQuery( document ).ready( function( $ ) {
// IMPORTANT: edit H3 tag contains 'Shipping' if necessary
$( "h3:contains('Shipping') .edit_address" ).hide();
});
</script>
<?php
}
}
}
}
add_action( 'admin_footer', 'action_admin_footer', 10, 0 );

Enable 'Read only' on order page (WooCommerce) for order created & customer email for shop manager only

First create js file in your active parent/child theme

Add the code:

jQuery(function($) {
$('.order_data_column .date-picker').attr("disabled", true);
$('.order_data_column .hour').attr("disabled", true);
$('.order_data_column .minute').attr("disabled", true);
$('.wc-customer-user select').attr("disabled", true);
});

In your functions.php file add this:

function prevent_editing_shop_order_fields( $current_screen ) {
$user = wp_get_current_user();
if ( 'shop_order' == $current_screen->post_type && 'post' === $current_screen->base && isset( $user->roles[0] ) && $user->roles[0] == 'shop_manager' ) {
// Fix accordingly with your file
wp_enqueue_script('myscript', get_stylesheet_directory_uri().'/assets/js/scripts.js',array('jquery'));
}
}
add_action( 'current_screen', 'prevent_editing_shop_order_fields' );

$user->roles[0] == 'shop_manager' - is a condition to check if current user is shop_manager role

Change Woocommerce Admin Order List Time and Date Format

As far as I can read, there is a filter that hooks the date format: woocommerce_admin_order_date_format - https://github.com/woocommerce/woocommerce/blob/a7d57a248ed03dac7bad119e71f83dd961f43cb3/includes/admin/list-tables/class-wc-admin-list-table-orders.php but it fires only if the order is older than 1 day.

Maybe that's why is not working.

Another elegant approach, without touching any template file would be to remove the native WooCommerce date columns and add a custom one.

Here is a code snippet (untested) you can paste in your functions.php file.

add_filter( 'manage_edit-shop_order_columns', 'so61375632_add_new_order_admin_column' );
function so61375632_add_new_order_admin_column( $columns ) {

unset( $columns['order_date'] ); // remove the default column

$edit_columns = array_splice( $columns, 0, 2 );

$edit_columns['order_custom_date'] = 'Date';

return array_merge( $edit_columns, $columns );

}

add_action( 'manage_shop_order_posts_custom_column', 'so61375632_add_new_order_admin_column_content', 20, 2 );
function so61375632_add_new_order_admin_column_content( $column, $order_id ) {

if ( $column == 'order_custom_date' ) {

$order = wc_get_order( $order_id );

if( ! is_wp_error( $order ) ) {

echo $order->get_date_created()->format ('M j, Y H:i'); // pass any PHP date format

}

}

}

Good luck ;)



Related Topics



Leave a reply



Submit