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

如何通过PHP在WooCommerce结账页面订单回顾表格中添加自定义文本(含指定商品按数量配置)

WooCommerce Checkout Customizations with PHP

Got it, let's get your two WooCommerce checkout customizations sorted with clean PHP code—since you want to ditch the JavaScript approach. Here's how to make this work properly:

1. Add Custom Text Next to Prices in the Checkout Order Review Table

We'll use the woocommerce_cart_item_price filter hook, which targets the price display for every cart item on both cart and checkout pages. This lets you easily append or prepend text right next to the product price without messy DOM manipulation.

// Add custom text next to product prices in the checkout order review table
add_filter( 'woocommerce_cart_item_price', 'append_text_to_checkout_product_price', 10, 3 );
function append_text_to_checkout_product_price( $price_html, $cart_item, $cart_item_key ) {
    // Optional: Uncomment below if you only want this text for specific products (e.g., 7498)
    // if ( $cart_item['product_id'] !== 7498 ) {
    //     return $price_html;
    // }
    
    // Replace with your desired text
    $custom_price_text = ' (Your custom price label here)';
    return $price_html . $custom_price_text;
}

2. Show Custom Text Below the Order Review Table for Product 7498 (Qty 1 or 2)

Your original code used a cart-page hook, but we need to target the checkout order review specifically. The woocommerce_after_checkout_order_review hook inserts content right below the order review table—perfect for your needs. We'll loop through the cart to check for product 7498 and its quantity, then display the matching text.

// Add custom text below checkout order review table for product 7498 (quantity 1 or 2)
add_action( 'woocommerce_after_checkout_order_review', 'display_custom_text_for_7498_after_review' );
function display_custom_text_for_7498_after_review() {
    $target_product_id = 7498;
    $has_target_product = false;
    $product_quantity = 0;

    // Loop through cart items to find our target product and get its quantity
    foreach( WC()->cart->get_cart() as $cart_item ) {
        if ( $cart_item['product_id'] === $target_product_id ) {
            $has_target_product = true;
            $product_quantity = $cart_item['quantity'];
            break; // Stop looping once we find the product—no need to check others
        }
    }

    // Show the right text based on quantity
    if ( $has_target_product ) {
        $text_container = '<div class="custom-checkout-notice" style="margin-top:15px; padding:10px; background:#f8f9fa; border-radius:4px;">%s</div>';
        
        if ( $product_quantity === 1 ) {
            printf( $text_container, 'Your custom message for product 7498 when quantity is 1.' );
        } elseif ( $product_quantity === 2 ) {
            printf( $text_container, 'Your custom message for product 7498 when quantity is 2.' );
        }
    }
}

Quick Tips:

  • Hook Choice: woocommerce_after_checkout_order_review ensures your text only shows on the checkout page, exactly where you want it (below the order review table).
  • Efficiency: We break the loop as soon as we find product 7498 to avoid unnecessary work.
  • Styling: The custom-checkout-notice class (and inline styles) let you match the text to your store's design. Tweak the CSS as needed.
  • Conditional Price Text: In the first snippet, uncomment the product ID check if you only want the price text to appear for specific items (like 7498).

Just drop both snippets into your theme's functions.php file or a custom plugin, then replace the placeholder text with your actual content.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.27 10:42:35