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

Bal*_*aji 4 php wordpress product woocommerce hook-woocommerce

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

这是一个明确的屏幕截图: 如何为单个产品页面添加内容

Loi*_*tec 7

由于您的屏幕截图不太清楚您想要此自定义内容的位置,因此您有 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
}
Run Code Online (Sandbox Code Playgroud)

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
}
Run Code Online (Sandbox Code Playgroud)

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

remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
Run Code Online (Sandbox Code Playgroud)

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

经过测试并有效……