| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- <?php
- // Handles custom order status registration and integration.
- class Order_Status_Manager {
- private $custom_statuses;
- public function __construct() {
- $this->custom_statuses = array(
- 'wc-to-print' => array(
- 'label' => _x('Prepare to Printing', 'Order status', 'studiou-wc-ord-print-statuses'),
- 'label_count' => _n_noop('Prepare to Printing <span class="count">(%s)</span>', 'Prepare to Printing <span class="count">(%s)</span>', 'studiou-wc-ord-print-statuses')
- ),
- 'wc-in-print' => array(
- 'label' => _x('In Printing', 'Order status', 'studiou-wc-ord-print-statuses'),
- 'label_count' => _n_noop('In Printing <span class="count">(%s)</span>', 'In Printing <span class="count">(%s)</span>', 'studiou-wc-ord-print-statuses')
- )
- );
- add_action('init', array($this, 'register_custom_order_statuses'));
- add_filter('wc_order_statuses', array($this, 'add_custom_order_statuses_to_list'));
- add_action('woocommerce_order_status_changed', array($this, 'handle_status_transitions'), 10, 3);
- add_action('woocommerce_payment_complete', array($this, 'set_order_processing_status'));
- }
- public function register_custom_order_statuses() {
- foreach ($this->custom_statuses as $status_slug => $status_args) {
- register_post_status($status_slug, array(
- 'label' => $status_args['label'],
- 'public' => true,
- 'exclude_from_search' => false,
- 'show_in_admin_all_list' => true,
- 'show_in_admin_status_list' => true,
- 'label_count' => $status_args['label_count']
- ));
- }
- UtilsLog::log('Statuses registered');
- }
- 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') {
- foreach ($this->custom_statuses as $status_slug => $status_args) {
- $new_order_statuses[$status_slug] = $status_args['label'];
- }
- } }
- return $new_order_statuses;
- }
- public function handle_status_transitions($order_id, $old_status, $new_status) {
- // Handle status transitions here
- // For example:
- UtilsLog::log('Order #' . $order_id . ' status change ' . $old_status . ' -> ' . $new_status);
- if ($new_status === 'to-print') {
- update_post_meta($order_id, 'to_print_date', current_time('mysql'));
- } elseif ($new_status === 'in-print') {
- update_post_meta($order_id, 'in_print_date', current_time('mysql'));
- }
- }
- public function set_order_processing_status($order_id) {
- UtilsLog::log('Order #' . $order_id . ' payment complete');
- $order = wc_get_order($order_id);
- if ($order && !$order->has_status('completed')) {
- UtilsLog::log('Order #' . $order_id . ' will be move to processing');
- $order->update_status('processing', __('Payment received, order is now processing.', 'studiou-wc-ord-print-statuses'));
- // Add a note to the order
- $order->add_order_note(__('Order automatically set to processing by Studiou WC Order Print Statuses plugin.', 'studiou-wc-ord-print-statuses'));
-
- }
- }
- public function get_custom_statuses() {
- return $this->custom_statuses;
- }
- }
|