You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在WooCommerce中用PHP传递购物车商品及附加选项到URL

解决WooCommerce传递YITH商品附加选项到外部站点的问题

核心思路

YITH WooCommerce Product Add-Ons的附加选项数据已自动存储在WooCommerce购物车项中,无需直接查询wp_yith_wapo_addons表。购物车项的yith_wapo_options字段包含用户选择的所有附加选项信息,只需读取该字段并序列化为适合URL传递的格式,追加到跳转URL参数即可。

修改后的完整插件代码

<?php
/**
 * Plugin Name: Custom Payment Gateway
 * Description: 自定义支付网关,将用户重定向到外部配送站点完成支付,支持传递YITH商品附加选项
 * Version: 1.0.2
 * Author: Chris
 * Text Domain: custom-payment-gateway
 * Domain Path: /languages
 */

if (!defined('ABSPATH')) {
    exit;
}

add_filter('woocommerce_payment_gateways', 'add_custom_payment_gateway');
function add_custom_payment_gateway($gateways) {
    $gateways[] = 'WC_Custom_Payment_Gateway';
    return $gateways;
}

add_action('plugins_loaded', 'init_custom_payment_gateway');
function init_custom_payment_gateway() {
    class WC_Custom_Payment_Gateway extends WC_Payment_Gateway {

        public function __construct() {
            $this->id = 'custom_payment_gateway';
            $this->method_title = '自定义支付网关';
            $this->method_description = '用于WooCommerce的自定义支付网关,支持传递商品附加选项到外部站点';
            $this->supports = array('products');

            $this->init_form_fields();
            $this->init_settings();

            $this->title = $this->get_option('title');
            $this->button_label = $this->get_option('button_label');
            $this->button_url = $this->get_option('button_url');

            add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options'));
            add_action('wp_footer', array($this, 'replace_proceed_to_checkout_button'));
        }

        public function init_form_fields() {
            $this->form_fields = array(
                'enabled' => array(
                    'title' => __('启用/禁用', 'woocommerce'),
                    'type' => 'checkbox',
                    'label' => __('启用自定义支付网关', 'woocommerce'),
                    'default' => 'no'
                ),
                'title' => array(
                    'title' => __('标题', 'woocommerce'),
                    'type' => 'text',
                    'description' => __('用户在结账时看到的网关标题', 'woocommerce'),
                    'default' => __('自定义支付', 'woocommerce'),
                    'desc_tip' => true,
                ),
                'button_label' => array(
                    'title' => __('按钮文字', 'woocommerce'),
                    'type' => 'text',
                    'description' => __('结账按钮显示的文字', 'woocommerce'),
                    'default' => __('前往外部支付站点', 'woocommerce'),
                    'desc_tip' => true,
                ),
                'button_url' => array(
                    'title' => __('跳转URL', 'woocommerce'),
                    'type' => 'text',
                    'description' => __('外部支付站点的URL', 'woocommerce'),
                    'default' => '',
                    'desc_tip' => true,
                ),
            );
        }

        public function replace_proceed_to_checkout_button() {
            if (is_cart()) {
                global $woocommerce;

                $checkout_url = esc_url($this->button_url);
                $cart_items = $woocommerce->cart->get_cart();

                $product_ids = array();
                $quantities = array();
                $addons_data = array();

                // 遍历购物车项,收集商品ID、数量和附加选项
                foreach ($cart_items as $cart_item) {
                    $product_ids[] = $cart_item['product_id'];
                    $quantities[] = $cart_item['quantity'];
                    
                    // 收集YITH附加选项数据
                    if (!empty($cart_item['yith_wapo_options'])) {
                        // 整理附加选项为易读格式,保留名称、值和价格
                        $item_addons = array();
                        foreach ($cart_item['yith_wapo_options'] as $addon) {
                            $item_addons[] = array(
                                'name' => $addon['name'],
                                'value' => $addon['value'],
                                'price' => $addon['price']
                            );
                        }
                        $addons_data[] = $item_addons;
                    } else {
                        $addons_data[] = array(); // 无附加选项时传入空数组
                    }
                }

                // 构建URL参数
                $query_params = array(
                    'add-to-cart' => implode(',', $product_ids),
                    'quantity' => implode(',', $quantities),
                );

                // 如果有附加选项,添加到参数中
                if (!empty(array_filter($addons_data))) {
                    $query_params['addons'] = urlencode(wp_json_encode($addons_data));
                }

                $checkout_url = add_query_arg($query_params, $checkout_url);
                ?>
                <script type="text/javascript">
                    jQuery(function($) {
                        var checkout_url = "<?php echo $checkout_url; ?>";
                        $('.wc-proceed-to-checkout a.checkout-button').attr('href', checkout_url).text('<?php echo esc_js($this->button_label); ?>');
                    });
                </script>
                <?php
            }
        }
    }
}

关键修改说明

  1. 附加选项读取:在购物车项循环中,直接读取$cart_item['yith_wapo_options']——这是YITH插件自动存入购物车的用户选择数据,包含附加选项的名称、值和价格。
  2. 数据格式化:将每个商品的附加选项整理为包含name、value、price的数组,确保外部站点能清晰解析。
  3. URL参数处理:用wp_json_encode将附加选项数组转为JSON字符串,再用urlencode处理后作为addons参数加入URL,避免特殊字符导致的解析问题。
  4. 多商品兼容:addons参数是一个JSON数组,数组索引与add-to-cart和quantity的商品顺序一一对应,外部站点可按顺序匹配每个商品的附加选项。

外部站点解析示例(参考)

外部站点可通过以下方式获取并解析附加选项:

// 获取URL中的addons参数
if (isset($_GET['addons'])) {
    $addons_data = json_decode(urldecode($_GET['addons']), true);
    // 遍历附加选项数据,对应每个商品处理
    foreach ($addons_data as $index => $item_addons) {
        // $index对应add-to-cart中商品的索引位置
        // $item_addons为该商品的所有附加选项数组
        foreach ($item_addons as $addon) {
            echo "附加选项名称:{$addon['name']},选择值:{$addon['value']},价格:{$addon['price']}<br>";
        }
    }
}

内容的提问来源于stack exchange,提问作者Amesey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 08:44:51