在 WooCommerce 单个产品页面上为特定产品添加自定义内容

Add custom content for a specific product on WooCommerce single product pages

在 Woocommerce 中,如何在单个产品页面上为特定产品添加自定义内容?

这是一个明确的屏幕截图:

只需编辑产品即可添加内容。并且该内容将显示在其详细信息页面上。

由于您的屏幕截图不太清楚您想要此自定义内容的位置,您有 2 个选择:

1)低于商品价格

woocommerce_before_single_product_summary 操作挂钩中使用此自定义函数,您可以将一些自定义内容 添加到特定产品 ID (要在函数中定义)这样:

add_action( 'woocommerce_single_product_summary', 'add_custom_content_for_specific_product', 15 );
function add_custom_content_for_specific_product() {
    global $product;

    // Limit to a specific product ID only (Set your product ID below )
    if( $product->get_id() != 37 ) return;

    // The content start below (with translatables texts)
    ?>
        <div class="custom-content product-id-<?php echo $product->get_id(); ?>">
            <h3><?php _e("My custom content title", "woocommerce"); ?></h3>
            <p><?php _e("This is my custom content text, this is my custom content text, this is my custom content text…", "woocommerce"); ?></p>
        </div>
    <?php
    // End of content
}

2) 商品图片下:

woocommerce_before_single_product_summary 操作挂钩中使用此自定义函数,您可以将一些自定义内容 添加到特定产品 ID (要在函数中定义)这样:

add_action( 'woocommerce_before_single_product_summary', 'add_custom_content_for_specific_product', 25 );
function add_custom_content_for_specific_product() {
    global $product;

    // Limit to a specific product ID only (Set your product ID below )
    if( $product->get_id() != 37 ) return;

    // The content start below (with translatables texts)
    ?>
        <div class="custom-content product-id-<?php echo $product->get_id(); ?>">
            <h3><?php _e("My custom content title", "woocommerce"); ?></h3>
            <p><?php _e("This is my custom content text, this is my custom content text, this is my custom content text…", "woocommerce"); ?></p>
        </div>
    <?php
    // End of content
}

如果您想删除产品简短描述,您可以在 if 语句之后添加到函数中:

remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );

代码进入您的活动子主题(或主题)的 functions.php 文件或任何插件文件。

经过测试并有效……