如何在WooCommerce中通过GET请求传递自定义元数据至购物车商品
嘿,这事儿我熟!要通过URL的GET参数把自定义元数据(比如你提到的custom_price和custom_reference_meta)带到WooCommerce的购物车商品里,分几步就能搞定,我给你一步步拆解:
1. 捕获URL中的自定义参数
首先得在商品添加到购物车前,把URL里的自定义参数抓下来暂存到WooCommerce会话中,方便后续调用。用woocommerce_add_to_cart_validation钩子就能实现:
add_filter( 'woocommerce_add_to_cart_validation', 'capture_custom_cart_meta', 10, 3 ); function capture_custom_cart_meta( $passed, $product_id, $quantity ) { // 检查URL是否包含目标自定义参数 if ( isset( $_GET['custom_price'] ) && isset( $_GET['custom_reference_meta'] ) ) { // 将参数存入WC会话,后续添加购物车时取用 WC()->session->set( 'custom_cart_meta', array( 'custom_price' => sanitize_text_field( $_GET['custom_price'] ), 'custom_reference_meta' => sanitize_text_field( $_GET['custom_reference_meta'] ) ) ); } return $passed; }
2. 绑定自定义元数据到购物车项
接下来要把会话里的自定义数据绑定到具体的购物车项上,确保每个带自定义元的商品是独立项,不会和普通商品合并。用woocommerce_add_cart_item_data钩子:
add_filter( 'woocommerce_add_cart_item_data', 'add_custom_meta_to_cart_item', 10, 3 ); function add_custom_meta_to_cart_item( $cart_item_data, $product_id, $variation_id ) { // 从会话中取出暂存的自定义元数据 $custom_meta = WC()->session->get( 'custom_cart_meta' ); if ( $custom_meta ) { // 添加唯一标识键,保证带自定义元的商品独立存在 $cart_item_data['custom_meta_unique_key'] = md5( microtime() . rand() ); // 将自定义数据写入购物车项 $cart_item_data['custom_price'] = $custom_meta['custom_price']; $cart_item_data['custom_reference_meta'] = $custom_meta['custom_reference_meta']; // 用完清空会话数据,避免影响后续添加的商品 WC()->session->__unset( 'custom_cart_meta' ); } return $cart_item_data; }
3. 在购物车/结账页面展示自定义元数据
为了让用户能看到这些自定义信息,需要在购物车和结账页面把它们显示出来,用woocommerce_get_item_data钩子:
add_filter( 'woocommerce_get_item_data', 'display_custom_meta_in_cart', 10, 2 ); function display_custom_meta_in_cart( $item_data, $cart_item ) { // 展示自定义价格 if ( isset( $cart_item['custom_price'] ) ) { $item_data[] = array( 'key' => '自定义价格', 'value' => wc_price( $cart_item['custom_price'] ) ); } // 展示自定义参考编号 if ( isset( $cart_item['custom_reference_meta'] ) ) { $item_data[] = array( 'key' => '自定义参考编号', 'value' => $cart_item['custom_reference_meta'] ); } return $item_data; }
4. (可选)让自定义价格生效
如果你需要让custom_price覆盖商品原价,用woocommerce_before_calculate_totals钩子实现:
add_action( 'woocommerce_before_calculate_totals', 'apply_custom_price_to_cart_item', 10, 1 ); function apply_custom_price_to_cart_item( $cart ) { // 后台环境跳过,避免干扰管理员操作 if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; foreach ( $cart->get_cart() as $cart_item ) { if ( isset( $cart_item['custom_price'] ) ) { // 设置商品的自定义价格 $cart_item['data']->set_price( $cart_item['custom_price'] ); } } }
5. 保存自定义元数据到订单
最后要把这些自定义元数据存到订单和订单项里,方便后续查看订单时能找到这些信息,用woocommerce_checkout_create_order_line_item钩子:
add_action( 'woocommerce_checkout_create_order_line_item', 'save_custom_meta_to_order_item', 10, 4 ); function save_custom_meta_to_order_item( $item, $cart_item_key, $cart_item, $order ) { if ( isset( $cart_item['custom_price'] ) ) { $item->add_meta_data( '自定义价格', wc_price( $cart_item['custom_price'] ), true ); } if ( isset( $cart_item['custom_reference_meta'] ) ) { $item->add_meta_data( '自定义参考编号', $cart_item['custom_reference_meta'], true ); } }
使用说明
把上面的代码复制到你的WordPress主题functions.php文件中,或者创建一个自定义插件来存放(推荐用插件,避免主题更新丢失代码)。
测试验证
用你给出的示例URL访问:http://yourdomain.com/?add-to-cart=25&custom_price=99.99&custom_reference_meta=REF0019,商品会自动添加到购物车,你能在购物车页面看到自定义价格和参考编号,结账后这些信息也会同步到订单里。
内容的提问来源于stack exchange,提问作者Kai

