VAT
Your cart

Your cart is empty.

Recipe: add an action to the order quick actions popup

Goal: a new section in the order quick actions popup, with your own fields and a save that uses the WooCommerce API.

The action

An action implements AddoBack\Modules\OrderActions\QuickActionInterface. The popup and the REST route pick up registered actions without further changes.

  • id(): stable id, used in the REST route. Lower case letters, digits, dashes and underscores.
  • label(): label of the save button. Empty for a section without saving, such as links.
  • section(): heading of the popup section.
  • priority(): position in the popup, lower comes first.
  • available( $order, $user ): whether the action is offered for this order and user. A missing action is left out of the popup and refused by the REST route.
  • render( $order ): the fields, escaped. Field names are plain, without a prefix.
  • handle( $order, $input ): carries out the action. Return an array with at least message, or a WP_Error.
use AddoBack\Modules\OrderActions\QuickActionInterface;

final class Priority_Action implements QuickActionInterface {

	public function id(): string {
		return 'myplugin_priority';
	}

	public function label(): string {
		return __( 'Save priority', 'myplugin' );
	}

	public function section(): string {
		return __( 'Priority', 'myplugin' );
	}

	public function priority(): int {
		return 50;
	}

	public function available( \WC_Order $order, \WP_User $user ): bool {
		return user_can( $user, 'edit_shop_orders' );
	}

	public function render( \WC_Order $order ): string {
		$current = (string) $order->get_meta( '_myplugin_priority' );
		return '<select name="level"><option value="low"' . selected( $current, 'low', false ) . '>' . esc_html__( 'Low', 'myplugin' )
			. '</option><option value="high"' . selected( $current, 'high', false ) . '>' . esc_html__( 'High', 'myplugin' ) . '</option></select>';
	}

	public function handle( \WC_Order $order, array $input ) {
		$level = isset( $input['level'] ) && 'high' === $input['level'] ? 'high' : 'low';
		$order->update_meta_data( '_myplugin_priority', $level );
		$order->save(); // The same WooCommerce API as the order screen, so all hooks fire.
		return array( 'message' => __( 'Priority saved.', 'myplugin' ) );
	}
}

Register it

add_filter( 'adba_order_quick_actions', static function ( array $actions ) {
	$actions[] = new Priority_Action();
	return $actions;
} );

Ready to connect your AI to real tools?

Start free trial