czlexpress-for-woocommerce/includes/class-czl-label.php
wood chen 64e952903a Update system requirements and enhance plugin functionality
- Updated system requirements in the README to reflect compatibility with WordPress 6.0+ and PHP 7.4+.
- Revised usage steps for better clarity on plugin installation and configuration.
- Improved order management features, including AJAX handling for shipment creation and tracking updates.
- Added new custom order statuses (In Transit, Delivered) for better order tracking.
- Enhanced error handling and logging during shipment creation and tracking updates.
- Introduced chargeable weight calculation for shipping rates based on product dimensions and weight.

These changes enhance the overall performance and usability of the CZL Express plugin.
2024-12-20 03:03:12 +08:00

94 lines
2.9 KiB
PHP

<?php
class CZL_Label {
private static $api;
public static function init() {
self::$api = new CZL_API();
}
/**
* 获取标签URL
*/
public static function get_label_url($order_id) {
try {
$order = wc_get_order($order_id);
if (!$order) {
throw new Exception('Order not found');
}
// 获取运单号和订单号
$tracking_number = $order->get_meta('_czl_tracking_number');
$czl_order_id = $order->get_meta('_czl_order_id');
// 构建标签URL
$base_url = 'https://tms.czl.net/printOrderLabel.htm';
$params = array();
// 优先使用订单号
if (!empty($czl_order_id)) {
$params['order_id'] = $czl_order_id;
$url = add_query_arg($params, $base_url);
return $url;
}
// 如果没有订单号但有运单号,使用运单号
if (!empty($tracking_number)) {
$params['documentCode'] = $tracking_number;
$url = add_query_arg($params, $base_url);
return $url;
}
throw new Exception('No tracking number or order ID found');
} catch (Exception $e) {
error_log('CZL Express Error: Failed to get label URL - ' . $e->getMessage());
throw $e;
}
}
/**
* 添加标签打印按钮
*/
public static function add_print_actions($actions, $order) {
try {
$url = self::get_label_url($order->get_id());
if ($url) {
$actions['czl_print_label'] = array(
'url' => wp_nonce_url(admin_url('admin-ajax.php?action=czl_print_label&order_id=' . $order->get_id()), 'czl_print_label'),
'name' => __('打印运单', 'woo-czl-express'),
'action' => 'czl_print_label'
);
}
} catch (Exception $e) {
error_log('CZL Express Error: ' . $e->getMessage());
}
return $actions;
}
/**
* 处理标签打印请求
*/
public static function handle_print_request() {
if (!current_user_can('edit_shop_orders')) {
wp_die(__('您没有权限执行此操作', 'woo-czl-express'));
}
check_admin_referer('czl_print_label');
$order_id = isset($_GET['order_id']) ? absint($_GET['order_id']) : 0;
if (!$order_id) {
wp_die(__('订单ID无效', 'woo-czl-express'));
}
$label_url = self::get_label_url($order_id);
if (!$label_url) {
wp_die(__('未找到运单标签', 'woo-czl-express'));
}
wp_redirect($label_url);
exit;
}
}
// 在插件初始化时调用
add_action('init', array('CZL_Label', 'init'));