| 1 |
<?php |
| 2 |
/* * class |
| 3 |
* Description of OrderDataTabController |
| 4 |
* |
| 5 |
* @author Ali2Woo Team |
| 6 |
* |
| 7 |
* @autoload: a2wl_admin_init |
| 8 |
* |
| 9 |
* @ajax: true |
| 10 |
*/ |
| 11 |
// phpcs:ignoreFile WordPress.Security.NonceVerification.Recommended |
| 12 |
namespace AliNext_Lite;; |
| 13 |
use Automattic\WooCommerce\Utilities\OrderUtil; |
| 14 |
use Pages; |
| 15 |
use Throwable; |
| 16 |
|
| 17 |
class OrderDataTabController extends AbstractController |
| 18 |
{ |
| 19 |
public function __construct() |
| 20 |
{ |
| 21 |
parent::__construct(); |
| 22 |
|
| 23 |
//the following 4 methods are shared with tracking plugin |
| 24 |
add_action('admin_enqueue_scripts', [$this, 'assets']); |
| 25 |
add_action('woocommerce_admin_order_data_after_order_details', array($this, 'woocommerce_admin_order_data_after_order_details')); |
| 26 |
add_action('woocommerce_process_shop_order_meta', array($this, 'woocommerce_process_shop_order_meta')); |
| 27 |
add_action('wp_ajax_a2wl_reset_order_data', [$this, 'ajax_reset_order_data']); |
| 28 |
|
| 29 |
//Modify order item data |
| 30 |
add_filter('woocommerce_order_item_display_meta_key', array($this, 'woocommerce_order_item_display_meta_key'), 99, 3); |
| 31 |
add_filter('woocommerce_order_item_display_meta_value', array($this, 'woocommerce_order_item_display_meta_value'), 10, 3); |
| 32 |
|
| 33 |
//Filter order meta in emails |
| 34 |
add_filter('woocommerce_email_order_items_args', array($this, 'woocommerce_email_order_items_args'), 10); |
| 35 |
} |
| 36 |
|
| 37 |
public function assets(): void |
| 38 |
{ |
| 39 |
if (function_exists('WC') && OrderUtil::custom_orders_table_usage_is_enabled()) { |
| 40 |
$currentPage = $_REQUEST['page'] ?? false; |
| 41 |
$orderId = $_REQUEST['id'] ?? false; |
| 42 |
if ($currentPage === 'wc-orders' && $orderId !== false) { |
| 43 |
$this->enqueueAssets(); |
| 44 |
} |
| 45 |
} else { |
| 46 |
if (isset($_GET['post']) && isset($_GET['action']) && $_GET['action'] === 'edit') { |
| 47 |
$this->enqueueAssets(); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
if (wp_script_is('a2wl-wc-order-edit-script', 'enqueued')) { |
| 52 |
$data = [ |
| 53 |
'ajaxurl' => admin_url('admin-ajax.php'), |
| 54 |
'nonce_action' => wp_create_nonce(self::AJAX_NONCE_ACTION), |
| 55 |
]; |
| 56 |
wp_localize_script('a2wl-wc-order-edit-script', 'a2wl_order_edit_script', $data); |
| 57 |
} |
| 58 |
|
| 59 |
if (!wp_script_is('a2wl-admin-script', 'enqueued')) { |
| 60 |
wp_enqueue_script('a2wl-admin-script', |
| 61 |
A2WL()->plugin_url() . '/assets/js/admin_script.js', |
| 62 |
array('jquery'), |
| 63 |
A2WL()->version |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
AbstractAdminPage::localizeAdminScript(); |
| 68 |
} |
| 69 |
|
| 70 |
public function ajax_reset_order_data(): void |
| 71 |
{ |
| 72 |
check_admin_referer(self::AJAX_NONCE_ACTION, self::NONCE); |
| 73 |
|
| 74 |
if (!PageGuardHelper::canAccessPage(Pages::IMPORT_LIST)) { |
| 75 |
$result = ResultBuilder::buildError($this->getErrorTextNoPermissions()); |
| 76 |
echo wp_json_encode($result); |
| 77 |
wp_die(); |
| 78 |
} |
| 79 |
|
| 80 |
$result = ResultBuilder::buildOk(); |
| 81 |
|
| 82 |
try { |
| 83 |
$order_id = (int) $_POST['id']; |
| 84 |
$order = wc_get_order($order_id); |
| 85 |
if ($order) { |
| 86 |
$order_items = $order->get_items(); |
| 87 |
foreach ($order_items as $item_id => $item) { |
| 88 |
$item->delete_meta_data(Constants::order_item_external_order_meta()); |
| 89 |
$item->delete_meta_data(Constants::order_item_tracking_data_meta()); |
| 90 |
$item->save(); |
| 91 |
} |
| 92 |
$order->add_order_note(esc_html__('The order external ID(s) and tracking numbers have been reset.', 'ali2woo'), false, true); |
| 93 |
} else { |
| 94 |
$result = ResultBuilder::buildError('did not find the order id: №' . $order_id); |
| 95 |
} |
| 96 |
restore_error_handler(); |
| 97 |
} catch (Throwable $e) { |
| 98 |
a2wl_print_throwable($e); |
| 99 |
$result = ResultBuilder::buildError($e->getMessage()); |
| 100 |
} |
| 101 |
|
| 102 |
echo wp_json_encode($result); |
| 103 |
wp_die(); |
| 104 |
} |
| 105 |
|
| 106 |
public function woocommerce_admin_order_data_after_order_details($WC_Order): void |
| 107 |
{ |
| 108 |
?> |
| 109 |
|
| 110 |
<br class="clear" /> |
| 111 |
<h3><?php esc_html_e('A2WL Order Data', 'ali2woo')?><a href="#" class="edit_address"><?php esc_html_e('Edit')?></a></h3> |
| 112 |
|
| 113 |
<div class="address order_items"> |
| 114 |
|
| 115 |
<?php |
| 116 |
$order_items = $WC_Order->get_items(); |
| 117 |
|
| 118 |
?> |
| 119 |
|
| 120 |
<?php if (count($order_items)): ?> |
| 121 |
<p> |
| 122 |
<?php foreach ($order_items as $item_id => $item): ?> |
| 123 |
|
| 124 |
|
| 125 |
<?php |
| 126 |
|
| 127 |
$a2wl_order_item = new WooCommerceOrderItem($item); |
| 128 |
|
| 129 |
$external_order_id = $a2wl_order_item->get_external_order_id(); |
| 130 |
|
| 131 |
$ali_order_link = $a2wl_order_item->get_ali_order_item_link(); |
| 132 |
|
| 133 |
$order_item_tracking_codes = $a2wl_order_item->get_formated_tracking_codes(); |
| 134 |
|
| 135 |
//$carrier_link = $a2wl_order_item->get_formated_carrier_link(); |
| 136 |
|
| 137 |
$carrier_name = $a2wl_order_item->get_carrier_name(); |
| 138 |
|
| 139 |
?> |
| 140 |
|
| 141 |
<strong><?php esc_html_e('Item', 'ali2woo')?>:</strong> <a target="_blank" href="<?php echo get_edit_post_link($item['product_id']); ?>"><?php echo $this->shorten_display_value($item->get_name()); ?></a> |
| 142 |
<a class="product_view" target="_blank" title="<?php esc_html_e('View product', 'ali2woo')?>" href="<?php echo get_permalink($item['product_id']); ?>"></a><br/> |
| 143 |
<strong><?php esc_html_e('AliExpress order ID', 'ali2woo')?>:</strong> <?php echo $ali_order_link; ?><br/> |
| 144 |
<strong><?php esc_html_e('Tracking numbers', 'ali2woo')?>:</strong> <?php echo $order_item_tracking_codes; ?><br/> |
| 145 |
<?php if ($carrier_name): ?><strong><?php esc_html_e('Carrier', 'ali2woo')?>:</strong> <?php echo $carrier_name; ?><br/><?php endif;?> |
| 146 |
<br/> |
| 147 |
<?php endforeach;?> |
| 148 |
</p> |
| 149 |
<?php endif;?> |
| 150 |
|
| 151 |
<a role="button" class="a2wl_reset_order_data" href="#"><?php esc_html_e('Reset A2W Order Data', 'ali2woo')?></a> |
| 152 |
|
| 153 |
<?php |
| 154 |
|
| 155 |
/** todo: In previous plugin version the Tracking numbers and external order id are save in other metas |
| 156 |
* in the order data instead of order item data |
| 157 |
* The plugin still shows this data in read-only format, it will be removed completely in the future plugin version |
| 158 |
**/ |
| 159 |
$order_external_id_array = get_post_meta($WC_Order->get_id(), Constants::old_order_external_order_id()); |
| 160 |
|
| 161 |
$order_tracking_codes = get_post_meta($WC_Order->get_id(), Constants::old_order_tracking_code()); |
| 162 |
|
| 163 |
?> |
| 164 |
|
| 165 |
<?php if ($order_external_id_array || $order_tracking_codes): ?> |
| 166 |
<hr/> |
| 167 |
<p><?php esc_html_e('Old order data goes below. It will be removed completely in a future versions of the Ali2Woo plugin.', 'ali2woo')?></p> |
| 168 |
<?php endif;?> |
| 169 |
|
| 170 |
<?php if ($order_external_id_array): ?> |
| 171 |
<?php esc_html_e('AliExpress order ID(s)', 'ali2woo')?>: |
| 172 |
<div class="a2wl_external_order_id"> |
| 173 |
<?php foreach ($order_external_id_array as $k => $order_external_id): ?> |
| 174 |
<?php echo htmlentities($order_external_id) ?><?php if ($k < count($order_external_id_array) - 1): ?>,<?php endif;?> |
| 175 |
<?php endforeach;?> |
| 176 |
</div> |
| 177 |
|
| 178 |
<?php endif;?> |
| 179 |
|
| 180 |
|
| 181 |
<?php if ($order_tracking_codes): ?> |
| 182 |
<?php esc_html_e('AliExpress tracking numbers', 'ali2woo')?>: |
| 183 |
<div class="a2wl_tracking_code_data"> |
| 184 |
<?php foreach ($order_tracking_codes as $k => $tracking_code): ?> |
| 185 |
<?php echo htmlentities($tracking_code) ?><?php if ($k < count($order_tracking_codes) - 1): ?>,<?php endif;?> |
| 186 |
<?php endforeach;?> |
| 187 |
</div> |
| 188 |
|
| 189 |
<?php endif;?> |
| 190 |
|
| 191 |
</div> |
| 192 |
<div class="edit_address"> |
| 193 |
|
| 194 |
<?php if (count($order_items)): ?> |
| 195 |
<?php foreach ($order_items as $item_id => $item): ?> |
| 196 |
<div class="clear"></div> |
| 197 |
<h3><?php esc_html_e('Item', 'ali2woo')?>: <span><a target="_blank" href="<?php echo get_edit_post_link($item['product_id']); ?>"><?php echo $this->shorten_display_value($item->get_name()); ?></a> <a class="product_view" target="_blank" title="<?php esc_html_e('View product', 'ali2woo')?>" href="<?php echo get_permalink($item['product_id']); ?>"></a></span></h3> |
| 198 |
|
| 199 |
<?php |
| 200 |
|
| 201 |
$a2wl_order_item = new WooCommerceOrderItem($item); |
| 202 |
|
| 203 |
$external_order_id = $a2wl_order_item->get_external_order_id(); |
| 204 |
|
| 205 |
$order_item_tracking_codes = $a2wl_order_item->get_formated_tracking_codes(true); |
| 206 |
?> |
| 207 |
|
| 208 |
<?php woocommerce_wp_text_input(array( |
| 209 |
'id' => 'a2wl_external_order_id_' . $item_id, |
| 210 |
'name' => 'a2wl_external_order_id_' . $item_id, |
| 211 |
'label' => esc_html__('AliExpress Order ID', 'ali2woo'), |
| 212 |
'value' => $external_order_id, |
| 213 |
'wrapper_class' => 'form-field-wide', |
| 214 |
)); |
| 215 |
?> |
| 216 |
|
| 217 |
<?php woocommerce_wp_text_input(array( |
| 218 |
'id' => 'a2wl_tracking_code_' . $item_id, |
| 219 |
'name' => 'a2wl_tracking_code_' . $item_id, |
| 220 |
'label' => esc_html__('Tracking numbers', 'ali2woo'), |
| 221 |
/* Here it can be an array of Tracking numbers, because sometimes AliExpress replace old tracking code with new one for some orders */ |
| 222 |
'value' => $order_item_tracking_codes, |
| 223 |
'wrapper_class' => 'form-field-wide', |
| 224 |
)); |
| 225 |
?> |
| 226 |
|
| 227 |
<?php endforeach;?> |
| 228 |
<?php endif;?> |
| 229 |
</div> |
| 230 |
<?php } |
| 231 |
|
| 232 |
public function woocommerce_process_shop_order_meta($order_id): void |
| 233 |
{ |
| 234 |
$order = wc_get_order($order_id); |
| 235 |
$order_items = $order->get_items(); |
| 236 |
|
| 237 |
foreach ($order_items as $item_id => $item) { |
| 238 |
|
| 239 |
$a2wl_order_item = new WooCommerceOrderItem($item); |
| 240 |
|
| 241 |
if (isset($_POST['a2wl_external_order_id_' . $item_id]) && !empty($_POST['a2wl_external_order_id_' . $item_id])) { |
| 242 |
|
| 243 |
$external_order_id = intval($_POST['a2wl_external_order_id_' . $item_id]); |
| 244 |
|
| 245 |
$a2wl_order_item->update_external_order($external_order_id); |
| 246 |
|
| 247 |
} |
| 248 |
|
| 249 |
if (isset($_POST['a2wl_tracking_code_' . $item_id]) && !empty($_POST['a2wl_tracking_code_' . $item_id])) { |
| 250 |
$tracking_codes = explode(',', $_POST['a2wl_tracking_code_' . $item_id]); |
| 251 |
$tracking_codes = array_map('trim', $tracking_codes); |
| 252 |
|
| 253 |
$a2wl_order_item->update_tracking_codes($tracking_codes); |
| 254 |
|
| 255 |
} |
| 256 |
|
| 257 |
if ($a2wl_order_item->save()) { |
| 258 |
$order->add_order_note(esc_html__('The order external ID(s) and/or tracking numbers have been added to the order manually.', 'ali2woo'), false, true); |
| 259 |
} |
| 260 |
|
| 261 |
} |
| 262 |
|
| 263 |
} |
| 264 |
|
| 265 |
public function woocommerce_order_item_display_meta_key($display_key, $meta, $item) |
| 266 |
{ |
| 267 |
|
| 268 |
//todo: maybe remove this code? THese metas are not displayed on th order edit page |
| 269 |
|
| 270 |
if ($meta->key === Shipping::get_order_item_shipping_meta_key()) { |
| 271 |
$display_key = esc_html__('Shipping Info', 'ali2woo'); |
| 272 |
} |
| 273 |
|
| 274 |
if ($meta->key == Constants::order_item_external_order_meta()) { |
| 275 |
$display_key = esc_html__('AliExpress Order ID', 'ali2woo'); |
| 276 |
} |
| 277 |
|
| 278 |
if ($meta->key == Constants::order_item_tracking_data_meta()) { |
| 279 |
$display_key = esc_html__('Tracking numbers', 'ali2woo'); |
| 280 |
} |
| 281 |
|
| 282 |
return $display_key; |
| 283 |
} |
| 284 |
|
| 285 |
public function woocommerce_order_item_display_meta_value($display_value, $meta, $item) |
| 286 |
{ |
| 287 |
if ($meta->key === Shipping::get_order_item_shipping_meta_key()) { |
| 288 |
$value = $meta->value; |
| 289 |
if ($value) { |
| 290 |
$order_id = $item->get_order_id(); |
| 291 |
$display_value = Shipping::get_formated_shipping_info_meta($order_id, $value); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
if ($meta->key == Constants::order_item_tracking_data_meta()) { |
| 296 |
$display_value = $this->get_formated_order_item_tracking_data_meta($value); |
| 297 |
} |
| 298 |
|
| 299 |
return $display_value; |
| 300 |
|
| 301 |
} |
| 302 |
|
| 303 |
public function woocommerce_email_order_items_args($args) |
| 304 |
{ |
| 305 |
|
| 306 |
if ($args['sent_to_admin']) { |
| 307 |
return $args; |
| 308 |
} |
| 309 |
|
| 310 |
//a little hack, save in global that we are in the email template |
| 311 |
$_POST['a2wl_email_template_check'] = true; |
| 312 |
|
| 313 |
return $args; |
| 314 |
|
| 315 |
} |
| 316 |
|
| 317 |
private function get_formated_order_item_tracking_data_meta($value) |
| 318 |
{ |
| 319 |
|
| 320 |
$tracking_codes = ""; |
| 321 |
|
| 322 |
if ($value && isset($value['tracking_codes'])) { |
| 323 |
$tracking_codes = implode(",", $value['tracking_codes']); |
| 324 |
} |
| 325 |
|
| 326 |
return $tracking_codes; |
| 327 |
} |
| 328 |
|
| 329 |
private function shorten_display_value($string) |
| 330 |
{ |
| 331 |
if (strlen($string) >= 40) { |
| 332 |
return substr($string, 0, 30) . " ... " . substr($string, -10); |
| 333 |
} else { |
| 334 |
return $string; |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
private function enqueueAssets(): void |
| 339 |
{ |
| 340 |
wp_enqueue_script('a2wl-wc-order-edit-script', A2WL()->plugin_url() . '/assets/js/wc_order_edit.js', [], A2WL()->version); |
| 341 |
wp_enqueue_style('a2wl-wc-order-edit-style', A2WL()->plugin_url() . '/assets/css/wc_order_edit.css', [], A2WL()->version); |
| 342 |
} |
| 343 |
|
| 344 |
} |
| 345 |
|