1 <?php
2
3 4 5 6 7 8 9 10 11 12 13 14
15
16 namespace WC_POS\Admin;
17
18 use WC_POS\Admin\Settings\Checkout;
19
20 class Orders {
21
22 private $pos_order;
23
24 public function __construct() {
25
26
27 add_action( 'woocommerce_admin_order_data_after_order_details', array($this, 'order_details') );
28 add_action( 'woocommerce_process_shop_order_meta', array($this, 'save'), 10, 2 );
29
30
31 if( version_compare( WC()->version, '2.3', '>' ) ){
32 add_filter( 'woocommerce_payment_gateways', array($this, 'payment_gateways'), 20, 1 );
33 }
34
35
36 add_filter( 'views_edit-shop_order', array( $this, 'pos_order_filters' ), 10, 1 );
37 add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ), 10, 1 );
38
39
40 add_filter( 'post_class', array( $this, 'post_class' ), 10, 3 );
41 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_styles' ) );
42
43 }
44
45 46 47 48
49 public function order_details($order){
50 $this->pos_order = get_post_meta( $order->id, '_pos', true );
51 include 'views/order-details.php';
52 }
53
54 55 56 57 58
59 public function save( $post_id, $post ){
60 if( isset($_POST['wc_pos_order_type']) ){
61 update_post_meta($post_id, '_pos', $_POST['wc_pos_order_type']);
62 }
63 }
64
65 66 67 68 69 70
71 public function payment_gateways($gateways){
72
73 $settings = Checkout::get_instance();
74 $enabled_ids = $settings->get_enabled_gateway_ids();
75
76 $loaded_gateways = array();
77
78 foreach($gateways as $Gateway){
79 $gateway = new $Gateway();
80 if( in_array( $gateway->id, $enabled_ids ) ){
81 $gateway->enabled = 'yes';
82 }
83 $loaded_gateways[] = $gateway;
84 }
85
86 return $loaded_gateways;
87 }
88
89 90 91 92 93
94 public function pos_order_filters( $views ) {
95 global $wpdb;
96
97 $visibility_filters = array(
98 'yes' => __( 'POS', 'woocommerce-pos' ),
99 'no' => __( 'Online', 'woocommerce-pos' )
100 );
101
102 if ( isset( $_GET['pos_order'] ) && !empty( $_GET['pos_order'] ) ) {
103 $views['all'] = str_replace( 'class="current"', '', $views['all'] );
104 }
105
106 foreach( $visibility_filters as $key => $label ) {
107 $sql = "SELECT count(DISTINCT pm.post_id)
108 FROM $wpdb->postmeta pm
109 JOIN $wpdb->posts p ON (p.ID = pm.post_id)
110 WHERE ";
111 $sql .= $key == 'no' ? " pm.post_id NOT IN ( SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_pos' ) " : " pm.meta_key = '_pos' AND pm.meta_value = '1' ";
112
113 $sql .= "AND p.post_type = 'shop_order'";
114
115
116
117
118 $count = $wpdb->get_var($sql);
119 $class = ( isset( $_GET['pos_order'] ) && $_GET['pos_order'] == $key ) ? 'current' : '';
120 $query_string = remove_query_arg(array( 'pos_order' ));
121 if( $class == '' ) $query_string = remove_query_arg(array( 'paged' ));
122 $query_string = add_query_arg( 'pos_order', urlencode($key), $query_string );
123 $views[$key] = '<a href="'. $query_string . '" class="' . esc_attr( $class ) . '">' . $label . ' <span class="count">(' . number_format_i18n( $count ) . ')</a>';
124 }
125
126 return $views;
127 }
128
129 130 131 132
133 function pre_get_posts( $query ) {
134
135 if ( isset( $_GET['pos_order'] ) && !empty( $_GET['pos_order'] ) ) {
136 if( $_GET['pos_order'] == 'yes' ) {
137 $meta_query = array(
138 array(
139 'key' => '_pos',
140 'value' => '1',
141 'compare' => '=='
142 )
143 );
144 }
145 else {
146 $meta_query = array(
147 'relation' => 'OR',
148 array(
149 'key' => '_pos',
150 'value' => '0',
151 'compare' => '=='
152 ),
153 array(
154 'key' => '_pos',
155 'compare' => 'NOT EXISTS'
156 )
157 );
158 }
159
160 $query->set( 'meta_query', $meta_query );
161 }
162
163 }
164
165 166 167 168 169 170 171 172
173 public function post_class($classes, $class, $id){
174 if( get_post_meta( $id, '_pos', true ) ){
175 $classes[] = 'type-wc_pos_order';
176 }
177 return $classes;
178 }
179
180 181 182
183 public function enqueue_admin_styles() {
184 $css = '
185 .widefat .type-wc_pos_order .column-order_status {
186 background: url( '. \WC_POS\PLUGIN_URL .'assets/logo.svg ) no-repeat 75% 9px;
187 background-size: 18px;
188 fill: #94d31b;
189 }
190 .widefat .type-wc_pos_order .column-order_status mark {
191 margin: 0 auto 0 0;
192 }
193 ';
194 wp_add_inline_style( 'wp-admin', $css );
195 }
196
197 }