WooCommerce中通过GET请求添加商品到购物车并传递自定义元数据问题
Hey there! I see you've got reorder buttons working for simple and variable products on your order details page, but that custom "door width" meta field just won't stick. Let's fix that—here's a step-by-step solution tailored to your setup:
Step 1: Update the Reorder Button Link to Include the Meta Field
First, we need to pass the door width value from the order item into your add-to-cart URL. In your order-details-item.php file, modify your button code to grab the existing meta field from the order item and append it as a URL parameter:
// Get product and variation IDs from the order item $product_id = $item->get_product_id(); $variation_id = $item->get_variation_id(); // Pull the saved "door width" meta from the order item (match your actual meta key!) $door_width = $item->get_meta('门宽', true); // Build the add-to-cart URL with all necessary parameters $reorder_url = add_query_arg( array( 'add-to-cart' => $product_id, 'variation_id' => $variation_id, // Leave empty if not a variation 'door_width' => $door_width ), wc_get_cart_url() ); ?> <!-- Your reorder button --> <a href="<?php echo esc_url($reorder_url); ?>" class="button reorder-btn">复购此商品</a>
Note: Double-check that the meta key '门宽' matches exactly what's stored in your order items—case and characters matter!
Step 2: Capture and Save the Meta Field to Cart & Orders
WooCommerce doesn't automatically pick up custom URL parameters for cart items, so we'll use three core hooks to handle this:
Add this code to your theme's functions.php file or a custom plugin:
// 1. Save the door width to the cart item data when added add_filter('woocommerce_add_cart_item_data', 'store_door_width_in_cart', 10, 3); function store_door_width_in_cart($cart_item_data, $product_id, $variation_id) { if (isset($_GET['door_width']) && !empty($_GET['door_width'])) { // Sanitize the input for security $cart_item_data['door_width'] = sanitize_text_field($_GET['door_width']); // Add a unique key to ensure different widths create separate cart items (optional but useful) $cart_item_data['unique_door_width_key'] = md5(microtime() . rand()); } return $cart_item_data; } // 2. Display the door width in the cart and checkout pages add_filter('woocommerce_get_item_data', 'show_door_width_in_cart', 10, 2); function show_door_width_in_cart($item_data, $cart_item) { if (isset($cart_item['door_width'])) { $item_data[] = array( 'key' => '门宽', 'value' => wc_clean($cart_item['door_width']) ); } return $item_data; } // 3. Save the door width to the final order items add_action('woocommerce_add_order_item_meta', 'save_door_width_to_order', 10, 3); function save_door_width_to_order($item_id, $cart_item, $cart_item_key) { if (isset($cart_item['door_width'])) { wc_add_order_item_meta($item_id, '门宽', $cart_item['door_width']); } }
Key Things to Verify
- Meta Key Consistency: Make sure
'门宽'is the exact key used everywhere—when saving to the original order, pulling from the order item, and saving to the new order. - Security: We used
sanitize_text_field()andwc_clean()to prevent malicious input, which is crucial for production sites. - Variation Compatibility: If you're working with variable products, the
variation_idparameter ensures the correct variant is added along with the door width.
Test this out by clicking your reorder button—you should see the door width show up in the cart, and it should persist to the new order once checkout is complete.
内容的提问来源于stack exchange,提问作者Lcapitao

