| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* WooCommerce Routeapp Woocommerce |
| 5 |
* |
| 6 |
* @link https://route.com/ |
| 7 |
* @since 1.0.0 |
| 8 |
* |
| 9 |
* @package Routeapp |
| 10 |
* @subpackage Routeapp/includes |
| 11 |
*/ |
| 12 |
|
| 13 |
use Automattic\WooCommerce\Utilities\OrderUtil; |
| 14 |
|
| 15 |
class Routeapp_Woocommerce |
| 16 |
{ |
| 17 |
|
| 18 |
public function __construct() { |
| 19 |
$this->init(); |
| 20 |
} |
| 21 |
|
| 22 |
public function init() { |
| 23 |
|
| 24 |
if ($this->routeapp_route_enabled_extra_columns()) { |
| 25 |
add_filter( 'manage_edit-shop_order_columns', array( $this, 'routeapp_add_order_route_columns_header' ), 20 ); |
| 26 |
add_action( 'manage_shop_order_posts_custom_column', array( $this, 'routeapp_manage_route_charge_posts_custom_column' ) ); |
| 27 |
add_action( 'restrict_manage_posts', array( $this, 'routeapp_display_admin_shop_order_route_protection_filter' ) ); |
| 28 |
add_action( 'pre_get_posts', array( $this, 'routeapp_process_admin_shop_order_route_protection' ) ); |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Add their respective Route Protection and Route Charge columns after order date |
| 34 |
* |
| 35 |
* @since 1.0.3 |
| 36 |
*/ |
| 37 |
public function routeapp_add_order_route_columns_header( $columns ) { |
| 38 |
$new_columns = array(); |
| 39 |
|
| 40 |
foreach ( $columns as $column_name => $column_info ) { |
| 41 |
$new_columns[ $column_name ] = $column_info; |
| 42 |
|
| 43 |
if ( 'order_date' === $column_name ) { |
| 44 |
$new_columns['route_protection'] = __( 'Route Protection', 'routeapp' ); |
| 45 |
$new_columns['route_charge'] = __( 'Route Charge', 'routeapp' ); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
return $new_columns; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Add their respective Route Protection and Route Charge values. |
| 54 |
* |
| 55 |
* @since 1.0.3 |
| 56 |
* @return void |
| 57 |
*/ |
| 58 |
public function routeapp_manage_route_charge_posts_custom_column( $column ) { |
| 59 |
global $post; |
| 60 |
$order = wc_get_order( $post->ID ); |
| 61 |
|
| 62 |
if ( $column === 'route_charge' ) { |
| 63 |
echo wc_price( $this->routeapp_get_route_charge_fee( $order ), array( 'currency' => $order->get_currency() ) ); |
| 64 |
} else if ( $column === 'route_protection' ) { |
| 65 |
echo $this->routeapp_order_has_route_protection_fee( $order ) ? "Yes" : "No"; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Returns the order's post_meta and checks the Route Charge price |
| 71 |
* |
| 72 |
* @since 1.0.3 |
| 73 |
* @param $order Order's woocommerce object |
| 74 |
* @return float $price |
| 75 |
*/ |
| 76 |
public function routeapp_get_route_charge_fee( $order ) { |
| 77 |
$price = ""; |
| 78 |
if ( class_exists('Automattic\WooCommerce\Utilities\OrderUtil') |
| 79 |
&& OrderUtil::custom_orders_table_usage_is_enabled() ) { |
| 80 |
// HPOS usage is enabled. |
| 81 |
$price = $order->get_meta('_routeapp_route_charge'); |
| 82 |
} else { |
| 83 |
// Traditional CPT-based orders are in use. |
| 84 |
$price = get_post_meta($order->get_id(), '_routeapp_route_charge', true); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
if ( $price === "" ) { |
| 89 |
$price = 0; |
| 90 |
} |
| 91 |
|
| 92 |
return $price; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Returns the order's post_meta and checks if it has Route Protection |
| 97 |
* |
| 98 |
* @since 1.0.3 |
| 99 |
* @param $order Order's woocommerce object |
| 100 |
* @return bool |
| 101 |
*/ |
| 102 |
public function routeapp_order_has_route_protection_fee( $order ) { |
| 103 |
$route_protection = get_post_meta($order->get_id(), '_routeapp_route_protection', true); |
| 104 |
return $route_protection === "1"; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Create Route Protection select field with Yes and No options |
| 109 |
* |
| 110 |
* @since 1.0.3 |
| 111 |
* @return void |
| 112 |
*/ |
| 113 |
public function routeapp_display_admin_shop_order_route_protection_filter() { |
| 114 |
global $pagenow, $post_type; |
| 115 |
|
| 116 |
if( 'shop_order' === $post_type && 'edit.php' === $pagenow ) { |
| 117 |
$options = array( |
| 118 |
0 => "Yes", |
| 119 |
1 => "No" |
| 120 |
); |
| 121 |
$current = isset($_GET['filter_shop_order_route_protection']) ? $_GET['filter_shop_order_route_protection'] : ''; |
| 122 |
|
| 123 |
echo '<select name="filter_shop_order_route_protection"><option value="">' . __('Route Protection', 'routeapp') . '</option>'; |
| 124 |
foreach ( $options as $option ) { |
| 125 |
printf('<option value="%s"%s>%s</option>', $option, |
| 126 |
$option === $current ? '" selected="selected"' : '', $option ); |
| 127 |
} |
| 128 |
echo '</select>'; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Processes the filter that lists orders that have or do not have Route Protection.. |
| 134 |
* |
| 135 |
* @since 1.0.3 |
| 136 |
* @param $query Woocommerce Query Object |
| 137 |
* @return void |
| 138 |
*/ |
| 139 |
public function routeapp_process_admin_shop_order_route_protection( $query ) { |
| 140 |
global $pagenow; |
| 141 |
|
| 142 |
if ( $query->is_admin && $pagenow == 'edit.php' && isset( $_GET['filter_shop_order_route_protection'] ) |
| 143 |
&& $_GET['filter_shop_order_route_protection'] != '' && $_GET['post_type'] == 'shop_order' ) { |
| 144 |
|
| 145 |
$input = esc_attr( $_GET['filter_shop_order_route_protection'] ) === "Yes" ? 1 : 0; |
| 146 |
$meta_query = $query->get( 'meta_query' ) ? $query->get( 'meta_query' ) : array(); |
| 147 |
|
| 148 |
$meta_query[] = array( |
| 149 |
'key' => '_routeapp_route_protection', |
| 150 |
'value' => 1, |
| 151 |
'compare' => intval($input) == 1 ? '==' : '!=' |
| 152 |
); |
| 153 |
|
| 154 |
$query->set( 'meta_query', $meta_query ); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
/** |
| 160 |
* Checks whether or not the user has selected the option to disable extra columns |
| 161 |
* |
| 162 |
* @since 1.0.3 |
| 163 |
* @return bool |
| 164 |
*/ |
| 165 |
public function routeapp_route_enabled_extra_columns() { |
| 166 |
return get_option('routeapp_route_enable_extra_columns') !== "no"; |
| 167 |
} |
| 168 |
} |
| 169 |
|