| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class Order\OrdersList file. |
| 4 |
* |
| 5 |
* @package PostNLWooCommerce\Order |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace PostNLWooCommerce\Order; |
| 9 |
|
| 10 |
use PostNLWooCommerce\Utils; |
| 11 |
|
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Class OrdersList |
| 18 |
* |
| 19 |
* @package PostNLWooCommerce\Order |
| 20 |
*/ |
| 21 |
class OrdersList extends Base { |
| 22 |
|
| 23 |
/** |
| 24 |
* Collection of hooks when initiation. |
| 25 |
*/ |
| 26 |
public function init_hooks() { |
| 27 |
// add 'Delivery Date' orders page column header |
| 28 |
add_filter( 'manage_edit-shop_order_columns', array( $this, 'add_order_delivery_date_column_header' ), 29 ); |
| 29 |
add_filter( 'manage_woocommerce_page_wc-orders_columns', array( $this, 'add_order_delivery_date_column_header' ), 29 ); |
| 30 |
|
| 31 |
// add 'Delivery Date' orders page column content |
| 32 |
add_action( 'manage_shop_order_posts_custom_column', array( $this, 'add_order_delivery_date_column_content' ), 10, 2 ); |
| 33 |
add_action( 'manage_woocommerce_page_wc-orders_custom_column', array( $this, 'add_order_delivery_date_column_content' ), 10, 2 ); |
| 34 |
|
| 35 |
// add 'Shipping options' orders page column header |
| 36 |
add_filter( 'manage_edit-shop_order_columns', array( $this, 'add_order_shipping_options_column_header' ), 30 ); |
| 37 |
add_filter( 'manage_woocommerce_page_wc-orders_columns', array( $this, 'add_order_shipping_options_column_header' ), 30 ); |
| 38 |
|
| 39 |
// add 'Shipping options' orders page column content |
| 40 |
add_action( 'manage_shop_order_posts_custom_column', array( $this, 'add_order_shipping_options_column_content' ), 10, 2 ); |
| 41 |
add_action( 'manage_woocommerce_page_wc-orders_custom_column', array( $this, 'add_order_shipping_options_column_content' ), 10, 2 ); |
| 42 |
|
| 43 |
// add 'Label Created' orders page column header |
| 44 |
add_filter( 'manage_edit-shop_order_columns', array( $this, 'add_order_barcode_column_header' ), 31 ); |
| 45 |
add_filter( 'manage_woocommerce_page_wc-orders_columns', array( $this, 'add_order_barcode_column_header' ), 31 ); |
| 46 |
|
| 47 |
// add 'Label Created' orders page column content |
| 48 |
add_action( 'manage_shop_order_posts_custom_column', array( $this, 'add_order_barcode_column_content' ), 10, 2 ); |
| 49 |
add_action( 'manage_woocommerce_page_wc-orders_custom_column', array( $this, 'add_order_barcode_column_content' ), 10, 2 ); |
| 50 |
|
| 51 |
// Make delivery date column sortable. |
| 52 |
add_filter( 'manage_edit-shop_order_sortable_columns', array( $this, 'sort_delivery_date_column' ) ); |
| 53 |
|
| 54 |
// Make sorting work properly. |
| 55 |
add_action( 'pre_get_posts', array( $this, 'sortable_orderby_delivery_date' ) ); |
| 56 |
|
| 57 |
// Add 'Eligible Auto Letterbox' orders page column header |
| 58 |
if ( wc_get_base_location()['country'] != 'BE' ) { |
| 59 |
add_filter( 'manage_edit-shop_order_columns', array( $this, 'add_eligible_auto_letterbox_column_header' ), 29, 3 ); |
| 60 |
add_filter( 'manage_woocommerce_page_wc-orders_columns', array( $this, 'add_eligible_auto_letterbox_column_header' ), 29, 3 ); |
| 61 |
} |
| 62 |
// Add 'Eligible Auto Letterbox' orders page column content |
| 63 |
add_action( 'manage_shop_order_posts_custom_column', array( $this, 'add_eligible_auto_letterbox_column_content' ), 10, 3 ); |
| 64 |
add_action( 'manage_woocommerce_page_wc-orders_custom_column', array( $this, 'add_eligible_auto_letterbox_column_content' ), 10, 3 ); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Add barcode column header. |
| 69 |
* |
| 70 |
* @param $columns. |
| 71 |
* |
| 72 |
* @return array. |
| 73 |
*/ |
| 74 |
public function add_order_barcode_column_header( $columns ) { |
| 75 |
$wc_actions = $columns['wc_actions'] ?? null; |
| 76 |
unset( $columns['wc_actions'] ); |
| 77 |
|
| 78 |
$columns['postnl_tracking_link'] = esc_html__( 'PostNL Tracking', 'postnl-for-woocommerce' ); |
| 79 |
|
| 80 |
if ( $wc_actions ) { |
| 81 |
$columns['wc_actions'] = $wc_actions; |
| 82 |
} |
| 83 |
|
| 84 |
return $columns; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Add barcode column content. |
| 89 |
* |
| 90 |
* @param $column. |
| 91 |
* @param $order_id. |
| 92 |
* |
| 93 |
* @return void. |
| 94 |
*/ |
| 95 |
public function add_order_barcode_column_content( $column, $order_id ) { |
| 96 |
if ( empty( $order_id ) ) { |
| 97 |
return; |
| 98 |
} |
| 99 |
|
| 100 |
if ( 'postnl_tracking_link' === $column ) { |
| 101 |
echo $this->get_tracking_link( $order_id ); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Add delivery date column header. |
| 107 |
* |
| 108 |
* @param $columns . |
| 109 |
* |
| 110 |
* @return array. |
| 111 |
*/ |
| 112 |
public function add_order_delivery_date_column_header( $columns ) { |
| 113 |
$wc_actions = $columns['wc_actions'] ?? null; |
| 114 |
unset( $columns['wc_actions'] ); |
| 115 |
|
| 116 |
// Add the new Delivery Date column. |
| 117 |
$columns['postnl_delivery_date'] = esc_html__( 'Delivery Date', 'postnl-for-woocommerce' ); |
| 118 |
|
| 119 |
if ( $wc_actions ) { |
| 120 |
$columns['wc_actions'] = $wc_actions; |
| 121 |
} |
| 122 |
|
| 123 |
return $columns; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Add delivery date column content. |
| 128 |
* |
| 129 |
* @param $column . |
| 130 |
* @param $order_id . |
| 131 |
* |
| 132 |
* @return void. |
| 133 |
*/ |
| 134 |
public function add_order_delivery_date_column_content( $column, $order_id ) { |
| 135 |
if ( empty( $order_id ) ) { |
| 136 |
return; |
| 137 |
} |
| 138 |
|
| 139 |
if ( 'postnl_delivery_date' === $column ) { |
| 140 |
$order = wc_get_order( $order_id ); |
| 141 |
|
| 142 |
if ( ! is_a( $order, 'WC_Order' ) ) { |
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
if ( Utils::is_order_eligible_auto_letterbox( $order ) ) { |
| 147 |
esc_html_e( 'As soon as possible', 'postnl-for-woocommerce' ); |
| 148 |
return; |
| 149 |
} |
| 150 |
|
| 151 |
$delivery_info = $this->get_order_frontend_info( $order, 'delivery_day_date' ); |
| 152 |
|
| 153 |
echo Utils::generate_delivery_date_html( $delivery_info ); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Add shipping options column header. |
| 159 |
* |
| 160 |
* @param $columns . |
| 161 |
* |
| 162 |
* @return array. |
| 163 |
*/ |
| 164 |
public function add_order_shipping_options_column_header( $columns ) { |
| 165 |
$wc_actions = $columns['wc_actions'] ?? null; |
| 166 |
unset( $columns['wc_actions'] ); |
| 167 |
|
| 168 |
$columns['postnl_shipping_options'] = esc_html__( 'Shipping options', 'postnl-for-woocommerce' ); |
| 169 |
|
| 170 |
if ( $wc_actions ) { |
| 171 |
$columns['wc_actions'] = $wc_actions; |
| 172 |
} |
| 173 |
|
| 174 |
return $columns; |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Add shipping options column content. |
| 179 |
* |
| 180 |
* @param $column . |
| 181 |
* @param $order_id . |
| 182 |
* |
| 183 |
* @return void. |
| 184 |
*/ |
| 185 |
public function add_order_shipping_options_column_content( $column, $order_id ) { |
| 186 |
if ( empty( $order_id ) ) { |
| 187 |
return; |
| 188 |
} |
| 189 |
if ( 'postnl_shipping_options' === $column ) { |
| 190 |
$shipping_options = $this->get_shipping_options( wc_get_order( $order_id ) ); |
| 191 |
echo esc_html( Utils::generate_shipping_options_html( $shipping_options, $order_id ) ); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Make delivery date column sortable. |
| 197 |
* |
| 198 |
* @param array $columns Added columns. |
| 199 |
*/ |
| 200 |
public function sort_delivery_date_column( $columns ) { |
| 201 |
$meta_key = 'postnl_delivery_date'; |
| 202 |
return wp_parse_args( array( 'postnl_delivery_date' => $meta_key ), $columns ); |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Modify query to order by delivery date. |
| 207 |
* |
| 208 |
* @param \WP_Query $query. |
| 209 |
* |
| 210 |
* @return void |
| 211 |
*/ |
| 212 |
public function sortable_orderby_delivery_date( $query ) { |
| 213 |
global $pagenow; |
| 214 |
|
| 215 |
if ( 'edit.php' !== $pagenow || ! isset( $_GET['post_type'] ) || 'shop_order' !== $_GET['post_type'] ) { |
| 216 |
return; |
| 217 |
} |
| 218 |
|
| 219 |
$orderby = $query->get( 'orderby' ); |
| 220 |
$order = 'ASC' === strtoupper( $query->get( 'order' ) ) ? 'ASC' : 'DESC'; |
| 221 |
|
| 222 |
if ( 'postnl_delivery_date' === $orderby ) { |
| 223 |
// Only if sorting by delivery date, filter the results to "on-hold" and "pending" statuses |
| 224 |
$query->set( 'post_status', array( 'wc-on-hold', 'wc-pending' ) ); |
| 225 |
|
| 226 |
add_filter( |
| 227 |
'posts_join', |
| 228 |
function ( $join ) { |
| 229 |
global $wpdb; |
| 230 |
$join .= " LEFT JOIN {$wpdb->postmeta} AS m1 ON {$wpdb->posts}.ID = m1.post_id AND m1.meta_key = '_postnl_old_orders_delivery_date' "; |
| 231 |
$join .= " LEFT JOIN {$wpdb->postmeta} AS m2 ON {$wpdb->posts}.ID = m2.post_id AND m2.meta_key = '_postnl_frontend_delivery_day_date' "; |
| 232 |
return $join; |
| 233 |
} |
| 234 |
); |
| 235 |
|
| 236 |
add_filter( |
| 237 |
'posts_orderby', |
| 238 |
function ( $orderby ) use ( $order ) { |
| 239 |
$orderby = "CASE |
| 240 |
WHEN m1.meta_key IS NULL AND m2.meta_key IS NULL THEN 0 |
| 241 |
ELSE 1 |
| 242 |
END {$order}, |
| 243 |
LEAST(IFNULL(m1.meta_value, '2999-12-31'), IFNULL(m2.meta_value, '2999-12-31')) {$order}"; |
| 244 |
return $orderby; |
| 245 |
} |
| 246 |
); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* Add eligible auto letterbox column header. |
| 252 |
* |
| 253 |
* @param array $columns Order table columns. |
| 254 |
* |
| 255 |
* @return array |
| 256 |
*/ |
| 257 |
public function add_eligible_auto_letterbox_column_header( $columns ) { |
| 258 |
$wc_actions = $columns['wc_actions'] ?? null; |
| 259 |
unset( $columns['wc_actions'] ); |
| 260 |
|
| 261 |
$columns['postnl_eligible_auto_letterbox'] = esc_html__( 'Fits through letterbox', 'postnl-for-woocommerce' ); |
| 262 |
|
| 263 |
if ( $wc_actions ) { |
| 264 |
$columns['wc_actions'] = $wc_actions; |
| 265 |
} |
| 266 |
|
| 267 |
return $columns; |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* Add eligible auto letterbox column content - tick or cross. |
| 272 |
* |
| 273 |
* @param string $column order column ID. |
| 274 |
* @param int $order_id \WC_Order ID. |
| 275 |
* |
| 276 |
* @return void |
| 277 |
*/ |
| 278 |
public function add_eligible_auto_letterbox_column_content( $column, $order_id ) { |
| 279 |
if ( 'postnl_eligible_auto_letterbox' === $column ) { |
| 280 |
if ( Utils::is_order_eligible_auto_letterbox( $order_id ) ) { |
| 281 |
?> |
| 282 |
<span class="postnl_eligible_auto_letterbox eligible">✓</span> |
| 283 |
<?php |
| 284 |
} else { |
| 285 |
?> |
| 286 |
<span class="postnl_eligible_auto_letterbox non-eligible">×</span> |
| 287 |
<?php |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
|