woo-custom-order-status/custom-order-status-for-woocommerce.php

139 lines
5 KiB
PHP

<?php
/**
* Plugin Name: Custom Order Status for WooCommerce
* Description: Adds the ability to create and manage custom order statuses in WooCommerce.
* Plugin URI: https://absoftlab.com
* Version: 1.0.2
* Author: absoftlab
* Author URI: https://absoftlab.com
* License: GPL2
*/
if (!defined('ABSPATH')) exit;
class Absoftlab_Custom_Order_Status_Manager {
private $option_key = 'absoftlab_custom_order_statuses';
public function __construct() {
add_action('admin_menu', [$this, 'add_admin_menu']);
add_action('admin_init', [$this, 'handle_form_submit']);
add_action('init', [$this, 'register_custom_statuses']);
add_filter('wc_order_statuses', [$this, 'add_to_wc_statuses']);
}
// Admin Menu
public function add_admin_menu() {
add_submenu_page(
'woocommerce',
'Custom Order Status',
'Custom Order Status',
'manage_woocommerce',
'absoftlab-order-status',
[$this, 'settings_page']
);
}
// Get stored statuses
private function get_statuses() {
return get_option($this->option_key, []);
}
// Register statuses with WooCommerce
public function register_custom_statuses() {
foreach ($this->get_statuses() as $slug => $label) {
register_post_status($slug, [
'label' => $label,
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop("$label (%s)", "$label (%s)")
]);
}
}
// Add to WooCommerce list
public function add_to_wc_statuses($statuses) {
foreach ($this->get_statuses() as $slug => $label) {
$statuses[$slug] = $label;
}
return $statuses;
}
// Handle form: add/delete
public function handle_form_submit() {
if (!current_user_can('manage_woocommerce')) return;
// Add new status
if (isset($_POST['absoftlab_add_status'])) {
check_admin_referer('absoftlab_add_status');
$label = sanitize_text_field($_POST['status_label']);
$slug = 'wc-' . sanitize_title($label);
$statuses = $this->get_statuses();
$statuses[$slug] = $label;
update_option($this->option_key, $statuses);
wp_redirect(admin_url('admin.php?page=absoftlab-order-status&added=1'));
exit;
}
// Delete status
if (isset($_GET['absoftlab_delete']) && isset($_GET['_wpnonce'])) {
if (wp_verify_nonce($_GET['_wpnonce'], 'absoftlab_delete_status')) {
$slug = sanitize_text_field($_GET['absoftlab_delete']);
$statuses = $this->get_statuses();
unset($statuses[$slug]);
update_option($this->option_key, $statuses);
wp_redirect(admin_url('admin.php?page=absoftlab-order-status&deleted=1'));
exit;
}
}
}
// Admin Page HTML
public function settings_page() {
$statuses = $this->get_statuses();
?>
<div class="wrap">
<h1>Custom Order Status Manager</h1>
<?php if (isset($_GET['added'])): ?>
<div class="notice notice-success"><p>Status added successfully!</p></div>
<?php elseif (isset($_GET['deleted'])): ?>
<div class="notice notice-success"><p>Status deleted successfully!</p></div>
<?php endif; ?>
<h2>Add New Status</h2>
<form method="post">
<?php wp_nonce_field('absoftlab_add_status'); ?>
<table class="form-table">
<tr>
<th><label for="status_label">Status Label</label></th>
<td><input type="text" name="status_label" id="status_label" required /></td>
</tr>
</table>
<p><input type="submit" name="absoftlab_add_status" class="button button-primary" value="Add Status" /></p>
</form>
<h2>Existing Statuses</h2>
<table class="widefat striped">
<thead><tr><th>Slug</th><th>Label</th><th>Action</th></tr></thead>
<tbody>
<?php foreach ($statuses as $slug => $label): ?>
<tr>
<td><?php echo esc_html($slug); ?></td>
<td><?php echo esc_html($label); ?></td>
<td>
<a href="<?php echo wp_nonce_url(admin_url('admin.php?page=absoftlab-order-status&absoftlab_delete=' . $slug), 'absoftlab_delete_status'); ?>"
class="button button-small delete">Delete</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php
}
}
new Absoftlab_Custom_Order_Status_Manager();