| 1 |
<?php |
| 2 |
/** |
| 3 |
* List_Orders. |
| 4 |
* |
| 5 |
* @package WCPOS\WooCommercePOS |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WCPOS\WooCommercePOS\Admin\Orders; |
| 9 |
|
| 10 |
use WC_Abstract_Order; |
| 11 |
use WP_Query; |
| 12 |
use const WCPOS\WooCommercePOS\PLUGIN_URL; |
| 13 |
|
| 14 |
/** |
| 15 |
* List_Orders class. |
| 16 |
*/ |
| 17 |
class List_Orders { |
| 18 |
|
| 19 |
/** |
| 20 |
* List_Orders constructor. |
| 21 |
*/ |
| 22 |
public function __construct() { |
| 23 |
// add filter dropdown to orders list page. |
| 24 |
add_action( 'restrict_manage_posts', array( $this, 'order_filter_dropdown' ) ); |
| 25 |
add_filter( 'parse_query', array( $this, 'parse_query' ) ); |
| 26 |
|
| 27 |
// add column for POS orders. |
| 28 |
add_filter( 'manage_edit-shop_order_columns', array( $this, 'pos_shop_order_column' ) ); |
| 29 |
add_action( 'manage_shop_order_posts_custom_column', array( $this, 'pos_orders_list_column_content' ), 10, 2 ); |
| 30 |
add_action( 'admin_enqueue_scripts', array( $this, 'pos_order_column_width' ) ); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Add a filter dropdown to the orders list page. |
| 35 |
* |
| 36 |
* @return void |
| 37 |
*/ |
| 38 |
public function order_filter_dropdown(): void { |
| 39 |
$selected = isset( $_GET['pos_order'] ) ? sanitize_text_field( wp_unslash( $_GET['pos_order'] ) ) : ''; |
| 40 |
|
| 41 |
$options = array( |
| 42 |
'yes' => /* translators: Order channel filter value shown in WooCommerce admin for POS orders. */ __( 'POS', 'woocommerce-pos' ), |
| 43 |
'no' => /* translators: Order channel filter value shown in WooCommerce admin for online orders. */ __( 'Online', 'woocommerce-pos' ), |
| 44 |
); |
| 45 |
|
| 46 |
echo '<select name="pos_order" id="pos_order">'; |
| 47 |
echo '<option value="">All orders</option>'; |
| 48 |
foreach ( $options as $value => $label ) { |
| 49 |
echo '<option value="' . esc_attr( $value ) . '" '; |
| 50 |
selected( $selected, $value ); |
| 51 |
echo '>' . esc_html( $label ) . '</option>'; |
| 52 |
} |
| 53 |
echo '</select>'; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Parse the query to filter orders by POS or online. |
| 58 |
* |
| 59 |
* @param WP_Query $query The WP_Query instance (passed by reference). |
| 60 |
* |
| 61 |
* @return WP_Query |
| 62 |
*/ |
| 63 |
public function parse_query( $query ) { |
| 64 |
if ( isset( $_GET['pos_order'] ) && '' != $_GET['pos_order'] ) { |
| 65 |
$meta_query = array( 'relation' => 'AND' ); |
| 66 |
|
| 67 |
if ( 'yes' == $_GET['pos_order'] ) { |
| 68 |
$meta_query[] = array( |
| 69 |
'relation' => 'OR', |
| 70 |
array( |
| 71 |
'key' => '_pos', |
| 72 |
'value' => '1', |
| 73 |
'compare' => '=', |
| 74 |
), |
| 75 |
array( |
| 76 |
'key' => '_created_via', |
| 77 |
'value' => 'woocommerce-pos', |
| 78 |
'compare' => '=', |
| 79 |
), |
| 80 |
); |
| 81 |
} elseif ( 'no' == $_GET['pos_order'] ) { |
| 82 |
$meta_query[] = array( |
| 83 |
'relation' => 'OR', |
| 84 |
array( |
| 85 |
'key' => '_pos', |
| 86 |
'compare' => 'NOT EXISTS', |
| 87 |
), |
| 88 |
array( |
| 89 |
'key' => '_pos', |
| 90 |
'value' => '1', |
| 91 |
'compare' => '!=', |
| 92 |
), |
| 93 |
); |
| 94 |
$meta_query[] = array( |
| 95 |
'relation' => 'OR', |
| 96 |
array( |
| 97 |
'key' => '_created_via', |
| 98 |
'compare' => 'NOT EXISTS', |
| 99 |
), |
| 100 |
array( |
| 101 |
'key' => '_created_via', |
| 102 |
'value' => 'woocommerce-pos', |
| 103 |
'compare' => '!=', |
| 104 |
), |
| 105 |
); |
| 106 |
} |
| 107 |
|
| 108 |
if ( isset( $query->query_vars['meta_query'] ) ) { |
| 109 |
$query->query_vars['meta_query'] = array_merge( |
| 110 |
$query->query_vars['meta_query'], |
| 111 |
$meta_query |
| 112 |
); |
| 113 |
} else { |
| 114 |
$query->query_vars['meta_query'] = $meta_query; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
return $query; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
/** |
| 123 |
* Add a custom column to the orders list table. |
| 124 |
* |
| 125 |
* @param string[] $columns The column header labels keyed by column ID. |
| 126 |
* |
| 127 |
* @return string[] |
| 128 |
*/ |
| 129 |
public function pos_shop_order_column( array $columns ): array { |
| 130 |
$new_columns = array(); |
| 131 |
|
| 132 |
foreach ( $columns as $column_name => $column_info ) { |
| 133 |
if ( 'order_date' === $column_name ) { |
| 134 |
$new_columns['wcpos'] = ''; |
| 135 |
} |
| 136 |
|
| 137 |
$new_columns[ $column_name ] = $column_info; |
| 138 |
} |
| 139 |
|
| 140 |
return $new_columns; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Display the content for the custom column. |
| 145 |
* |
| 146 |
* @param string $column_name The name of the column to display. |
| 147 |
* @param int $post_id The current post ID. |
| 148 |
* |
| 149 |
* @return void |
| 150 |
*/ |
| 151 |
public function pos_orders_list_column_content( string $column_name, int $post_id ): void { |
| 152 |
if ( 'wcpos' === $column_name && woocommerce_pos_is_pos_order( $post_id ) ) { |
| 153 |
echo '<span class="wcpos-icon" title="POS Order"></span>'; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
/** |
| 159 |
* Add inline CSS for the POS order column width. |
| 160 |
* |
| 161 |
* @return void |
| 162 |
*/ |
| 163 |
public function pos_order_column_width(): void { |
| 164 |
// Define the URL of your custom icon. |
| 165 |
$icon_url = PLUGIN_URL . '/assets/img/wp-menu-icon.svg'; |
| 166 |
|
| 167 |
$css = ' |
| 168 |
body.post-type-shop_order .wp-list-table .column-wcpos { width: 16px !important; padding: 0 !important; } |
| 169 |
.wcpos-icon { display: inline-block; width: 16px; height: 16px; background: url(' . $icon_url . ') no-repeat center center; margin-top: 10px; } |
| 170 |
'; |
| 171 |
|
| 172 |
wp_add_inline_style( 'woocommerce_admin_styles', $css ); |
| 173 |
} |
| 174 |
} |
| 175 |
|