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

求助:ACF自定义字段无法在WooCommerce商品详情页指定位置显示

Fixing ACF Pricing Table Display Above WooCommerce Add-to-Cart Button

It looks like your code has a couple of critical issues that are stopping the ACF field from showing up correctly. Let’s walk through the fixes step by step:

1. Fix the Incomplete Syntax Error

Your current code cuts off mid-echo statement (echo '<p>&), which is causing a PHP syntax error. This alone would prevent the entire function from running, so that’s the first thing to address.

2. Properly Retrieve and Display the ACF Field

You haven’t actually pulled the value of your ACF pricing table field yet. You need to use ACF’s built-in functions to fetch the content associated with your field.

Corrected Code Snippet

Here’s a revised, working version of your code:

add_action( 'woocommerce_before_add_to_cart_button', 'display_acf_pricing_table_above_add_to_cart', 10 );

function display_acf_pricing_table_above_add_to_cart() {
    // Replace 'pricing_table' with your actual ACF field name (case-sensitive!)
    $pricing_table_content = get_field( 'pricing_table' );

    // Only display the section if the field has content
    if ( $pricing_table_content ) {
        echo '<div id="price-table" style="margin-bottom: 20px;">';
        echo '<strong>Price Table:</strong>';
        echo $pricing_table_content; // Outputs the ACF field's content
        echo '</div>';
    }
}

Key Details to Note:

  • Field Name Match: Make sure 'pricing_table' exactly matches the field name you set up in your ACF field group.
  • Priority Adjustment: Using priority 10 ensures the table loads before the add-to-cart button (the button’s default priority is 20).
  • Conditional Check: The if ( $pricing_table_content ) line prevents empty divs from appearing if the field is blank for a specific product.
  • Spacing: The inline style adds space between the table and button—you can move this to your theme’s CSS for cleaner code.

Extra Troubleshooting Tips

If the table still doesn’t show up:

  • Confirm your ACF field group is assigned to the Product post type in ACF’s settings.
  • Check your site’s PHP error log for hidden issues (enable debugging temporarily by adding define('WP_DEBUG', true); to your wp-config.php file).
  • Test with a simple text ACF field first to rule out issues with the pricing table field type itself.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 10:17:26