| 1 |
<?php |
| 2 |
/* * class |
| 3 |
* Description of WooCommerceOrderListController |
| 4 |
* |
| 5 |
* @author MA_GROUP |
| 6 |
* |
| 7 |
* @autoload: a2wl_admin_init |
| 8 |
* |
| 9 |
* @ajax: true |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace AliNext_Lite;; |
| 13 |
|
| 14 |
use Automattic\WooCommerce\Utilities\OrderUtil; |
| 15 |
use Pages; |
| 16 |
|
| 17 |
class WooCommerceOrderListController extends AbstractController |
| 18 |
{ |
| 19 |
private $bulk_actions = []; |
| 20 |
|
| 21 |
public function __construct() |
| 22 |
{ |
| 23 |
parent::__construct(); |
| 24 |
|
| 25 |
add_action('admin_enqueue_scripts', array($this, 'assets')); |
| 26 |
|
| 27 |
add_action('a2wl_install', array($this, 'install')); |
| 28 |
|
| 29 |
add_filter('woocommerce_admin_order_actions', array($this, 'admin_order_actions'), 2, 100); |
| 30 |
|
| 31 |
add_action('wp_ajax_a2wl_order_info', [$this, 'ajax_order_info']); |
| 32 |
|
| 33 |
add_action('wp_ajax_a2wl_get_fulfilled_orders', [$this, 'ajax_get_fulfilled_orders']); |
| 34 |
|
| 35 |
//todo: rudiment method? |
| 36 |
add_action('wp_ajax_a2wl_save_tracking_code', [$this, 'ajax_save_tracking_code']); |
| 37 |
|
| 38 |
add_action('admin_init', [$this, 'admin_init']); |
| 39 |
|
| 40 |
$this->bulk_actions = apply_filters('a2wl_wcol_bulk_actions_init', $this->bulk_actions); |
| 41 |
} |
| 42 |
|
| 43 |
public function admin_init(): void |
| 44 |
{ |
| 45 |
if (function_exists('WC') && OrderUtil::custom_orders_table_usage_is_enabled()) { |
| 46 |
add_action('woocommerce_order_list_table_extra_tablenav', [$this, 'woocommerce_order_list_table_extra_tablenav']); |
| 47 |
add_action('manage_woocommerce_page_wc-orders_custom_column', [$this, 'manage_columns_data'], 10, 2); |
| 48 |
add_filter('woocommerce_shop_order_list_table_columns', [$this, 'manage_columns_headers']); |
| 49 |
add_filter("bulk_actions-woocommerce_page_wc-orders", [$this, 'bulk_actions']); |
| 50 |
} else { |
| 51 |
add_action('manage_posts_extra_tablenav', [$this, 'manage_posts_extra_tablenav']); |
| 52 |
//two methods below are shared with the tracking plugin |
| 53 |
add_action('manage_shop_order_posts_custom_column', [$this, 'manage_columns_data'], 10, 2); |
| 54 |
add_filter('manage_edit-shop_order_columns', [$this, 'manage_columns_headers']); |
| 55 |
add_filter("bulk_actions-edit-shop_order", [$this, 'bulk_actions']); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
public function bulk_actions($actions) |
| 60 |
{ |
| 61 |
foreach ($this->bulk_actions as $action => $title) { |
| 62 |
$actions[$action] = $title; |
| 63 |
} |
| 64 |
|
| 65 |
return $actions; |
| 66 |
} |
| 67 |
|
| 68 |
public function install() |
| 69 |
{ |
| 70 |
$user = wp_get_current_user(); |
| 71 |
$page = "edit-shop_order"; |
| 72 |
$hidden = array("billing_address"); |
| 73 |
update_user_option($user->ID, "manage{$page}columnshidden", $hidden, true); |
| 74 |
} |
| 75 |
|
| 76 |
public function manage_posts_extra_tablenav(): void |
| 77 |
{ |
| 78 |
// phpcs:ignoreFile WordPress.Security.NonceVerification.Recommended |
| 79 |
if (isset($_GET['post_type']) && $_GET['post_type'] == "shop_order") { |
| 80 |
$this->add_bulk_order_sync_button(); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
public function woocommerce_order_list_table_extra_tablenav(string $order_type): void |
| 85 |
{ |
| 86 |
if ($order_type === "shop_order") { |
| 87 |
$this->add_bulk_order_sync_button(); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
public function ajax_get_fulfilled_orders(): void |
| 92 |
{ |
| 93 |
check_admin_referer(self::AJAX_NONCE_ACTION, self::NONCE); |
| 94 |
|
| 95 |
if (!PageGuardHelper::canAccessPage(Pages::ORDER_MANAGEMENT)) { |
| 96 |
$result = ResultBuilder::buildError($this->getErrorTextNoPermissions()); |
| 97 |
echo wp_json_encode($result); |
| 98 |
wp_die(); |
| 99 |
} |
| 100 |
|
| 101 |
/** @var $woocommerce_model Woocommerce */ |
| 102 |
$woocommerce_model = A2WL()->getDI()->get('AliNext_Lite\Woocommerce'); |
| 103 |
$result = ResultBuilder::buildOk( |
| 104 |
array('data' => $woocommerce_model->get_fulfilled_orders_data()) |
| 105 |
); |
| 106 |
|
| 107 |
echo wp_json_encode($result); |
| 108 |
wp_die(); |
| 109 |
} |
| 110 |
|
| 111 |
public function ajax_save_tracking_code(): void |
| 112 |
{ |
| 113 |
check_admin_referer(self::AJAX_NONCE_ACTION, self::NONCE); |
| 114 |
|
| 115 |
if (!PageGuardHelper::canAccessPage(Pages::ORDER_MANAGEMENT)) { |
| 116 |
$result = ResultBuilder::buildError($this->getErrorTextNoPermissions()); |
| 117 |
echo wp_json_encode($result); |
| 118 |
wp_die(); |
| 119 |
} |
| 120 |
|
| 121 |
$order_id = intval($_POST['id']); |
| 122 |
$ext_id = intval($_POST['ext_id']); |
| 123 |
$tracking_codes = isset($_POST['tracking_codes']) ? $_POST['tracking_codes'] : array(); |
| 124 |
$carrier_name = isset($_POST['carrier_name']) ? $_POST['carrier_name'] : ''; |
| 125 |
$carrier_url = isset($_POST['carrier_url']) ? $_POST['carrier_url'] : ''; |
| 126 |
$tracking_status = isset($_POST['tracking_status']) ? $_POST['tracking_status'] : ''; |
| 127 |
|
| 128 |
/** @var $woocommerce_model Woocommerce */ |
| 129 |
$woocommerce_model = A2WL()->getDI()->get('AliNext_Lite\Woocommerce'); |
| 130 |
$result = $woocommerce_model->save_tracking_code($order_id, $ext_id, $tracking_codes, $carrier_name, $carrier_url, $tracking_status); |
| 131 |
|
| 132 |
if (!$result) { |
| 133 |
$result = ResultBuilder::buildError(_x('Didn`t find the Woocommerce order №', 'Error text', 'ali2woo') . $order_id); |
| 134 |
} else { |
| 135 |
$result = ResultBuilder::buildOk(); |
| 136 |
} |
| 137 |
|
| 138 |
echo wp_json_encode($result); |
| 139 |
wp_die(); |
| 140 |
} |
| 141 |
|
| 142 |
public function ajax_order_info(): void |
| 143 |
{ |
| 144 |
check_admin_referer(self::AJAX_NONCE_ACTION, self::NONCE); |
| 145 |
|
| 146 |
if (!PageGuardHelper::canAccessPage(Pages::ORDER_MANAGEMENT)) { |
| 147 |
$result = ResultBuilder::buildError($this->getErrorTextNoPermissions()); |
| 148 |
echo wp_json_encode($result); |
| 149 |
wp_die(); |
| 150 |
} |
| 151 |
|
| 152 |
$result = array("state" => "ok", "data" => ""); |
| 153 |
|
| 154 |
$order_id = isset($_POST['id']) ? $_POST['id'] : false; |
| 155 |
|
| 156 |
if (!$order_id) { |
| 157 |
$result['state'] = 'error'; |
| 158 |
echo wp_json_encode($result); |
| 159 |
wp_die(); |
| 160 |
} |
| 161 |
|
| 162 |
$content = array(); |
| 163 |
|
| 164 |
$order = wc_get_order($order_id); |
| 165 |
|
| 166 |
$items = $order->get_items(); |
| 167 |
|
| 168 |
// $order_external_id_array = get_post_meta($order->get_id(), Constants::old_order_external_order_id()); |
| 169 |
// $order_tracking_codes = get_post_meta($order->get_id(), Constants::old_order_tracking_code()); |
| 170 |
|
| 171 |
$k = 1; |
| 172 |
|
| 173 |
foreach ($items as $item) { |
| 174 |
|
| 175 |
$a2wl_order_item = new WooCommerceOrderItem($item); |
| 176 |
|
| 177 |
$product_name = $a2wl_order_item->get_name(); |
| 178 |
$product_id = $a2wl_order_item->get_product_id(); |
| 179 |
|
| 180 |
$tmp = ''; |
| 181 |
|
| 182 |
if ($product_id > 0) { |
| 183 |
$product_url = get_post_meta($product_id, '_a2w_product_url', true); |
| 184 |
$seller_url = get_post_meta($product_id, '_a2w_seller_url', true); |
| 185 |
|
| 186 |
$shipping_info = $item->get_meta(Shipping::get_order_item_shipping_meta_key()); |
| 187 |
$shipping_code = $item->get_meta(Shipping::get_order_item_legacy_shipping_meta_key()); |
| 188 |
|
| 189 |
if ($product_url) { |
| 190 |
$tmp = $k . '). <a title="' . $product_name . '" href="' . $product_url . '" target="_blank" class="link_to_source product_url">' . _x('Product page', 'hint', 'ali2woo') . '</a>'; |
| 191 |
} |
| 192 |
|
| 193 |
if ($seller_url) { |
| 194 |
$tmp .= "<span class='seller_url_block'> | <a href='" . $seller_url . "' target='_blank' class='seller_url'>" . _x('Seller', 'hint', 'ali2woo') . "</a></span>"; |
| 195 |
} |
| 196 |
|
| 197 |
if ($shipping_info) { |
| 198 |
$display_value = Shipping::get_formated_shipping_info_meta($order->get_id(), $shipping_info); |
| 199 |
$tmp .= '<span class="seller_url_block"> | ' . $display_value . '</span>'; |
| 200 |
} else if ($shipping_code) { |
| 201 |
$tmp .= '<span class="seller_url_block"> | ' . $shipping_code . '</span>'; |
| 202 |
} |
| 203 |
|
| 204 |
// $external_order_id = $a2wl_order_item->get_external_order_id(); |
| 205 |
|
| 206 |
$ali_order_link = $a2wl_order_item->get_ali_order_item_link(); |
| 207 |
|
| 208 |
if ($ali_order_link) { |
| 209 |
$tmp .= " | " . esc_html__('AliExpress order ID', 'ali2woo') . ": <span class='seller_url_block'>" . $ali_order_link . "</span>"; |
| 210 |
|
| 211 |
$order_item_tracking_codes = $a2wl_order_item->get_formated_tracking_codes(); |
| 212 |
|
| 213 |
if ($order_item_tracking_codes !== "") { |
| 214 |
$tmp .= " | " . esc_html__('Tracking numbers', 'ali2woo') . ": <span class='seller_url_block'>" . $order_item_tracking_codes . "</span>"; |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
} else { |
| 219 |
$tmp .= $k . '). <span style="color:red;">' . _x('The product has been deleted', 'hint', 'ali2woo') . '</span>'; |
| 220 |
} |
| 221 |
|
| 222 |
$content[] = $tmp; |
| 223 |
$k++; |
| 224 |
} |
| 225 |
|
| 226 |
/* if (!empty($order_external_id_array) && isset($order_external_id_array[0]) && !empty($order_external_id_array[0]) ){ |
| 227 |
$content[] = "AliExpress order ID(s): <span class='seller_url_block'>" . implode(", ", $order_external_id_array). "</span>"; |
| 228 |
} |
| 229 |
|
| 230 |
if (!empty($order_tracking_codes)) { |
| 231 |
$content[] = "Tracking number(s): <span class='seller_url_block'>" . (is_array($order_tracking_codes) ? implode(", ", $order_tracking_codes) : strval($order_tracking_codes)) . "</span>"; |
| 232 |
}*/ |
| 233 |
|
| 234 |
$content = apply_filters('a2wl_get_order_content', $content, $order_id); |
| 235 |
$result['data'] = array('content' => $content, 'id' => $order_id); |
| 236 |
|
| 237 |
echo wp_json_encode($result); |
| 238 |
wp_die(); |
| 239 |
} |
| 240 |
|
| 241 |
public function assets() |
| 242 |
{ |
| 243 |
wp_enqueue_style('a2wl-wc-ol-style', A2WL()->plugin_url() . '/assets/css/wc_ol_style.css', array(), A2WL()->version); |
| 244 |
wp_enqueue_script('a2wl-wc-ol-script', A2WL()->plugin_url() . '/assets/js/wc_ol_script.js', ['jquery-ui-core', 'jquery-ui-dialog'], A2WL()->version); |
| 245 |
|
| 246 |
$lang_data = array( |
| 247 |
'aliexpress_info' => _x('AliExpress Info', 'Dialog title', 'ali2woo'), |
| 248 |
'please_wait_data_loads' => _x('Please wait, data loads...', 'Status', 'ali2woo'), |
| 249 |
'please_wait' => _x('Please wait...', 'Status', 'ali2woo'), |
| 250 |
'sync_process' => _x('Sync process', 'Status', 'ali2woo'), |
| 251 |
'sync_done' => _x('Sync done! Click "Refresh page" to see updated tracking data in your orders.', 'Button text', 'ali2woo'), |
| 252 |
'error' => _x('Error!', 'Button text', 'ali2woo'), |
| 253 |
'error_please_install_new_extension' => _x('Error! Please install the latest Chrome extension.', 'Error text', 'ali2woo'), |
| 254 |
'error_cant_do_tracking_sync' => _x('Can`t get tracking data. Unknown error in the Chrome extension. Please contact our support.', 'Error text', 'ali2woo'), |
| 255 |
'try_again' => _x('Try again?', 'Button text', 'ali2woo'), |
| 256 |
'error_didnt_do_find_alix_order_num' => _x('Didn`t find the AliExpress order № ', 'Error text', 'ali2woo'), |
| 257 |
'error_cant_do_tracking_sync_login_to_account' => _x('Can`t get tracking data. Please log-in to your AliExpress account first.', 'Error text', 'ali2woo'), |
| 258 |
'no_tracking_codes_for_order' => _x('No tracking numbers for the AliExpress order № ', 'Status', 'ali2woo'), |
| 259 |
'tracking_sync' => _x('SYNC ALL with AliExpress', 'Button text', 'ali2woo'), |
| 260 |
'error_403_code' => _x('Please log in your AliExpress account to get tracking data for the AliExpress order № ', 'Error text', 'ali2woo'), |
| 261 |
'tracking_sync_done' => _x('Tracking data is updated for the AliExpress order № ', 'Status', 'ali2woo'), |
| 262 |
'no_orders_for_synch' => _x('At the moment there are no orders available for synchronization. Please note: the plugin updates ONLY not delivered orders with filled AliExpress Order ID.', 'Status', 'ali2woo'), |
| 263 |
'bulk_order_sync' => _x('Bulk Order Sync', 'Popup title', 'ali2woo'), |
| 264 |
'order_sync' => _x('Order Sync', 'Popup title', 'ali2woo'), |
| 265 |
); |
| 266 |
|
| 267 |
wp_localize_script('a2wl-wc-ol-script', 'a2wl_script_data', array('lang' => $lang_data)); |
| 268 |
|
| 269 |
$data = [ |
| 270 |
'ajaxurl' => admin_url('admin-ajax.php'), |
| 271 |
'nonce_action' => wp_create_nonce(self::AJAX_NONCE_ACTION), |
| 272 |
]; |
| 273 |
wp_localize_script('a2wl-wc-ol-script', 'a2wl_wc_ol_script', $data); |
| 274 |
} |
| 275 |
|
| 276 |
public function manage_columns_data(string $columnName, $post_id_or_order_object): void |
| 277 |
{ |
| 278 |
$WC_Order = wc_get_order($post_id_or_order_object); |
| 279 |
switch ($columnName) { |
| 280 |
case 'tracking_code': |
| 281 |
$order_items = $WC_Order->get_items(); |
| 282 |
$result = []; |
| 283 |
foreach ($order_items as $item) { |
| 284 |
$a2wl_order_item = new WooCommerceOrderItem($item); |
| 285 |
|
| 286 |
$tracking_number = $a2wl_order_item->get_formated_tracking_codes(); |
| 287 |
|
| 288 |
if ($tracking_number) { |
| 289 |
$result[] = $tracking_number; |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
if (!empty($result)) { |
| 294 |
echo implode(",", $result); |
| 295 |
} else { |
| 296 |
esc_html_e('Not available yet', 'ali2woo'); |
| 297 |
} |
| 298 |
break; |
| 299 |
|
| 300 |
case 'aliexpress_order': |
| 301 |
$order_items = $WC_Order->get_items(); |
| 302 |
$result = []; |
| 303 |
foreach ($order_items as $item) { |
| 304 |
$a2wl_order_item = new WooCommerceOrderItem($item); |
| 305 |
$ali_order_link = $a2wl_order_item->get_ali_order_item_link(); |
| 306 |
|
| 307 |
if ($ali_order_link) { |
| 308 |
$result[] = $ali_order_link; |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
if (!empty($result)) { |
| 313 |
echo implode(",", $result); |
| 314 |
} else { |
| 315 |
esc_html_e('Not available yet', 'ali2woo'); |
| 316 |
} |
| 317 |
break; |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
public function manage_columns_headers($columns): array |
| 322 |
{ |
| 323 |
$new_columns = []; |
| 324 |
foreach ($columns as $column_name => $column_info) { |
| 325 |
$new_columns[$column_name] = $column_info; |
| 326 |
if ('order_total' === $column_name) { |
| 327 |
$new_columns['tracking_code'] = esc_html__('Tracking numbers', 'ali2woo'); |
| 328 |
$new_columns['aliexpress_order'] = esc_html__('AliExpress Order ID', 'ali2woo'); |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
return $new_columns; |
| 333 |
} |
| 334 |
|
| 335 |
public function admin_order_actions($actions, $order) |
| 336 |
{ |
| 337 |
|
| 338 |
$actions['a2wl_order_fulfillment'] = array( |
| 339 |
'url' => '#' . $order->get_id(), |
| 340 |
'name' => esc_html__('Order fulfillment', 'ali2woo'), |
| 341 |
'action' => 'a2wl_aliexpress_order_fulfillment', |
| 342 |
); |
| 343 |
|
| 344 |
$actions['a2wl-order-info'] = array( |
| 345 |
'url' => '#' . $order->get_id(), |
| 346 |
'name' => esc_html__('AliExpress Info', 'ali2woo'), |
| 347 |
'action' => 'a2wl-order-info', |
| 348 |
); |
| 349 |
|
| 350 |
$order_items = $order->get_items(); |
| 351 |
|
| 352 |
$all_external_id = array(); |
| 353 |
|
| 354 |
foreach ($order_items as $item_id => $item) { |
| 355 |
$a2wl_order_item = new WooCommerceOrderItem($item); |
| 356 |
|
| 357 |
$external_order_id = $a2wl_order_item->get_external_order_id(); |
| 358 |
|
| 359 |
if ($external_order_id) { |
| 360 |
$all_external_id[] = $external_order_id; |
| 361 |
} |
| 362 |
|
| 363 |
} |
| 364 |
|
| 365 |
if (!empty($all_external_id)) { |
| 366 |
$order_ids_url = implode("-", $all_external_id); |
| 367 |
|
| 368 |
$actions['a2wl-aliexpress-sync'] = array( |
| 369 |
'url' => '#' . $order_ids_url, |
| 370 |
'name' => esc_html__('SYNC with AliExpress', 'ali2woo'), |
| 371 |
'action' => 'a2wl-aliexpress-sync', |
| 372 |
); |
| 373 |
} |
| 374 |
|
| 375 |
return $actions; |
| 376 |
} |
| 377 |
|
| 378 |
private function add_bulk_order_sync_button(): void |
| 379 |
{ |
| 380 |
/** @var $woocommerce_model Woocommerce */ |
| 381 |
$woocommerce_model = A2WL()->getDI()->get('AliNext_Lite\Woocommerce'); |
| 382 |
$fulfilled_order_count = $woocommerce_model->get_fulfilled_orders_count(); |
| 383 |
if ($fulfilled_order_count > 0) { |
| 384 |
$buttonText = "SYNC ALL with AliExpress"; |
| 385 |
$hintText = "It allows you to get and update tracking data for all orders via the Ali2Woo Chrome extension."; |
| 386 |
?> |
| 387 |
<div class="alignleft actions"> |
| 388 |
<?php submit_button( |
| 389 |
esc_html__($buttonText, 'ali2woo'), |
| 390 |
'primary', 'a2wl_bulk_order_sync_manual', |
| 391 |
false); |
| 392 |
?> |
| 393 |
<h2 style="display: inline-block; margin-top: 3px;"><?php echo wc_help_tip($hintText); ?></h2> |
| 394 |
</div> |
| 395 |
<?php |
| 396 |
} |
| 397 |
} |
| 398 |
} |
| 399 |
|