File: /www/wwwroot/www.cytocare.cn/wp-content/themes/blocksy-child/functions-不传递地址信息-2024.8.18.php
<?php
if (! defined('WP_DEBUG')) {
die( 'Direct access forbidden.' );
}
add_action( 'wp_enqueue_scripts', function () {
wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
});
/* 这里是B网站。以下代码是接收A网站订单数据与支付成功后发送通知给A网站
代码请放在主题functions.php文件
*/
// 设置用于后期灵活修改的域名变量
$first_website_domain = 'https://www.hotgirlscard.com';
// 添加A网站商品到购物车
function add_product_to_cart_from_query() {
// Check if we are on the checkout page, and not in the admin area
if (!is_admin() && is_page('checkout')) {
// Check if the required query parameters are set
if (isset($_GET['product_id']) && isset($_GET['quantity']) && isset($_GET['uuid'])) {
$product_id = intval($_GET['product_id']);
$quantity = intval($_GET['quantity']);
$external_uuid = sanitize_text_field($_GET['uuid']); // 获取来自第一个网站的UUID
// Retrieve the cart object
$cart = WC()->cart;
// 清空现有购物车内容
$cart->empty_cart();
// Retrieve the product to validate if it exists and can be purchased
$product = wc_get_product($product_id);
if ($product && $quantity > 0) {
// Add the new product to the cart
$cart->add_to_cart($product_id, $quantity);
// Store the external UUID in session for later use
WC()->session->set('external_uuid', $external_uuid);
// Optionally redirect to prevent duplicate additions on page refresh
wp_safe_redirect(esc_url(remove_query_arg(array('product_id', 'quantity', 'uuid'))));
exit;
}
}
}
}
add_action('wp', 'add_product_to_cart_from_query');
// 在订单创建时添加来自A网站的UUID到订单
function add_external_uuid_to_order($order, $data) {
// 从session中获取外部UUID
$external_uuid = WC()->session->get('external_uuid');
if ($external_uuid) {
// 添加UUID信息至订单备注或元数据
// $order->add_order_note('来自A网站的订单UUID: ' . $external_uuid);
$order->update_meta_data('external_uuid', $external_uuid);
// 清除session中的UUID,以避免影响后续订单
WC()->session->__unset('external_uuid');
}
}
add_action('woocommerce_checkout_create_order', 'add_external_uuid_to_order', 20, 2);
// 通知A网站订单状态
function notify_first_website_order_status($uuid) {
global $first_website_domain;
// $url = $first_website_domain .'/update-order-status';
$url = $first_website_domain .'/wp-json/custom-payment/v1/update-order-status'; // 使用正确的第一个网站 URL
$args = array(
'body' => json_encode(array('uuid' => $uuid, 'status' => 'processing')),
'headers' => array('Content-Type' => 'application/json'),
'timeout' => 45,
);
$response = wp_remote_post($url, $args);
if (is_wp_error($response)) {
// 处理错误,例如记录错误日志
// error_log('Error notifying first website: ' . $response->get_error_message());
}
}
// 通知A网站订单状态并且跳转
function notify_first_website_and_redirect($order_id) {
global $first_website_domain;
$order = wc_get_order($order_id);
$external_uuid = $order->get_meta('external_uuid');
if ($external_uuid) {
// 通知A网站更新订单状态
notify_first_website_order_status($external_uuid);
/*
// 构建重定向到A网站的URL 支付成功后支付插件会跳转到Thank you页面,这个跳转逻辑一般不生效
$redirect_url = $first_website_domain . '/my-account/orders?uuid=' . urlencode($external_uuid);
// $redirect_url = $first_website_domain . '/order-info?uuid=' . urlencode($external_uuid);
// 执行重定向
wp_redirect($redirect_url);
exit;
*/
}
}
//支付完成后运行 支付完成后逻辑暂不执行,改由在Thank页面执行
//add_action('woocommerce_payment_complete', 'notify_first_website_and_redirect');
//在Thankyou页面更新订单信息进行跳转
function custom_redirect_after_payment($order_id) {
global $first_website_domain;
if (!$order_id) {
return;
}
$order = wc_get_order($order_id);
// 校验订单是否存在以及订单状态是否为已处理
if ($order && $order->has_status('processing')) {
$external_uuid = $order->get_meta('external_uuid'); // 假设已经存储了 UUID
// 通知A网站更新订单状态
notify_first_website_order_status($external_uuid);
// 构建重定向 URL,到第一个网站的订单信息页面
$redirect_url = $first_website_domain . '/my-account/orders?uuid=' . urlencode($external_uuid);
// 执行重定向
wp_redirect($redirect_url);
exit;
}
}
add_action('woocommerce_thankyou', 'custom_redirect_after_payment', 10, 1);
/* 网站B代码结束 */