I was using below code to have custom order status with php 7.4 My host will end php 7.4 support thus I have to use php 8.0.
I got "Attempt to read property "ID" on null" warning for this line.
How I can fix it?
if( get_post_type($post->ID) != 'shop_order' ) return; // Exit
//Packed Status
function register_packed_order_status() {
register_post_status( 'wc-packed', array(
'label' => 'Packed',
'public' => true,
'show_in_admin_status_list' => true,
'show_in_admin_all_list' => true,
'exclude_from_search' => false,
'label_count' => _n_noop( 'Packed <span class="count">(%s)</span>', 'Packed <span class="count">(%s)</span>' )
) );
}
add_action( 'init', 'register_packed_order_status' );
function add_packed_to_order_statuses( $order_statuses ) {
$new_order_statuses = array();
foreach ( $order_statuses as $key => $status ) {
$new_order_statuses[ $key ] = $status;
if ( 'wc-processing' === $key ) {
$new_order_statuses['wc-packed'] = 'Packed';
}
}
return $new_order_statuses;
}
add_filter( 'wc_order_statuses', 'add_packed_to_order_statuses' );
// 3. ADD COLOR TO IN PROGRESS BUTTON
add_action('admin_head', 'styling_packed_order_list' );
function styling_packed_order_list() {
global $pagenow, $post;
if( $pagenow != 'edit.php') return; // Exit
if( get_post_type($post->ID) != 'shop_order' ) return; // Exit
// HERE below set your custom status
$order_status = 'packed'; // <==== HERE
?>
<style>
.order-status.status-<?php echo sanitize_title( $order_status ); ?> {
background: #0D98BA;
color: #ffffff;
}
</style>
<?php
}