Home / Admin / Bulk Change Custom Order Status wpadmin
Duplicate Snippet

Embed Snippet on Your Site

Bulk Change Custom Order Status wpadmin

Code Preview
php
<?php
add_filter( 'bulk_actions-edit-shop_order', 'misha_register_bulk_action' ); // edit-shop_order is the screen ID of the orders page
function misha_register_bulk_action( $bulk_actions ) {
	$bulk_actions[ 'mark_packed' ] = 'Change status to packed'; // <option value="mark_packed">Change status to packed</option>
	$bulk_actions[ 'mark_shipping' ] = 'Change status to shipping'; // <option value="mark_shipping">Change status to shipping</option>
	return $bulk_actions;
}
add_action( 'handle_bulk_actions-edit-shop_order', 'misha_bulk_process_custom_status', 20, 3 );
function misha_bulk_process_custom_status( $redirect, $doaction, $object_ids ) {
	if( 'mark_packed' === $doaction ) {
		// change status of every selected order
		foreach ( $object_ids as $order_id ) {
			$order = wc_get_order( $order_id );
			$order->update_status( 'wc-custom-packed' );
		}
		// do not forget to add query args to URL because we will show notices later
		$redirect = add_query_arg(
			array(
				'bulk_action' => 'marked_packed',
				'changed' => count( $object_ids ),
			),
			$redirect
		);
	}
	if( 'mark_shipping' === $doaction ) {
		// change status of every selected order
		foreach ( $object_ids as $order_id ) {
			$order = wc_get_order( $order_id );
			$order->update_status( 'wc-custom-shipping' );
		}
		// do not forget to add query args to URL because we will show notices later
		$redirect = add_query_arg(
			array(
				'bulk_action' => 'marked_shipping',
				'changed' => count( $object_ids ),
			),
			$redirect
		);
	}
	return $redirect;
}
add_action( 'admin_notices', 'misha_custom_order_status_notices' );
function misha_custom_order_status_notices() {
	if( 
		isset( $_REQUEST[ 'bulk_action' ] ) 
		&& 'marked_packed' == $_REQUEST[ 'bulk_action' ]
	 	&& isset( $_REQUEST[ 'changed' ] )
		&& $_REQUEST[ 'changed' ]
	) {
		// displaying the message
		printf(
			'<div id="message" class="updated notice is-dismissible"><p>' . _n( '%d order status changed.', '%d order statuses changed.', $_REQUEST[ 'changed' ] ) . '</p></div>',
			$_REQUEST[ 'changed' ]
		);
		
	}
	if( 
		isset( $_REQUEST[ 'bulk_action' ] ) 
		&& 'marked_shipping' == $_REQUEST[ 'bulk_action' ]
	 	&& isset( $_REQUEST[ 'changed' ] )
		&& $_REQUEST[ 'changed' ]
	) {
		// displaying the message
		printf(
			'<div id="message" class="updated notice is-dismissible"><p>' . _n( '%d order status changed.', '%d order statuses changed.', $_REQUEST[ 'changed' ] ) . '</p></div>',
			$_REQUEST[ 'changed' ]
		);
		
	}
}

Comments

Add a Comment