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

WooCommerce购物车:仅显示指定Bookings元数据并替换数量为Persons值

Replace Cart Item Quantity with WooCommerce Bookings "Persons" Metadata from Product Variations

Hey there, let's get this sorted for you. It sounds like you're trying to swap out the default cart item quantity with the "Persons" metadata stored in your WooCommerce Bookings product variations, and you've made some edits but haven't hit the mark yet. Here's a step-by-step solution that should work:

1. First, Confirm the Correct Metadata Key

Before diving into code, double-check the exact meta key for your "Persons" field. WooCommerce Bookings sometimes uses prefixed keys like _wc_booking_persons, but if you've set a custom field named "Persons", the key might just be Persons. To verify, you can use a quick snippet to output all meta data for a variation:

// Add this temporarily to your functions.php, visit a product variation page, then remove it
add_action( 'wp_footer', 'debug_variation_meta' );
function debug_variation_meta() {
    if ( is_product() && isset( $_GET['debug_variation'] ) ) {
        $variation_id = intval( $_GET['debug_variation'] );
        echo '<pre>' . print_r( get_post_custom( $variation_id ), true ) . '</pre>';
    }
}

Then visit your product page with ?debug_variation=YOUR_VARIATION_ID to see all meta keys.

2. Replace Cart Quantity Display

Use this filter to override the quantity shown in the cart and checkout pages. It pulls the "Persons" meta from the variation and replaces the default quantity HTML:

add_filter( 'woocommerce_cart_item_quantity', 'swap_cart_quantity_with_persons', 10, 3 );
function swap_cart_quantity_with_persons( $quantity_html, $cart_item_key, $cart_item ) {
    // Only target variation products
    if ( empty( $cart_item['variation_id'] ) ) return $quantity_html;

    // Get the Persons meta value (replace 'Persons' with your actual meta key if needed)
    $persons_count = get_post_meta( $cart_item['variation_id'], 'Persons', true );

    // If we have a valid Persons value, replace the quantity display
    if ( ! empty( $persons_count ) && is_numeric( $persons_count ) ) {
        $quantity_html = sprintf( '<span class="quantity">%s</span>', esc_html( $persons_count ) );
        // Optional: Add a label like "Persons:" for clarity
        $quantity_html = '<span class="quantity-label">Persons:</span> ' . $quantity_html;
    }

    return $quantity_html;
}

If you want the actual cart item quantity (used for pricing, totals, etc.) to match the "Persons" value, add this action to update the quantity when the cart loads:

add_action( 'woocommerce_before_calculate_totals', 'update_cart_quantity_to_match_persons', 10, 1 );
function update_cart_quantity_to_match_persons( $cart ) {
    // Skip admin unless it's an AJAX request
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
        if ( empty( $cart_item['variation_id'] ) ) continue;

        $persons_count = get_post_meta( $cart_item['variation_id'], 'Persons', true );

        if ( ! empty( $persons_count ) && is_numeric( $persons_count ) ) {
            // Update the cart item quantity to match Persons
            $cart->set_quantity( $cart_item_key, intval( $persons_count ) );
        }
    }
}

Troubleshooting Tips

  • Hook Priority: If your previous edits didn't work, check if you used a lower priority number (higher priority) than default. The 10 in the examples is standard, but you can try 20 if needed.
  • Meta Key Spelling: Make sure the meta key in get_post_meta() exactly matches what you found in the debug step (case-sensitive!).
  • Code Placement: Add these snippets to your child theme's functions.php file or a custom plugin—never edit core WooCommerce files.

内容的提问来源于stack exchange,提问作者Kevin D.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 09:03:55