在WooCommerce单一产品页面上为特定产品添加自定义内容 [英] Add custom content for a specific product on WooCommerce single product pages
问题描述
在Woocommerce中,如何在单个产品页面上为特定产品添加自定义内容?
In Woocommerce, How can I add a custom content for a specific product on single product pages?
这是一个明确的屏幕截图:
Here is an explicit screen shot:
推荐答案
由于您不清楚此自定义内容的屏幕截图,因此有2个选项:
As your screenshot is not so clear on where you want this custom content you have 2 options:
1)在产品价格下
通过将此自定义功能挂在 woocommerce_before_single_product_summary
操作挂钩中,您可以将一些自定义内容添加到特定的产品ID (在功能中定义) 这样:
With this custom function hooked in woocommerce_before_single_product_summary
action hook you can add some custom content to a specific product ID (to be defined in the function) this way:
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 (在功能中定义) 这样:
With this custom function hooked in woocommerce_before_single_product_summary
action hook you can add some custom content to a specific product ID (to be defined in the function) this way:
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语句之后添加到函数中:
If you want to remove the product short description you can add into the function just after the if statement:
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
代码会出现在您活动的子主题(或主题)的functions.php文件中,也可能会出现在任何插件文件中.
经过测试,可以正常工作...
Tested and works…
这篇关于在WooCommerce单一产品页面上为特定产品添加自定义内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!