PluginProbe
Route ‑ Shipping Protection / 2.4.16
Route ‑ Shipping Protection v2.4.16
2.4.19 2.4.18 2.4.17 2.4.16 2.4.15 2.4.14 2.4.13 2.4.12 2.4.11 2.4.10 2.4.9 2.4.8 2.4.7 2.4.6 2.4.5 2.4.4 2.4.3 2.4.2 2.2.4 2.2.5 2.2.6 2.2.8 2.2.9 2.3.0 2.3.1 All 153 releases
routeapp / includes / class-routeapp-woocommerce.php

class-routeapp-woocommerce.php in Route ‑ Shipping Protection 2.4.16, at includes/class-routeapp-woocommerce.php

215 lines 7.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 add_action( 'manage_woocommerce_page_wc-orders_custom_column', array( $this, 'routeapp_manage_route_charge_posts_custom_column_new' ) , 99, 2 );
31 add_action( 'manage_shop_order_custom_column', array( $this, 'routeapp_manage_route_charge_posts_custom_column_new' ) , 99, 2 );
32
33 add_filter( 'manage_woocommerce_page_wc-orders_columns', array( $this, 'routeapp_add_order_route_columns_header' ), 20 );
34
35 }
36 }
37
38 /**
39 * Add their respective Route Protection and Route Charge columns after order date
40 *
41 * @since 1.0.3
42 */
43 public function routeapp_add_order_route_columns_header( $columns ) {
44 $new_columns = array();
45
46 foreach ( $columns as $column_name => $column_info ) {
47 $new_columns[ $column_name ] = $column_info;
48
49 if ( 'order_date' === $column_name ) {
50 $new_columns['route_protection'] = __( 'Route Protection', 'routeapp' );
51 $new_columns['route_charge'] = __( 'Route Charge', 'routeapp' );
52 }
53 }
54
55 return $new_columns;
56 }
57
58 /**
59 * Add their respective Route Protection and Route Charge values.
60 *
61 * @since 1.0.3
62 * @return void
63 */
64 public function routeapp_manage_route_charge_posts_custom_column( $column ) {
65 global $post;
66 $order = wc_get_order( $post->ID );
67
68 if ( $column === 'route_charge' ) {
69 echo wc_price( $this->routeapp_get_route_charge_fee( $order ), array( 'currency' => $order->get_currency() ) );
70 } else if ( $column === 'route_protection' ) {
71 echo $this->routeapp_order_has_route_protection_fee( $order ) ? "Yes" : "No";
72 }
73 }
74
75 /**
76 * Add their respective Route Protection and Route Charge values.
77 *
78 * @since 1.0.3
79 * @return void
80 */
81 public function routeapp_manage_route_charge_posts_custom_column_new( $column, $orderParam ) {
82 global $post;
83
84 // Check if $orderParam is set and not null
85 if ( isset( $orderParam ) && ! is_null( $orderParam ) ) {
86 $order = $orderParam; // Get the order using $orderParam if it's valid
87 } else {
88 $order = wc_get_order( $post->ID ); // Fallback to global $post if $orderParam is not set
89 }
90
91 if ( $column === 'route_charge' ) {
92 echo wc_price( $this->routeapp_get_route_charge_fee( $order ), array( 'currency' => $order->get_currency() ) );
93 } else if ( $column === 'route_protection' ) {
94 echo $this->routeapp_order_has_route_protection_fee( $order ) ? "Yes" : "No";
95 }
96 }
97
98 /**
99 * Returns the order's post_meta and checks the Route Charge price
100 *
101 * @since 1.0.3
102 * @param $order Order's woocommerce object
103 * @return float $price
104 */
105 public function routeapp_get_route_charge_fee( $order ) {
106 global $post;
107 $price = "";
108 if ( class_exists('Automattic\WooCommerce\Utilities\OrderUtil')
109 && OrderUtil::custom_orders_table_usage_is_enabled() ) {
110 // HPOS usage is enabled.
111 $price = $order->get_meta('_routeapp_route_charge');
112 } else {
113 $orderData = wc_get_order( $post->ID );
114 if ($orderData) {
115 // Traditional CPT-based orders are in use.
116 $price = get_post_meta($order->get_id(), '_routeapp_route_charge', true);
117 }
118 }
119
120
121 if ( $price === "" ) {
122 $price = 0;
123 }
124
125 return $price;
126 }
127
128 /**
129 * Returns the order's post_meta and checks if it has Route Protection
130 *
131 * @since 1.0.3
132 * @param $order Order's woocommerce object
133 * @return bool
134 */
135 public function routeapp_order_has_route_protection_fee( $order ) {
136 global $post;
137 $route_protection = "0";
138 if ( class_exists('Automattic\WooCommerce\Utilities\OrderUtil')
139 && OrderUtil::custom_orders_table_usage_is_enabled() ) {
140 // HPOS usage is enabled.
141 $route_protection = $order->get_meta('_routeapp_route_protection');
142 } else {
143 $orderData = wc_get_order( $post->ID );
144 if ($orderData) {
145 // Traditional CPT-based orders are in use.
146 $route_protection = get_post_meta($orderData->get_id(), '_routeapp_route_protection', true);
147 }
148 }
149
150 return $route_protection === "1";
151 }
152
153 /**
154 * Create Route Protection select field with Yes and No options
155 *
156 * @since 1.0.3
157 * @return void
158 */
159 public function routeapp_display_admin_shop_order_route_protection_filter() {
160 global $pagenow, $post_type;
161
162 if( 'shop_order' === $post_type && 'edit.php' === $pagenow ) {
163 $options = array(
164 0 => "Yes",
165 1 => "No"
166 );
167 $current = isset($_GET['filter_shop_order_route_protection']) ? $_GET['filter_shop_order_route_protection'] : '';
168
169 echo '<select name="filter_shop_order_route_protection"><option value="">' . __('Route Protection', 'routeapp') . '</option>';
170 foreach ( $options as $option ) {
171 printf('<option value="%s"%s>%s</option>', $option,
172 $option === $current ? '" selected="selected"' : '', $option );
173 }
174 echo '</select>';
175 }
176 }
177
178 /**
179 * Processes the filter that lists orders that have or do not have Route Protection..
180 *
181 * @since 1.0.3
182 * @param $query Woocommerce Query Object
183 * @return void
184 */
185 public function routeapp_process_admin_shop_order_route_protection( $query ) {
186 global $pagenow;
187
188 if ( $query->is_admin && $pagenow == 'edit.php' && isset( $_GET['filter_shop_order_route_protection'] )
189 && $_GET['filter_shop_order_route_protection'] != '' && $_GET['post_type'] == 'shop_order' ) {
190
191 $input = esc_attr( $_GET['filter_shop_order_route_protection'] ) === "Yes" ? 1 : 0;
192 $meta_query = $query->get( 'meta_query' ) ? $query->get( 'meta_query' ) : array();
193
194 $meta_query[] = array(
195 'key' => '_routeapp_route_protection',
196 'value' => 1,
197 'compare' => intval($input) == 1 ? '==' : '!='
198 );
199
200 $query->set( 'meta_query', $meta_query );
201 }
202 }
203
204
205 /**
206 * Checks whether or not the user has selected the option to disable extra columns
207 *
208 * @since 1.0.3
209 * @return bool
210 */
211 public function routeapp_route_enabled_extra_columns() {
212 return get_option('routeapp_route_enable_extra_columns') !== "no";
213 }
214 }
215