| 1 |
<?php |
| 2 |
/** |
| 3 |
* Module: Services order. |
| 4 |
* |
| 5 |
* @package Orderable/Classes |
| 6 |
*/ |
| 7 |
|
| 8 |
defined( 'ABSPATH' ) || exit; |
| 9 |
|
| 10 |
/** |
| 11 |
* Services order class. |
| 12 |
*/ |
| 13 |
class Orderable_Services_Order { |
| 14 |
/** |
| 15 |
* Init. |
| 16 |
*/ |
| 17 |
public static function run() { |
| 18 |
add_action( 'restrict_manage_posts', array( __CLASS__, 'services_filter' ), 50 ); |
| 19 |
add_action( 'pre_get_posts', array( __CLASS__, 'filter_admin_orders' ), 100 ); |
| 20 |
add_filter( 'manage_edit-shop_order_columns', array( __CLASS__, 'add_admin_order_columns' ), 10 ); |
| 21 |
add_action( 'manage_shop_order_posts_custom_column', array( __CLASS__, 'add_admin_order_columns_content' ), 10, 2 ); |
| 22 |
add_filter( 'orderable_get_order_date_time', array( __CLASS__, 'modify_order_date_time_labels' ), 10, 2 ); |
| 23 |
add_action( 'woocommerce_before_order_object_save', array( __CLASS__, 'before_save_order' ), 10, 2 ); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Services filter. |
| 28 |
*/ |
| 29 |
public static function services_filter() { |
| 30 |
$service = self::get_filtered_service(); |
| 31 |
?> |
| 32 |
<select name="orderable_service"> |
| 33 |
<option value=""><?php esc_attr_e( 'All services', 'orderable' ); ?></option> |
| 34 |
<option value="delivery" <?php selected( $service, 'delivery' ); ?>><?php esc_attr_e( 'Delivery', 'orderable' ); ?></option> |
| 35 |
<option value="pickup" <?php selected( $service, 'pickup' ); ?>><?php esc_attr_e( 'Pickup', 'orderable' ); ?></option> |
| 36 |
</select> |
| 37 |
<?php |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Get filtered service. |
| 42 |
* |
| 43 |
* @return mixed |
| 44 |
*/ |
| 45 |
public static function get_filtered_service() { |
| 46 |
$service = filter_input( INPUT_GET, 'orderable_service', FILTER_SANITIZE_STRING ); |
| 47 |
|
| 48 |
if ( ! $service ) { |
| 49 |
return ''; |
| 50 |
} |
| 51 |
|
| 52 |
return $service; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Filter orders. |
| 57 |
* |
| 58 |
* @param WP_Query $query |
| 59 |
*/ |
| 60 |
public static function filter_admin_orders( $query ) { |
| 61 |
if ( ! Orderable_Orders::is_orders_page() || 'shop_order' !== $query->get( 'post_type' ) ) { |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
$meta_query = (array) $query->get( 'meta_query' ); |
| 66 |
$service = self::get_filtered_service(); |
| 67 |
|
| 68 |
if ( ! empty( $service ) ) { |
| 69 |
$meta_query[] = array( |
| 70 |
'key' => '_orderable_service_type', |
| 71 |
'value' => $service, |
| 72 |
); |
| 73 |
} |
| 74 |
|
| 75 |
$query->set( 'meta_query', $meta_query ); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Get service type for an order. |
| 80 |
* |
| 81 |
* @param WC_Order $order |
| 82 |
* |
| 83 |
* @return bool|string |
| 84 |
*/ |
| 85 |
public static function get_service_type( $order ) { |
| 86 |
$shipping_methods = $order->get_shipping_methods(); |
| 87 |
$type = false; |
| 88 |
|
| 89 |
if ( empty( $shipping_methods ) ) { |
| 90 |
return $type; |
| 91 |
} |
| 92 |
|
| 93 |
$shipping_method = array_shift( $shipping_methods ); |
| 94 |
$shipping_method_id = $shipping_method->get_method_id(); |
| 95 |
|
| 96 |
return Orderable_Services::is_pickup_method( $shipping_method_id ) ? 'pickup' : 'delivery'; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Add columns to admin orders screen. |
| 101 |
* |
| 102 |
* @param array $columns |
| 103 |
* |
| 104 |
* @return array |
| 105 |
*/ |
| 106 |
public static function add_admin_order_columns( $columns ) { |
| 107 |
$columns['orderable_service_type'] = __( 'Service', 'orderable' ); |
| 108 |
|
| 109 |
return $columns; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Add columns content to admin orders screen. |
| 114 |
* |
| 115 |
* @param $column_name |
| 116 |
* @param $post_id |
| 117 |
*/ |
| 118 |
public static function add_admin_order_columns_content( $column_name, $post_id ) { |
| 119 |
if ( 'orderable_service_type' === $column_name ) { |
| 120 |
$order = wc_get_order( $post_id ); |
| 121 |
$type = Orderable_Services_Order::get_service_type( $order ); |
| 122 |
$background = 'pickup' === $type ? '#C6C7E1' : '#c5e2df'; |
| 123 |
$color = 'pickup' === $type ? '#6264ac' : '#3e7a74'; |
| 124 |
|
| 125 |
printf( '<mark class="order-status order-status--%s" style="background-color: %s; color: %s;"><span>%s</span></mark>', esc_attr( $type ), esc_attr( $background ), esc_attr( $color ), Orderable_Services::get_service_label( $type ) ); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Modify order date/time labels. |
| 131 |
* |
| 132 |
* @param array $labels |
| 133 |
* @param WC_Order $order |
| 134 |
* |
| 135 |
* @return array |
| 136 |
*/ |
| 137 |
public static function modify_order_date_time_labels( $labels, $order ) { |
| 138 |
$type = Orderable_Services_Order::get_service_type( $order ); |
| 139 |
$type_label = Orderable_Services::get_service_label( $type ); |
| 140 |
|
| 141 |
$labels['order_date']['label'] = sprintf( '%s %s', $type_label, $labels['order_date']['label'] ); |
| 142 |
$labels['order_time']['label'] = sprintf( '%s %s', $type_label, $labels['order_time']['label'] ); |
| 143 |
|
| 144 |
return $labels; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Save order meta. |
| 149 |
* |
| 150 |
* @param WC_Abstract_Order $abstract_order |
| 151 |
* @param WC_Data_Store $data_store |
| 152 |
* |
| 153 |
* @return string |
| 154 |
*/ |
| 155 |
public static function before_save_order( $abstract_order, $data_store ) { |
| 156 |
if ( empty( $abstract_order ) ) { |
| 157 |
return; |
| 158 |
} |
| 159 |
|
| 160 |
$type = Orderable_Services_Order::get_service_type( $abstract_order ); |
| 161 |
|
| 162 |
$abstract_order->update_meta_data( '_orderable_service_type', $type ); |
| 163 |
} |
| 164 |
} |