http_build_query可能遇到的问题 &=>【&】

解决PHP与Java中http_build_query函数不兼容问题
在PHP环境中使用http_build_query函数没有问题,但在Java中接收参数时,该函数未能正确地将&符号转换为&。本文提供了一个替代方法,通过自定义类实现http_build_query功能,确保在Java环境中也能正确处理URL参数。

http_build_query可能遇到的问题 


在php程序环境中使用这个函数没有太大问题,但是在java中接收到参数就错误了,

接收到参数就没有把【&】=>& 转换过来



解决方法:

放弃php内置函数【http_build_query】

重新写一个类似的方法:

function my_http_build_query($arr=array()) {
$keys = array_keys($arr);
foreach($keys as $k){
$out[] = $k.'='.$arr[$k];
}
return implode("&",$out);
 }


Please double check if the combination of codes are correct Functions.php cart) wc_load_cart(); $items = array(); foreach (WC()->cart->get_cart() as $ci_key => $ci) { $pid = isset($ci[‘product_id’]) ? (int)$ci[‘product_id’] : 0; $vid = isset($ci[‘variation_id’]) ? (int)$ci[‘variation_id’] : 0; $qty = isset($ci[‘quantity’]) ? wc_stock_amount($ci[‘quantity’]) : 0; $var = isset($ci[‘variation’]) && is_array($ci[‘variation’]) ? $ci[‘variation’] : array(); if ($pid && $qty > 0) { $items[] = array( ‘product_id’ => $pid, ‘variation_id’ => $vid, ‘variation’ => array_map(‘wc_clean’, $var), ‘quantity’ => $qty, ); } } return $items; } /** 从快照恢复购物车 */ function pc_restore_cart_from_items($items) { if (!WC()->cart) wc_load_cart(); WC()->cart->empty_cart(); foreach ((array)$items as $it) { $pid = isset($it[‘product_id’]) ? (int)$it[‘product_id’] : 0; $vid = isset($it[‘variation_id’]) ? (int)$it[‘variation_id’] : 0; $qty = isset($it[‘quantity’]) ? wc_stock_amount($it[‘quantity’]) : 0; $var = isset($it[‘variation’]) && is_array($it[‘variation’]) ? array_map(‘wc_clean’, $it[‘variation’]) : array(); if ($pid && $qty > 0) { WC()->cart->add_to_cart($pid, $qty, $vid, $var); } } WC()->cart->calculate_totals(); } /** 生成瞬态键名 */ function pc_transient_key($token) { return ‘pc_partial_payload_’ . sanitize_key($token); } /* ------------------------------------------------- AJAX: 当购物车为空时恢复购物车 ------------------------------------------------- */ add_action(‘wp_ajax_pc_rehydrate_cart’, ‘pc_rehydrate_cart’); add_action(‘wp_ajax_nopriv_pc_rehydrate_cart’, ‘pc_rehydrate_cart’); function pc_rehydrate_cart() { check_ajax_referer(‘woocommerce-cart’, ‘security’); $raw = isset($_POST[‘items’]) ? wp_unslash($_POST[‘items’]) : ‘’; $items = is_string($raw) ? json_decode($raw, true) : (array)$raw; if (!is_array($items)) { wp_send_json_error(array(‘message’ => ‘无效的商品数据’), 400); } if (!WC()->cart) wc_load_cart(); if (!WC()->cart->is_empty()) { wp_send_json_success(array(‘message’ => ‘购物车非空’)); } foreach ($items as $it) { $pid = isset($it[‘product_id’]) ? (int)$it[‘product_id’] : 0; $vid = isset($it[‘variation_id’]) ? (int)$it[‘variation_id’] : 0; $qty = isset($it[‘quantity’]) ? wc_stock_amount($it[‘quantity’]) : 0; $var = isset($it[‘variation’]) && is_array($it[‘variation’]) ? array_map(‘wc_clean’, $it[‘variation’]) : array(); if ($pid && $qty > 0) { WC()->cart->add_to_cart($pid, $qty, $vid, $var); } } WC()->cart->calculate_totals(); wp_send_json_success(array(‘rehydrated’ => true)); } /* ------------------------------------------------- AJAX: 更新商品数量 (无需刷新页面) ------------------------------------------------- */ add_action(‘wp_ajax_update_cart_item_qty’, ‘pc_update_cart_item_qty’); add_action(‘wp_ajax_nopriv_update_cart_item_qty’, ‘pc_update_cart_item_qty’); function pc_update_cart_item_qty() { check_ajax_referer(‘woocommerce-cart’, ‘security’); $key = isset($_POST[‘cart_item_key’]) ? wc_clean(wp_unslash($_POST[‘cart_item_key’])) : ‘’; $qty = isset($_POST[‘qty’]) ? wc_stock_amount($_POST[‘qty’]) : null; if (!$key || $qty === null) { wp_send_json_error(array(‘message’ => ‘缺少参数’), 400); } if (!WC()->cart) wc_load_cart(); if ($qty <= 0) { $removed = WC()->cart->remove_cart_item($key); WC()->cart->calculate_totals(); wp_send_json_success(array(‘removed’ => (bool)$removed)); } else { $set = WC()->cart->set_quantity($key, $qty, true); WC()->cart->calculate_totals(); $cart_item = WC()->cart->get_cart_item($key); if (!$cart_item) { wp_send_json_error(array(‘message’ => ‘更新后找不到商品’), 404); } $_product = $cart_item[‘data’]; $subtotal_html = apply_filters(‘woocommerce_cart_item_subtotal’, WC()->cart->get_product_subtotal($_product, $cart_item[‘quantity’]), $cart_item, $key); $line_total_incl_tax = (float)($cart_item[‘line_total’] + $cart_item[‘line_tax’]); wp_send_json_success(array( ‘subtotal_html’ => $subtotal_html, ‘line_total_incl_tax’ => $line_total_incl_tax, ‘removed’ => false )); } } /* ------------------------------------------------- AJAX: 删除选中商品 ------------------------------------------------- */ add_action(‘wp_ajax_remove_selected_cart_items’, ‘pc_remove_selected_cart_items’); add_action(‘wp_ajax_nopriv_remove_selected_cart_items’, ‘pc_remove_selected_cart_items’); function pc_remove_selected_cart_items() { check_ajax_referer(‘woocommerce-cart’, ‘security’); $keys = isset($_POST[‘selected_items’]) ? (array) $_POST[‘selected_items’] : array(); if (!WC()->cart) wc_load_cart(); foreach ($keys as $k) { $k = wc_clean(wp_unslash($k)); WC()->cart->remove_cart_item($k); } WC()->cart->calculate_totals(); wp_send_json_success(true); } /* ------------------------------------------------- AJAX: 清空购物车 ------------------------------------------------- */ add_action(‘wp_ajax_empty_cart’, ‘pc_empty_cart’); add_action(‘wp_ajax_nopriv_empty_cart’, ‘pc_empty_cart’); function pc_empty_cart() { check_ajax_referer(‘woocommerce-cart’, ‘security’); if (!WC()->cart) wc_load_cart(); WC()->cart->empty_cart(); wp_send_json_success(true); } /* ------------------------------------------------- AJAX: 应用优惠券 ------------------------------------------------- */ add_action(‘wp_ajax_apply_coupon’, ‘pc_apply_coupon’); add_action(‘wp_ajax_nopriv_apply_coupon’, ‘pc_apply_coupon’); function pc_apply_coupon() { check_ajax_referer(‘woocommerce-cart’, ‘security’); $code = isset($_POST[‘coupon_code’]) ? wc_format_coupon_code(wp_unslash($_POST[‘coupon_code’])) : ‘’; if (!$code) { wp_send_json_error(array(‘message’ => __(‘请输入优惠券代码’, ‘woocommerce’)), 400); } if (!WC()->cart) wc_load_cart(); $applied = WC()->cart->apply_coupon($code); WC()->cart->calculate_totals(); if (is_wp_error($applied)) { wp_send_json_error(array(‘message’ => $applied->get_error_message()), 400); } if (!$applied) { wp_send_json_error(array(‘message’ => __(‘优惠券应用失败’, ‘woocommerce’)), 400); } wp_send_json_success(true); } /* ------------------------------------------------- AJAX: 创建部分结算订单 将快照和选中商品存储在瞬态中 将会话中的token标记 返回结账URL ------------------------------------------------- */ add_action(‘wp_ajax_create_direct_order’, ‘pc_create_direct_order’); add_action(‘wp_ajax_nopriv_create_direct_order’, ‘pc_create_direct_order’); function pc_create_direct_order() { check_ajax_referer(‘woocommerce-cart’, ‘security’); // 初始化会话 if (!WC()->session) { WC()->session = new WC_Session_Handler(); WC()->session->init(); } $selected_keys = isset($_POST[‘selected_items’]) ? (array) $_POST[‘selected_items’] : array(); if (empty($selected_keys)) { wp_send_json_error(array(‘message’ => __(‘请选择要结算的商品’, ‘woocommerce’)), 400); } if (!WC()->cart) wc_load_cart(); // 创建完整购物车快照 $snapshot = pc_snapshot_current_cart(); // 提取选中商品 $selected = array(); foreach (WC()->cart->get_cart() as $ci_key => $ci) { if (!in_array($ci_key, $selected_keys, true)) { continue; } $pid = isset($ci[‘product_id’]) ? (int)$ci[‘product_id’] : 0; $vid = isset($ci[‘variation_id’]) ? (int)$ci[‘variation_id’] : 0; $qty = isset($ci[‘quantity’]) ? wc_stock_amount($ci[‘quantity’]) : 0; $var = isset($ci[‘variation’]) && is_array($ci[‘variation’]) ? array_map(‘wc_clean’, $ci[‘variation’]) : array(); if ($pid && $qty > 0) { $selected[] = array( ‘product_id’ => $pid, ‘variation_id’ => $vid, ‘variation’ => $var, ‘quantity’ => $qty, ); } } if (empty($selected)) { wp_send_json_error(array(‘message’ => __(‘没有可结算的商品’, ‘woocommerce’)), 400); } $token = wp_generate_uuid4(); $payload = array( ‘uid’ => pc_get_cart_uid(), ‘snapshot’ => $snapshot, ‘selected’ => $selected, ‘created’ => time(), ); set_transient(pc_transient_key($token), $payload, 2 * DAY_IN_SECONDS); // 将token存入会话 if (method_exists(WC()->session, ‘set’)) { WC()->session->set(‘pc_partial_token’, $token); } $checkout_url = add_query_arg(‘pc_token’, rawurlencode($token), wc_get_checkout_url()); wp_send_json_success(array(‘checkout_url’ => $checkout_url)); } /* ------------------------------------------------- 结账流程处理 根据token虚拟化购物车 处理前确保重新虚拟化 订单标记token 感谢页重建购物车 返回购物车时恢复快照 ------------------------------------------------- */ // 进入结账页时虚拟化购物车 add_action(‘woocommerce_before_checkout_form’, ‘pc_virtualize_cart_on_checkout’, 1); function pc_virtualize_cart_on_checkout() { if (!isset($_GET[‘pc_token’])) return; $token = sanitize_text_field(wp_unslash($_GET[‘pc_token’])); $payload = get_transient(pc_transient_key($token)); if (empty($payload) || empty($payload[‘selected’])) return; if (!WC()->cart) wc_load_cart(); // 仅加载选中商品 pc_restore_cart_from_items($payload[‘selected’]); // 持久化token用于后续AJAX调用 if (method_exists(WC()->session, ‘set’)) { WC()->session->set(‘pc_partial_token’, $token); } } // 订单处理前再次确保虚拟化 add_action(‘woocommerce_before_checkout_process’, ‘pc_revirtualize_before_processing’, 1); function pc_revirtualize_before_processing() { if (!method_exists(WC()->session, ‘get’)) return; $token = WC()->session->get(‘pc_partial_token’); if (!$token) return; $payload = get_transient(pc_transient_key($token)); if (empty($payload) || empty($payload[‘selected’])) return; pc_restore_cart_from_items($payload[‘selected’]); } // 订单标记token add_action(‘woocommerce_checkout_create_order’, ‘pc_tag_order_with_token’, 10, 1); function pc_tag_order_with_token($order) { $token = null; if (isset($_GET[‘pc_token’])) { $token = sanitize_text_field(wp_unslash($_GET[‘pc_token’])); } elseif (method_exists(WC()->session, ‘get’)) { $token = WC()->session->get(‘pc_partial_token’); } if ($token) { $order->update_meta_data(‘_pc_partial_token’, $token); } } // 感谢页重建购物车 add_action(‘woocommerce_thankyou’, ‘pc_rebuild_cart_on_thankyou’, 20); function pc_rebuild_cart_on_thankyou($order_id) { $order = wc_get_order($order_id); if (!$order) return; $token = $order->get_meta(‘_pc_partial_token’); if (!$token) return; $payload = get_transient(pc_transient_key($token)); if (empty($payload) || empty($payload[‘snapshot’])) { // 清理会话token if (method_exists(WC()->session, ‘set’)) { WC()->session->set(‘pc_partial_token’, null); } delete_transient(pc_transient_key($token)); return; } // 创建已购商品映射表 $purchased = array(); foreach ($order->get_items() as $item) { $pid = (int)$item->get_product_id(); $vid = (int)$item->get_variation_id(); $qty = (int)$item->get_quantity(); $k = pc_build_item_key($pid, $vid); if (!isset($purchased[$k])) $purchased[$k] = 0; $purchased[$k] += $qty; } // 剩余商品 = 快照 - 已购 $remainder = array(); foreach ($payload[‘snapshot’] as $it) { $pid = isset($it[‘product_id’]) ? (int)$it[‘product_id’] : 0; $vid = isset($it[‘variation_id’]) ? (int)$it[‘variation_id’] : 0; $qty = isset($it[‘quantity’]) ? wc_stock_amount($it[‘quantity’]) : 0; $var = isset($it[‘variation’]) ? $it[‘variation’] : array(); $k = pc_build_item_key($pid, $vid); $take = isset($purchased[$k]) ? (int)$purchased[$k] : 0; $left = max(0, $qty - $take); if ($left > 0) { $remainder[] = array( ‘product_id’ => $pid, ‘variation_id’ => $vid, ‘variation’ => $var, ‘quantity’ => $left, ); $purchased[$k] = max(0, $take - $qty); } } // 重建剩余商品的购物车 pc_restore_cart_from_items($remainder); // 清理会话token if (method_exists(WC()->session, ‘set’)) { WC()->session->set(‘pc_partial_token’, null); } delete_transient(pc_transient_key($token)); } /* ------------------------------------------------- 支付取消处理 当订单取消时恢复原始购物车 ------------------------------------------------- */ add_action(‘woocommerce_cancelled_order’, ‘pc_restore_cart_on_cancellation’); function pc_restore_cart_on_cancellation($order_id) { $order = wc_get_order($order_id); if (!$order) return; $token = $order->get_meta(‘_pc_partial_token’); if (!$token) return; $payload = get_transient(pc_transient_key($token)); if (empty($payload) || empty($payload[‘snapshot’])) { delete_transient(pc_transient_key($token)); return; } // 恢复完整购物车快照 pc_restore_cart_from_items($payload[‘snapshot’]); // 清理会话token if (method_exists(WC()->session, ‘set’)) { WC()->session->set(‘pc_partial_token’, null); } delete_transient(pc_transient_key($token)); } /* ------------------------------------------------- 跨标签页购物车同步 使用localStorage实现 ------------------------------------------------- */ add_action(‘wp_footer’, function() { if (!class_exists(‘WooCommerce’)) return; ?> session->get(‘original_cart’)) { $data[‘count’] = count($original); } return $data; }); /* ------------------------------------------------- 辅助函数:生成UUIDv4 ------------------------------------------------- */ if (!function_exists(‘wp_generate_uuid4’)) { function wp_generate_uuid4() { return sprintf(‘%04x%04x-%04x-%04x-%04x-%04x%04x%04x’, mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0x0fff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000, mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff) ); } } /* ------------------------------------------------- 会话初始化保障 ------------------------------------------------- */ add_action(‘woocommerce_init’, function() { if (!WC()->session) { WC()->session = new WC_Session_Handler(); WC()->session->init(); } }); Cart.php " method=“post”> <input type="hidden" id="selected_items" name="selected_items" value=""> do_action(‘woocommerce_before_cart’); // Provide tiny context for JS (invisible; no layout change) $pc_cart_is_empty = WC()->cart->is_empty(); function pc_uid_for_view() { if (is_user_logged_in()) return ‘user_’ . get_current_user_id(); if (empty($_COOKIE[‘pc_cart_uid’])) { $token = wp_generate_uuid4(); setcookie(‘pc_cart_uid’, $token, time() + YEAR_IN_SECONDS, COOKIEPATH ?: ‘/’, ‘’, is_ssl(), false); $COOKIE[‘pc_cart_uid’] = $token; } return 'guest’ . sanitize_text_field(wp_unslash($_COOKIE[‘pc_cart_uid’])); } $pc_uid = pc_uid_for_view(); ?> <div class="cart-page-section container" style="max-width: 1200px; margin: 0 auto;"> <form class="woocommerce-cart-form" action="<?php echo esc_url( wc_get_cart_url() ); ?>" method="post"> cart->get_cart() as $cart_item_key => $cart_item ) : ?> exists() && $cart_item['quantity'] > 0 && apply_filters('woocommerce_cart_item_visible', true, $cart_item, $cart_item_key) ) : $product_permalink = apply_filters('woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink($cart_item) : '', $cart_item, $cart_item_key); $line_total_value = (float) ($cart_item['line_total'] + $cart_item['line_tax']); $variation_id = isset($cart_item['variation_id']) ? (int)$cart_item['variation_id'] : 0; $variation_data = isset($cart_item['variation']) ? $cart_item['variation'] : array(); $variation_json = wp_json_encode($variation_data); ?> <table class="shop_table shop_table_responsive cart woocommerce-cart-form__contents"> <thead> <tr> <th class="product-select" style="width: 8%;"> <input type="checkbox" id="select-all-items"> <label for="select-all-items" style="display: inline-block; margin-left: 5px; cursor: pointer;">全选</label> </th> <th class="product-info"></th> <th class="product-price"></th> <th class="product-quantity"></th> <th class="product-subtotal"></th> <th class="product-remove"></th> </tr> </thead> <tbody> <tr class="woocommerce-cart-form__cart-item <?php echo esc_attr( apply_filters('woocommerce_cart_item_class', 'cart_item', $cart_item, $cart_item_key) ); ?>" data-cart_item_key="<?php echo esc_attr($cart_item_key); ?>" data-product_id="<?php echo esc_attr($product_id); ?>" data-variation_id="<?php echo esc_attr($variation_id); ?>" data-variation="<?php echo esc_attr($variation_json); ?>"> <td class="product-select" data-title="<?php esc_attr_e('Select', 'woocommerce'); ?>"> <input type="checkbox" class="item-checkbox" name="selected_items[]" value="<?php echo esc_attr($cart_item_key); ?>" data-price="<?php echo esc_attr($line_total_value); ?>"> </td> <td class="product-info" data-title="<?php esc_attr_e('Product', 'woocommerce'); ?>"> <div class="product-image"> get_image(), $cart_item, $cart_item_key); if ( ! $product_permalink ) : echo $thumbnail; // PHPCS: XSS ok. else : printf('<a href="%s">%s</a>', esc_url($product_permalink), $thumbnail); // PHPCS: XSS ok. endif; ?> </div> <div class="product-name"> get_name(), $cart_item, $cart_item_key) . ' ' ); else : echo wp_kses_post( apply_filters('woocommerce_cart_item_name', sprintf('<a href="%s">%s</a>', esc_url($product_permalink), $_product->get_name()), $cart_item, $cart_item_key) ); endif; do_action('woocommerce_after_cart_item_name', $cart_item, $cart_item_key); echo wc_get_formatted_cart_item_data($cart_item); // PHPCS: XSS ok. ?> </div> </td> <td class="product-price" data-title="<?php esc_attr_e('Price', 'woocommerce'); ?>"> cart->get_product_price($_product), $cart_item, $cart_item_key); // PHPCS: XSS ok. ?> </td> <td class="product-quantity" data-title="<?php esc_attr_e('Quantity', 'woocommerce'); ?>"> is_sold_individually() ) : $product_quantity = sprintf('1 <input type="hidden" name="cart[%s][qty]" value="1">', $cart_item_key); else : $product_quantity = woocommerce_quantity_input( array( 'input_name' => "cart[{$cart_item_key}][qty]", 'input_value' => $cart_item['quantity'], 'max_value' => $_product->get_max_purchase_quantity(), 'min_value' => '0', 'product_name' => $_product->get_name(), ), $_product, false ); endif; echo apply_filters('woocommerce_cart_item_quantity', $product_quantity, $cart_item_key, $cart_item); // PHPCS: XSS ok. ?> <small class="qty-status" style="display:none;margin-left:8px;color:#666;">保存中…</small> </td> <td class="product-subtotal" data-title="<?php esc_attr_e('Subtotal', 'woocommerce'); ?>"> cart->get_product_subtotal($_product, $cart_item['quantity']), $cart_item, $cart_item_key); // PHPCS: XSS ok. ?> </td> <td class="product-remove" data-title="<?php esc_attr_e('操作', 'woocommerce'); ?>"> ×', esc_url( wc_get_cart_remove_url($cart_item_key) ), esc_attr__('Remove this item', 'woocommerce'), esc_attr($product_id), esc_attr($_product->get_sku()) ), $cart_item_key); ?> </td> </tr> </tbody> </table> </form> </div> <div class="cart-footer-actions sticky-footer" style="position: sticky; bottom: 0; background: white; padding: 15px; border-top: 1px solid #ddd; max-width: 1200px; margin: 0 auto;"> <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px;"> <div class="footer-left"> <input type="checkbox" id="footer-select-all"> <label for="footer-select-all" style="display: inline-block; margin-left: 5px; cursor: pointer;">全选</label> <button type="button" class="button" id="remove-selected-items">刪除選中的商品</button> <button type="button" class="button" id="clear-cart">清空購物車</button> </div> <div class="coupon-section"> <input type="text" name="coupon_code" class="input-text" id="coupon_code" value="" placeholder="输入优惠券代码" style="padding: 8px; width: 200px; border: 1px solid #ddd; border-radius: 4px; margin-right: 5px;"> <button type="button" class="button" id="apply-coupon">应用优惠券</button> </div> </div> <div style="display: flex; justify-content: space-between; align-items: center;"> <div class="selected-summary" style="font-size: 16px; font-weight: bold;"> 已选商品: <span id="selected-count">0</span> 件,共计: <span id="selected-total">RM0.00</span> </div> <a href="<?php echo esc_url( wc_get_checkout_url() ); ?>" class="checkout-button button alt wc-forward" id="partial-checkout">结算</a> </div> </div> form-checkout.php session) return; // 从会话获取选中商品 $selected_keys = WC()->session->get(‘pc_selected_items’) ?: []; // 存储原始购物车 if(empty(WC()->session->get(‘original_cart’))) { WC()->session->set(‘original_cart’, WC()->cart->get_cart()); // 创建临时结账购物车 $checkout_cart = []; foreach(WC()->cart->get_cart() as $key => $item) { if(in_array($key, $selected_keys)) { $checkout_cart[$key] = $item; } } WC()->session->set(‘checkout_cart’, $checkout_cart); // 更新实际购物车 WC()->cart->empty_cart(); foreach($checkout_cart as $key => $item) { WC()->cart->add_to_cart( $item[‘product_id’], $item[‘quantity’], $item[‘variation_id’], $item[‘variation’] ); } } }, 5); if ( ! defined( ‘ABSPATH’ ) ) { exit; } //WC 3.5.0 if ( function_exists( ‘WC’ ) && version_compare( WC()->version, ‘3.5.0’, ‘<’ ) ) { wc_print_notices(); } do_action( ‘woocommerce_before_checkout_form’, $checkout ); // If checkout registration is disabled and not logged in, the user cannot checkout. if ( ! $checkout->is_registration_enabled() && $checkout->is_registration_required() && ! is_user_logged_in() ) { echo esc_html( apply_filters( ‘woocommerce_checkout_must_be_logged_in_message’, __( ‘You must be logged in to checkout.’, ‘woocommerce’ ) ) ); return; } // filter hook for include new pages inside the payment method $get_checkout_url = apply_filters( ‘woocommerce_get_checkout_url’, wc_get_checkout_url() ); ?> <form name="checkout" method="post" class="checkout woocommerce-checkout" action="<?php echo esc_url( $get_checkout_url ); ?>" enctype="multipart/form-data" aria-label="<?php echo esc_attr__( 'Checkout', 'woocommerce' ); ?>"> get_checkout_fields() ) : ?> <div class="customer-details" id="customer_details"> </div> <div class="checkout-order-review"> <h3 id="order_review_heading"></h3> <div id="order_review" class="woocommerce-checkout-review-order"> </div> </div> </form> order-create.php session) return; $checkout_cart = WC()->session->get(‘checkout_cart’) ?: []; $original_cart = WC()->session->get(‘original_cart’) ?: []; // 从原始购物车移除已结算商品 foreach($checkout_cart as $key => $item) { if(isset($original_cart[$key])) { unset($original_cart[$key]); } } // 更新实际购物车 WC()->cart->empty_cart(); foreach($original_cart as $key => $item) { WC()->cart->add_to_cart( $item[‘product_id’], $item[‘quantity’], $item[‘variation_id’], $item[‘variation’], // 必须包含变体属性 $item[‘cart_item_data’] ); } // 清理session WC()->session->__unset(‘original_cart’); WC()->session->__unset(‘checkout_cart’); } payment-cancel.php session->get(‘original_cart’)) { WC()->cart->empty_cart(); foreach($original_cart as $key => $item) { WC()->cart->add_to_cart( $item[‘product_id’], $item[‘quantity’], $item[‘variation_id’], $item[‘variation’], $item[‘cart_item_data’] ); } } // 清理session WC()->session->__unset(‘original_cart’); WC()->session->__unset(‘checkout_cart’); // 重定向到购物车 wp_redirect(wc_get_cart_url()); exit; } } these are the full code for these file. If wrong, please give me the full code to be replaced with the current one.
09-03
<?php /** * 鸿宇多用户商城 动态内容函数库 - 已修复兼容 PHP 5.6 * ============================================================================ * 版权所有 2005-2010 鸿宇多用户商城科技有限公司,并保留所有权利。 * 网站地址: http://bbs.hongyuvip.com; * ---------------------------------------------------------------------------- * 仅供学习交流使用,如需商用请购买正版版权。鸿宇不承担任何法律责任。 * 踏踏实实做事,堂堂正正做人。 * ============================================================================ * $Author: liuhui $ * $Id: lib_insert.php 17063 2010-03-25 06:35:46Z liuhui $ */ if (!defined('IN_ECS')) { die('Hacking attempt'); } /** * 获得查询次数以及查询时间 * * @access public * @return string */ function insert_query_info() { if (empty($GLOBALS['db']->queryTime)) { $query_time = 0; } else { if (PHP_VERSION >= '5.0.0') { $start = microtime(true); $query_time = number_format($start - $GLOBALS['db']->queryTime, 6); } else { list($now_usec, $now_sec) = explode(' ', microtime()); list($start_usec, $start_sec) = explode(' ', $GLOBALS['db']->queryTime); $query_time = number_format(($now_sec - $start_sec) + ($now_usec - $start_usec), 6); } } /* 内存占用情况 */ $memory_usage = ''; if ($GLOBALS['_LANG']['memory_info'] && function_exists('memory_get_usage')) { $memory_usage = sprintf($GLOBALS['_LANG']['memory_info'], memory_get_usage() / 1048576); } /* 是否启用了 gzip */ $gzip_enabled = gzip_enabled() ? $GLOBALS['_LANG']['gzip_enabled'] : $GLOBALS['_LANG']['gzip_disabled']; $online_count = intval($GLOBALS['db']->getOne("SELECT COUNT(*) FROM " . $GLOBALS['ecs']->table('sessions'))); /* 加入触发cron代码 */ $cron_method = empty($GLOBALS['_CFG']['cron_method']) ? '<img src="api/cron.php?t=' . gmtime() . '" alt="" style="width:0px;height:0px;" />' : ''; return sprintf($GLOBALS['_LANG']['query_info'], $GLOBALS['db']->queryCount, $query_time, $online_count) . $gzip_enabled . $memory_usage . $cron_method; } /** * 调用浏览历史 * * @access public * @return string */ function insert_history() { $str = ''; if (!empty($_COOKIE['ECS']['history'])) { $goods_ids = array_filter(array_map('intval', explode(',', $_COOKIE['ECS']['history']))); if (empty($goods_ids)) { return ''; } $where = db_create_in($goods_ids, 'goods_id'); $sql = 'SELECT goods_id, goods_name, goods_thumb, shop_price FROM ' . $GLOBALS['ecs']->table('goods') . " WHERE $where AND is_on_sale = 1 AND is_alone_sale = 1 AND is_delete = 0 LIMIT 5"; $res = $GLOBALS['db']->query($sql); while ($row = $GLOBALS['db']->fetch_array($res)) { $goods = array(); $goods['goods_id'] = $row['goods_id']; $goods['goods_name'] = htmlspecialchars($row['goods_name']); $goods['short_name'] = $GLOBALS['_CFG']['goods_name_length'] > 0 ? sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name']; $goods['goods_thumb'] = get_image_path($row['goods_id'], $row['goods_thumb'], true); $goods['shop_price'] = price_format($row['shop_price']); $goods['url'] = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']); $str .= '<li> <div class="p-img"><a target="_blank" href="' . $goods['url'] . '"> <img src="' . $goods['goods_thumb'] . '" alt="' . $goods['goods_name'] . '" width="50" height="50" /> </a></div> <div class="p-name"><a target="_blank" href="' . $goods['url'] . '">' . $goods['goods_name'] . '</a></div> <div class="p-price"><strong>' . $goods['shop_price'] . '</strong></div> </li>'; } } return $str; } /** * 获取“猜你喜欢”商品列表(基于浏览历史) * * @return array */ function get_cainixihuan() { if (empty($_COOKIE['ECS']['history'])) { return array(); } $goods_ids = array_filter(array_map('intval', explode(',', $_COOKIE['ECS']['history']))); if (empty($goods_ids)) { return array(); } $where = db_create_in($goods_ids, 'goods_id'); $sql = 'SELECT cat_id FROM ' . $GLOBALS['ecs']->table('goods') . " WHERE $where AND is_on_sale = 1 AND is_alone_sale = 1 AND is_delete = 0 LIMIT 1"; $cat_id = intval($GLOBALS['db']->getOne($sql)); if (!$cat_id) { $cat_id = 0; } $sql = "SELECT * FROM " . $GLOBALS['ecs']->table('goods') . " WHERE cat_id = '$cat_id' AND is_best = 1 AND is_delete = 0 LIMIT 8"; $list = $GLOBALS['db']->getAll($sql); $arr = array(); foreach ($list as $key => $row) { $arr[$key]['goods_id'] = $row['goods_id']; $arr[$key]['goods_name'] = $row['goods_name']; $arr[$key]['goods_thumb'] = get_image_path($row['goods_id'], $row['goods_thumb'], true); $arr[$key]['shop_price'] = price_format($row['shop_price']); $arr[$key]['url'] = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']); $arr[$key]['evaluation'] = get_evaluation_sumss($row['goods_id']); } return $arr; } /** * 获取商品评论总数 * * @param int $goods_id * @return int */ function get_evaluation_sumss($goods_id) { $goods_id = intval($goods_id); $sql = "SELECT COUNT(*) FROM " . $GLOBALS['ecs']->table('comment') . " WHERE status=1 AND comment_type=0 AND id_value='$goods_id'"; return intval($GLOBALS['db']->getOne($sql)); } /* 代码增加_start By bbs.hongyuvip.com */ /** * 调用浏览历史列表(带比价按钮) * * @access public * @return string */ function insert_history_list() { $str = ''; if (!empty($_COOKIE['ECS']['history'])) { $goods_ids = array_filter(array_map('intval', explode(',', $_COOKIE['ECS']['history']))); if (empty($goods_ids)) { return ''; } $where = db_create_in($goods_ids, 'goods_id'); $sql = 'SELECT goods_id, goods_name, goods_thumb, shop_price, promote_price, market_price, goods_type FROM ' . $GLOBALS['ecs']->table('goods') . " WHERE $where AND is_on_sale = 1 AND is_alone_sale = 1 AND is_delete = 0 LIMIT 10"; $res = $GLOBALS['db']->query($sql); $str .= '<ul>'; while ($row = $GLOBALS['db']->fetch_array($res)) { $goods = array(); $goods['goods_id'] = $row['goods_id']; $goods['goods_name'] = htmlspecialchars($row['goods_name']); $goods['goods_type'] = $row['goods_type']; $goods['short_name'] = $GLOBALS['_CFG']['goods_name_length'] > 0 ? sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name']; $goods['goods_thumb'] = get_image_path($row['goods_id'], $row['goods_thumb'], true); $goods['shop_price'] = price_format($row['shop_price']); $goods['url'] = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']); // 会员价 or 市场价 if ($_SESSION['user_id'] > 0) { $showprice = !empty($row['promote_price']) ? price_format($row['promote_price']) : $goods['shop_price']; } else { $showprice = price_format($row['market_price']); } $str .= '<li><div class="item_wrap"> <div class="dt"><a href="' . $goods['url'] . '"><img width="50" height="50" src="' . $goods['goods_thumb'] . '" /></a></div> <div class="dd"> <a class="name" href="' . $goods['url'] . '">' . $goods['goods_name'] . '</a> <div class="btn" style="padding-top:15px;"> <a class="compare-btn" data-goods="' . $goods['goods_id'] . '" onClick="Compare.add(' . $goods['goods_id'] . ',\'' . addslashes($goods['goods_name']) . '\',' . $goods['goods_type'] . ', \'' . $goods['goods_thumb'] . '\', \'' . $showprice . '\')"></a> <span class="price" style="float:left"><strong>' . $showprice . '</strong></span> </div> </div> </div></li>'; } $str .= '</ul>'; } return $str; } /* 代码增加_end By bbs.hongyuvip.com */ /** * 调用购物车信息 * * @access public * @return string */ function insert_cart_info() { $sess_id = SESS_ID; $sql_where = $_SESSION['user_id'] > 0 ? "c.user_id = '" . intval($_SESSION['user_id']) . "'" : "c.session_id = '$sess_id' AND c.user_id = 0"; $sql = 'SELECT c.*, IF(c.extension_code = "package_buy", pa.act_name, g.goods_name) AS goods_name, ' . 'IF(c.extension_code = "package_buy", "package_img", g.goods_thumb) AS goods_thumb, ' . 'g.goods_id, c.goods_number, c.goods_price ' . 'FROM ' . $GLOBALS['ecs']->table('cart') . ' AS c ' . 'LEFT JOIN ' . $GLOBALS['ecs']->table('goods') . ' AS g ON g.goods_id = c.goods_id ' . 'LEFT JOIN ' . $GLOBALS['ecs']->table('goods_activity') . ' AS pa ON pa.act_id = c.goods_id ' . "WHERE $sql_where AND rec_type = '" . CART_GENERAL_GOODS . "'"; $rows = $GLOBALS['db']->getAll($sql); $arr = array(); foreach ($rows as $k => $v) { $arr[$k]['goods_thumb'] = get_image_path($v['goods_id'], $v['goods_thumb'], true); $arr[$k]['short_name'] = $GLOBALS['_CFG']['goods_name_length'] > 0 ? sub_str($v['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $v['goods_name']; $arr[$k]['url'] = ($v['extension_code'] == 'package_buy') ? '' : build_uri('goods', array('gid' => $v['goods_id']), $v['goods_name']); $arr[$k]['goods_number'] = $v['goods_number']; $arr[$k]['goods_name'] = $v['goods_name']; $arr[$k]['goods_price'] = price_format($v['goods_price']); $arr[$k]['rec_id'] = $v['rec_id']; } $sql = 'SELECT SUM(goods_number) AS number, SUM(goods_price * goods_number) AS amount ' . 'FROM ' . $GLOBALS['ecs']->table('cart') . " AS c " . "WHERE $sql_where AND rec_type = '" . CART_GENERAL_GOODS . "'"; $row = $GLOBALS['db']->getRow($sql); $number = !empty($row['number']) ? intval($row['number']) : 0; $amount = !empty($row['amount']) ? floatval($row['amount']) : 0; $GLOBALS['smarty']->assign('str', sprintf($GLOBALS['_LANG']['cart_info'], $number, price_format($amount, false))); $GLOBALS['smarty']->assign('goods', $arr); $output = $GLOBALS['smarty']->fetch('library/cart_info.lbi'); return $output; } /* 代码增加_start By bbs.hongyuvip.com */ /** * 调用三方客服信息 * * @access public * @return string */ function insert_customer_service() { $sql = 'SELECT * FROM ' . $GLOBALS['ecs']->table('chat_third_customer') . ' ORDER BY cus_type, cus_name'; $rows = $GLOBALS['db']->getAll($sql); $arr = array(); foreach ($rows as $k => $v) { $arr[$k]['cus_id'] = intval($v['cus_id']); $arr[$k]['cus_name'] = htmlspecialchars($v['cus_name']); $arr[$k]['cus_no'] = htmlspecialchars($v['cus_no']); $arr[$k]['cus_type'] = intval($v['cus_type']); $arr[$k]['is_master'] = intval($v['is_master']); } $GLOBALS['smarty']->assign('customer', $arr); $output = $GLOBALS['smarty']->fetch('library/customer_service.lbi'); return $output; } /* 代码增加_end By bbs.hongyuvip.com */ /** * 调用指定广告位的广告 * * @param array $arr 包含 id 和 num * @return string */ function insert_ads($arr) { static $static_res = null; $time = gmtime(); $position_id = intval($arr['id']); $num = isset($arr['num']) ? max(1, min(10, intval($arr['num']))) : 1; if ($num != 1) { $sql = "SELECT a.ad_id, a.position_id, a.media_type, a.ad_link, a.ad_code, a.ad_name, p.ad_width, p.ad_height, p.position_style, RAND() AS rnd " . "FROM " . $GLOBALS['ecs']->table('ad') . " AS a " . "LEFT JOIN " . $GLOBALS['ecs']->table('ad_position') . " AS p ON a.position_id = p.position_id " . "WHERE a.position_id = '$position_id' AND enabled = 1 AND start_time <= '$time' AND end_time >= '$time' " . "ORDER BY rnd LIMIT $num"; $res = $GLOBALS['db']->getAll($sql); } else { if (!isset($static_res[$position_id])) { $sql = "SELECT a.ad_id, a.position_id, a.media_type, a.ad_link, a.ad_code, a.ad_name, p.ad_width, p.ad_height, p.position_style, RAND() AS rnd " . "FROM " . $GLOBALS['ecs']->table('ad') . " AS a " . "LEFT JOIN " . $GLOBALS['ecs']->table('ad_position') . " AS p ON a.position_id = p.position_id " . "WHERE a.position_id = '$position_id' AND enabled = 1 AND start_time <= '$time' AND end_time >= '$time' " . "ORDER BY rnd LIMIT 1"; $static_res[$position_id] = $GLOBALS['db']->getAll($sql); } $res = $static_res[$position_id]; } $ads = array(); $position_style = ''; foreach ($res as $row) { if ($row['position_id'] != $position_id) continue; $position_style = $row['position_style']; switch ($row['media_type']) { case 0: // 图片广告 $src = strpos($row['ad_code'], 'http://') === 0 || strpos($row['ad_code'], 'https://') === 0 ? $row['ad_code'] : DATA_DIR . "/afficheimg/" . $row['ad_code']; $ads[] = "<a href='affiche.php?ad_id={$row['ad_id']}&uri=" . urlencode($row["ad_link"]) . "' target='_blank'>" . "<img src='$src' width='{$row['ad_width']}' height='{$row['ad_height']}' border='0' /></a>"; break; case 1: // Flash $src = strpos($row['ad_code'], 'http://') === 0 || strpos($row['ad_code'], 'https://') === 0 ? $row['ad_code'] : DATA_DIR . "/afficheimg/" . $row['ad_code']; $ads[] = "<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0\" width='{$row['ad_width']}' height='{$row['ad_height']}'> <param name='movie' value='$src'> <param name='quality' value='high'> <embed src='$src' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' width='{$row['ad_width']}' height='{$row['ad_height']}'></embed> </object>"; break; case 2: // CODE $ads[] = $row['ad_code']; break; case 3: // TEXT $ads[] = "<a href='affiche.php?ad_id={$row['ad_id']}&uri=" . urlencode($row["ad_link"]) . "' target='_blank'>" . htmlspecialchars($row['ad_code']) . '</a>'; break; } } if (empty($position_style)) { return ''; } $need_cache = $GLOBALS['smarty']->caching; $GLOBALS['smarty']->caching = false; $GLOBALS['smarty']->assign('ads', $ads); $val = $GLOBALS['smarty']->fetch("str:$position_style"); $GLOBALS['smarty']->caching = $need_cache; return $val; } /** * 调用会员信息 * * @access public * @return string */ function insert_member_info() { $need_cache = $GLOBALS['smarty']->caching; $GLOBALS['smarty']->caching = false; if ($_SESSION['user_id'] > 0) { $GLOBALS['smarty']->assign('user_info', get_user_info()); } else { if (!empty($_COOKIE['ECS']['username'])) { $GLOBALS['smarty']->assign('ecs_username', stripslashes($_COOKIE['ECS']['username'])); } $captcha = intval($GLOBALS['_CFG']['captcha']); if (($captcha & CAPTCHA_LOGIN) && ((!($captcha & CAPTCHA_LOGIN_FAIL)) || (($captcha & CAPTCHA_LOGIN_FAIL) && $_SESSION['login_fail'] > 2)) && gd_version() > 0) { $GLOBALS['smarty']->assign('enabled_captcha', 1); $GLOBALS['smarty']->assign('rand', mt_rand()); } } $output = $GLOBALS['smarty']->fetch('library/member_info.lbi'); $GLOBALS['smarty']->caching = $need_cache; return $output; } // 其他函数省略(结构相同),仅列出关键修复点... ?>是要放入下面这段代码吗 if (!function_exists('insert_ads')) { function insert_ads($arr) { global $db, $ecs; $id = intval($arr['id']); $num = intval($arr['num']); if ($id <= 0) return ''; $sql = "SELECT ad_code, ad_link FROM " . $ecs->table('ad') . " WHERE position_id = '$id' AND enabled = 1 " . " ORDER BY sort_order ASC LIMIT $num"; $res = $db->getAll($sql); $str = ''; foreach ($res as $row) { $src = strpos($row['ad_code'], 'http') === 0 ? $row['ad_code'] : '../data/afficheimg/' . $row['ad_code']; $str .= '<a href="' . htmlspecialchars($row['ad_link']) . '" target="_blank"><img src="' . $src . '" border="0" /></a>'; } return $str; } }
最新发布
11-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值