384 lines
11 KiB
PHP
384 lines
11 KiB
PHP
<?php
|
|
/**
|
|
* AJAX处理类
|
|
*
|
|
* @package YooneSubscriptions
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Yoone_Ajax类
|
|
* 处理所有AJAX请求
|
|
*/
|
|
class Yoone_Ajax {
|
|
|
|
/**
|
|
* 构造函数
|
|
*/
|
|
public function __construct() {
|
|
$this->init_hooks();
|
|
}
|
|
|
|
/**
|
|
* 初始化钩子
|
|
*/
|
|
private function init_hooks() {
|
|
// 前端AJAX钩子
|
|
add_action('wp_ajax_yoone_calculate_bundle_price', array($this, 'calculate_bundle_price'));
|
|
add_action('wp_ajax_nopriv_yoone_calculate_bundle_price', array($this, 'calculate_bundle_price'));
|
|
|
|
add_action('wp_ajax_yoone_add_bundle_to_cart', array($this, 'add_bundle_to_cart'));
|
|
add_action('wp_ajax_nopriv_yoone_add_bundle_to_cart', array($this, 'add_bundle_to_cart'));
|
|
|
|
add_action('wp_ajax_yoone_update_subscription', array($this, 'update_subscription'));
|
|
add_action('wp_ajax_yoone_cancel_subscription', array($this, 'cancel_subscription'));
|
|
add_action('wp_ajax_yoone_pause_subscription', array($this, 'pause_subscription'));
|
|
add_action('wp_ajax_yoone_resume_subscription', array($this, 'resume_subscription'));
|
|
|
|
// 后端AJAX钩子
|
|
add_action('wp_ajax_yoone_search_products', array($this, 'search_products'));
|
|
add_action('wp_ajax_yoone_add_bundle_item', array($this, 'add_bundle_item'));
|
|
add_action('wp_ajax_yoone_remove_bundle_item', array($this, 'remove_bundle_item'));
|
|
add_action('wp_ajax_yoone_test_moneris_connection', array($this, 'test_moneris_connection'));
|
|
}
|
|
|
|
/**
|
|
* 计算混装价格
|
|
*/
|
|
public function calculate_bundle_price() {
|
|
check_ajax_referer('yoone_frontend_nonce', 'nonce');
|
|
|
|
$product_id = intval($_POST['product_id']);
|
|
$quantities = isset($_POST['quantities']) ? array_map('intval', $_POST['quantities']) : array();
|
|
|
|
if (!$product_id) {
|
|
wp_send_json_error('无效的产品ID');
|
|
}
|
|
|
|
$bundle = new Yoone_Bundle();
|
|
$bundle_data = $bundle->get_bundle_by_product_id($product_id);
|
|
|
|
if (!$bundle_data) {
|
|
wp_send_json_error('未找到混装数据');
|
|
}
|
|
|
|
$total_price = $bundle->calculate_bundle_price($product_id, $quantities);
|
|
|
|
wp_send_json_success(array(
|
|
'total_price' => $total_price,
|
|
'formatted_price' => wc_price($total_price)
|
|
));
|
|
}
|
|
|
|
/**
|
|
* 添加混装到购物车
|
|
*/
|
|
public function add_bundle_to_cart() {
|
|
check_ajax_referer('yoone_frontend_nonce', 'nonce');
|
|
|
|
$product_id = intval($_POST['product_id']);
|
|
$quantities = isset($_POST['quantities']) ? array_map('intval', $_POST['quantities']) : array();
|
|
|
|
if (!$product_id) {
|
|
wp_send_json_error('无效的产品ID');
|
|
}
|
|
|
|
$bundle = new Yoone_Bundle();
|
|
$bundle_data = $bundle->get_bundle_by_product_id($product_id);
|
|
|
|
if (!$bundle_data) {
|
|
wp_send_json_error('未找到混装数据');
|
|
}
|
|
|
|
// 添加混装到购物车
|
|
$cart_item_key = WC()->cart->add_to_cart($product_id, 1, 0, array(), array(
|
|
'yoone_bundle' => true,
|
|
'yoone_bundle_items' => $quantities
|
|
));
|
|
|
|
if ($cart_item_key) {
|
|
wp_send_json_success(array(
|
|
'message' => '混装产品已添加到购物车',
|
|
'cart_count' => WC()->cart->get_cart_contents_count()
|
|
));
|
|
} else {
|
|
wp_send_json_error('添加到购物车失败');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 更新订阅
|
|
*/
|
|
public function update_subscription() {
|
|
check_ajax_referer('yoone_frontend_nonce', 'nonce');
|
|
|
|
if (!is_user_logged_in()) {
|
|
wp_send_json_error('请先登录');
|
|
}
|
|
|
|
$subscription_id = intval($_POST['subscription_id']);
|
|
$action = sanitize_text_field($_POST['action_type']);
|
|
|
|
if (!$subscription_id) {
|
|
wp_send_json_error('无效的订阅ID');
|
|
}
|
|
|
|
$subscription = new Yoone_Subscription($subscription_id);
|
|
|
|
if (!$subscription->get_id() || $subscription->get_customer_id() !== get_current_user_id()) {
|
|
wp_send_json_error('订阅不存在或无权限');
|
|
}
|
|
|
|
$result = false;
|
|
$message = '';
|
|
|
|
switch ($action) {
|
|
case 'pause':
|
|
$result = $subscription->pause();
|
|
$message = $result ? '订阅已暂停' : '暂停订阅失败';
|
|
break;
|
|
case 'resume':
|
|
$result = $subscription->resume();
|
|
$message = $result ? '订阅已恢复' : '恢复订阅失败';
|
|
break;
|
|
case 'cancel':
|
|
$result = $subscription->cancel();
|
|
$message = $result ? '订阅已取消' : '取消订阅失败';
|
|
break;
|
|
default:
|
|
wp_send_json_error('无效的操作');
|
|
}
|
|
|
|
if ($result) {
|
|
wp_send_json_success(array(
|
|
'message' => $message,
|
|
'status' => $subscription->get_status()
|
|
));
|
|
} else {
|
|
wp_send_json_error($message);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 取消订阅
|
|
*/
|
|
public function cancel_subscription() {
|
|
check_ajax_referer('yoone_frontend_nonce', 'nonce');
|
|
|
|
if (!is_user_logged_in()) {
|
|
wp_send_json_error('请先登录');
|
|
}
|
|
|
|
$subscription_id = intval($_POST['subscription_id']);
|
|
$reason = sanitize_textarea_field($_POST['reason']);
|
|
|
|
if (!$subscription_id) {
|
|
wp_send_json_error('无效的订阅ID');
|
|
}
|
|
|
|
$subscription = new Yoone_Subscription($subscription_id);
|
|
|
|
if (!$subscription->get_id() || $subscription->get_customer_id() !== get_current_user_id()) {
|
|
wp_send_json_error('订阅不存在或无权限');
|
|
}
|
|
|
|
$result = $subscription->cancel($reason);
|
|
|
|
if ($result) {
|
|
wp_send_json_success(array(
|
|
'message' => '订阅已成功取消',
|
|
'status' => $subscription->get_status()
|
|
));
|
|
} else {
|
|
wp_send_json_error('取消订阅失败');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 暂停订阅
|
|
*/
|
|
public function pause_subscription() {
|
|
check_ajax_referer('yoone_frontend_nonce', 'nonce');
|
|
|
|
if (!is_user_logged_in()) {
|
|
wp_send_json_error('请先登录');
|
|
}
|
|
|
|
$subscription_id = intval($_POST['subscription_id']);
|
|
|
|
if (!$subscription_id) {
|
|
wp_send_json_error('无效的订阅ID');
|
|
}
|
|
|
|
$subscription = new Yoone_Subscription($subscription_id);
|
|
|
|
if (!$subscription->get_id() || $subscription->get_customer_id() !== get_current_user_id()) {
|
|
wp_send_json_error('订阅不存在或无权限');
|
|
}
|
|
|
|
$result = $subscription->pause();
|
|
|
|
if ($result) {
|
|
wp_send_json_success(array(
|
|
'message' => '订阅已暂停',
|
|
'status' => $subscription->get_status()
|
|
));
|
|
} else {
|
|
wp_send_json_error('暂停订阅失败');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 恢复订阅
|
|
*/
|
|
public function resume_subscription() {
|
|
check_ajax_referer('yoone_frontend_nonce', 'nonce');
|
|
|
|
if (!is_user_logged_in()) {
|
|
wp_send_json_error('请先登录');
|
|
}
|
|
|
|
$subscription_id = intval($_POST['subscription_id']);
|
|
|
|
if (!$subscription_id) {
|
|
wp_send_json_error('无效的订阅ID');
|
|
}
|
|
|
|
$subscription = new Yoone_Subscription($subscription_id);
|
|
|
|
if (!$subscription->get_id() || $subscription->get_customer_id() !== get_current_user_id()) {
|
|
wp_send_json_error('订阅不存在或无权限');
|
|
}
|
|
|
|
$result = $subscription->resume();
|
|
|
|
if ($result) {
|
|
wp_send_json_success(array(
|
|
'message' => '订阅已恢复',
|
|
'status' => $subscription->get_status()
|
|
));
|
|
} else {
|
|
wp_send_json_error('恢复订阅失败');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 搜索产品(后台使用)
|
|
*/
|
|
public function search_products() {
|
|
check_ajax_referer('yoone_admin_nonce', 'nonce');
|
|
|
|
if (!current_user_can('manage_woocommerce')) {
|
|
wp_send_json_error('权限不足');
|
|
}
|
|
|
|
$term = sanitize_text_field($_POST['term']);
|
|
|
|
if (strlen($term) < 2) {
|
|
wp_send_json_error('搜索词太短');
|
|
}
|
|
|
|
$products = wc_get_products(array(
|
|
'limit' => 20,
|
|
'status' => 'publish',
|
|
's' => $term,
|
|
'return' => 'objects'
|
|
));
|
|
|
|
$results = array();
|
|
foreach ($products as $product) {
|
|
$results[] = array(
|
|
'id' => $product->get_id(),
|
|
'text' => $product->get_name() . ' (#' . $product->get_id() . ')',
|
|
'price' => $product->get_price()
|
|
);
|
|
}
|
|
|
|
wp_send_json_success($results);
|
|
}
|
|
|
|
/**
|
|
* 添加混装商品项
|
|
*/
|
|
public function add_bundle_item() {
|
|
check_ajax_referer('yoone_admin_nonce', 'nonce');
|
|
|
|
if (!current_user_can('manage_woocommerce')) {
|
|
wp_send_json_error('权限不足');
|
|
}
|
|
|
|
$bundle_id = intval($_POST['bundle_id']);
|
|
$product_id = intval($_POST['product_id']);
|
|
$quantity = intval($_POST['quantity']);
|
|
|
|
if (!$bundle_id || !$product_id || !$quantity) {
|
|
wp_send_json_error('参数无效');
|
|
}
|
|
|
|
$bundle = new Yoone_Bundle($bundle_id);
|
|
$result = $bundle->add_item($product_id, $quantity);
|
|
|
|
if ($result) {
|
|
wp_send_json_success('商品已添加到混装');
|
|
} else {
|
|
wp_send_json_error('添加失败');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 移除混装商品项
|
|
*/
|
|
public function remove_bundle_item() {
|
|
check_ajax_referer('yoone_admin_nonce', 'nonce');
|
|
|
|
if (!current_user_can('manage_woocommerce')) {
|
|
wp_send_json_error('权限不足');
|
|
}
|
|
|
|
$item_id = intval($_POST['item_id']);
|
|
|
|
if (!$item_id) {
|
|
wp_send_json_error('参数无效');
|
|
}
|
|
|
|
$bundle = new Yoone_Bundle();
|
|
$result = $bundle->remove_item($item_id);
|
|
|
|
if ($result) {
|
|
wp_send_json_success('商品已从混装中移除');
|
|
} else {
|
|
wp_send_json_error('移除失败');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 测试 Moneris 连接
|
|
*/
|
|
public function test_moneris_connection() {
|
|
check_ajax_referer('yoone_admin_nonce', 'nonce');
|
|
|
|
if (!current_user_can('manage_woocommerce')) {
|
|
wp_send_json_error('权限不足');
|
|
}
|
|
|
|
$store_id = sanitize_text_field($_POST['store_id']);
|
|
$api_token = sanitize_text_field($_POST['api_token']);
|
|
$testmode = isset($_POST['testmode']) && $_POST['testmode'] === 'yes';
|
|
|
|
if (!$store_id || !$api_token) {
|
|
wp_send_json_error('Store ID 和 API Token 不能为空');
|
|
}
|
|
|
|
$gateway = new Yoone_Moneris_Gateway();
|
|
$result = $gateway->test_connection($store_id, $api_token, $testmode);
|
|
|
|
if ($result) {
|
|
wp_send_json_success('Moneris 连接测试成功');
|
|
} else {
|
|
wp_send_json_error('Moneris 连接测试失败');
|
|
}
|
|
}
|
|
} |