add_custom_order_statuses();
$this->add_custom_order_fields();
$this->modify_order_search();
$this->add_bulk_actions();
$this->add_custom_columns();
$this->add_import_buttons();
}
/**
* Display a notice if WooCommerce is not active
*/
public function woocommerce_missing_notice() {
?>
_x('Prepare to Printing', 'Order status', 'studiou-wc-ord-print-statuses'),
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop('Prepare to Printing (%s)', 'Prepare to Printing (%s)')
));
register_post_status('wc-in-print', array(
'label' => _x('In Printing', 'Order status', 'studiou-wc-ord-print-statuses'),
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop('In Printing (%s)', 'In Printing (%s)')
));
}
/**
* Add custom order statuses to the list of WooCommerce order statuses
*/
public function add_custom_order_statuses_to_list($order_statuses) {
$new_order_statuses = array();
foreach ($order_statuses as $key => $status) {
$new_order_statuses[$key] = $status;
if ($key === 'wc-processing') {
$new_order_statuses['wc-to-print'] = _x('Prepare to Printing', 'Order status', 'studiou-wc-ord-print-statuses');
$new_order_statuses['wc-in-print'] = _x('In Printing', 'Order status', 'studiou-wc-ord-print-statuses');
}
}
return $new_order_statuses;
}
/**
* Add custom order fields
*/
private function add_custom_order_fields() {
add_action('woocommerce_admin_order_data_after_order_details', array($this, 'display_custom_order_fields'));
add_action('woocommerce_process_shop_order_meta', array($this, 'save_custom_order_fields'));
}
/**
* Display custom order fields in the order edit page
*/
public function display_custom_order_fields($order) {
?>
update_meta_data('to_print_date', sanitize_text_field($_POST['to_print_date']));
}
if (isset($_POST['in_print_date'])) {
$order->update_meta_data('in_print_date', sanitize_text_field($_POST['in_print_date']));
}
if (isset($_POST['external_ref_ord_no'])) {
$order->update_meta_data('external_ref_ord_no', sanitize_text_field($_POST['external_ref_ord_no']));
}
if (isset($_POST['external_ref_ord_delivered'])) {
$order->update_meta_data('external_ref_ord_delivered', sanitize_text_field($_POST['external_ref_ord_delivered']));
}
$order->save();
}
/**
* Modify order search to include external reference order number
*/
private function modify_order_search() {
add_filter('woocommerce_shop_order_search_fields', array($this, 'add_external_ref_ord_no_to_search'));
}
/**
* Add external reference order number to search fields
*/
public function add_external_ref_ord_no_to_search($search_fields) {
$search_fields[] = 'external_ref_ord_no';
return $search_fields;
}
/**
* Add bulk actions to the orders list
*/
private function add_bulk_actions() {
add_filter('bulk_actions-edit-shop_order', array($this, 'register_bulk_actions'));
add_filter('handle_bulk_actions-edit-shop_order', array($this, 'handle_bulk_actions'), 10, 3);
}
/**
* Register custom bulk actions
*/
public function register_bulk_actions($bulk_actions) {
$bulk_actions['prepare_to_printing_export'] = __('Prepare to Printing Export', 'studiou-wc-ord-print-statuses');
$bulk_actions['set_status_to_prepare_to_printing'] = __('Set Status to Prepare to Printing', 'studiou-wc-ord-print-statuses');
$bulk_actions['set_status_to_in_printing'] = __('Set Status to In Printing', 'studiou-wc-ord-print-statuses');
return $bulk_actions;
}
/**
* Handle custom bulk actions
*/
public function handle_bulk_actions($redirect_to, $action, $post_ids) {
if ($action === 'prepare_to_printing_export') {
$this->prepare_to_printing_export($post_ids);
} elseif ($action === 'set_status_to_prepare_to_printing') {
$this->set_status_to_prepare_to_printing($post_ids);
} elseif ($action === 'set_status_to_in_printing') {
$this->set_status_to_in_printing($post_ids);
}
return $redirect_to;
}
/**
* Handle the "Prepare to Printing Export" bulk action
*/
private function prepare_to_printing_export($post_ids) {
global $wpdb;
$results = $wpdb->get_results("SELECT * FROM `vsu_orders_car_prod_qty_img` WHERE order_id IN (" . implode(',', array_map('intval', $post_ids)) . ")");
// Generate CSV file
$filename = 'prepare_to_printing_export_' . date('Y-m-d_His') . '.csv';
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="' . $filename . '"');
$output = fopen('php://output', 'w');
fputcsv($output, array_keys((array)$results[0])); // CSV header
foreach ($results as $row) {
fputcsv($output, (array)$row);
}
fclose($output);
// Update order statuses and to-print-date
foreach ($post_ids as $post_id) {
$order = wc_get_order($post_id);
$order->update_status('wc-to-print');
$order->update_meta_data('to_print_date', current_time('mysql'));
$order->save();
}
exit();
}
/**
* Handle the "Set Status to Prepare to Printing" bulk action
*/
private function set_status_to_prepare_to_printing($post_ids) {
foreach ($post_ids as $post_id) {
$order = wc_get_order($post_id);
$order->update_status('wc-to-print');
$order->update_meta_data('to_print_date', current_time('mysql'));
$order->save();
}
}
/**
* Handle the "Set Status to In Printing" bulk action
*/
private function set_status_to_in_printing($post_ids) {
foreach ($post_ids as $post_id) {
$order = wc_get_order($post_id);
$order->update_status('wc-in-print');
$order->update_meta_data('in_print_date', current_time('mysql'));
$order->save();
}
}
/**
* Add custom columns to the orders list
*/
private function add_custom_columns() {
add_filter('manage_edit-shop_order_columns', array($this, 'add_custom_shop_order_column'), 20);
add_action('manage_shop_order_posts_custom_column', array($this, 'add_custom_shop_order_column_content'), 20, 2);
}
/**
* Add custom columns to the orders list table
*/
public function add_custom_shop_order_column($columns) {
$new_columns = array();
foreach ($columns as $column_name => $column_info) {
$new_columns[$column_name] = $column_info;
if ('order_total' === $column_name) {
$new_columns['to_print_date'] = __('To Print Date', 'studiou-wc-ord-print-statuses');
$new_columns['in_print_date'] = __('In Print Date', 'studiou-wc-ord-print-statuses');
$new_columns['external_ref_ord_no'] = __('External Order Number', 'studiou-wc-ord-print-statuses');
}
}
return $new_columns;
}
/**
* Add content to custom columns in the orders list table
*/
public function add_custom_shop_order_column_content($column, $post_id) {
$order = wc_get_order($post_id);
switch ($column) {
case 'to_print_date':
echo esc_html($order->get_meta('to_print_date'));
break;
case 'in_print_date':
echo esc_html($order->get_meta('in_print_date'));
break;
case 'external_ref_ord_no':
echo esc_html($order->get_meta('external_ref_ord_no'));
break;
}
}
/**
* Add import buttons to the orders page
*/
private function add_import_buttons() {
add_action('woocommerce_order_list_table_restrict_manage_orders', array($this, 'add_import_buttons_to_orders_page'));
add_action('admin_post_import_inprint_protocol', array($this, 'handle_import_inprint_protocol'));
add_action('admin_post_import_delivered_protocol', array($this, 'handle_import_delivered_protocol'));
}
/**
* Add import buttons to the orders page
*/
public function add_import_buttons_to_orders_page() {
?>
update_status('wc-in-print');
$order->update_meta_data('in_print_date', current_time('mysql'));
$order->update_meta_data('external_ref_ord_no', $external_order);
$order->save();
}
}
fclose($file);
wp_redirect(admin_url('edit.php?post_type=shop_order'));
exit;
}
/**
* Handle the import of Delivered protocol
*/
public function handle_import_delivered_protocol() {
if (!isset($_FILES['file']) || $_FILES['file']['error'] !== UPLOAD_ERR_OK) {
wp_die('File upload failed.');
}
$file = fopen($_FILES['file']['tmp_name'], 'r');
if ($file === false) {
wp_die('Failed to open the uploaded file.');
}
// Skip the header row
fgetcsv($file);
while (($data = fgetcsv($file)) !== false) {
$order_number = $data[0];
$order = wc_get_order($order_number);
if ($order) {
$order->update_status('wc-completed');
$order->update_meta_data('external_ref_ord_delivered', current_time('mysql'));
$order->save();
}
}
fclose($file);
wp_redirect(admin_url('edit.php?post_type=shop_order'));
exit;
}
}
// Initialize the plugin
new Studiou_WC_Ord_Print_Statuses();