[英]Add custom content for a specific product on WooCommerce single product pages
As your screenshot is not so clear on where you want this custom content you have 2 options:由于您的屏幕截图不清楚您想要此自定义内容的位置,因此您有 2 个选项:
1) Under the product price 1)根据产品价格
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:使用
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) Under the product image: 2) 在产品图片下:
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:使用
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 you want to remove the product short description you can add into the function just after the if statement:如果要删除产品简短描述,可以在 if 语句之后添加到函数中:
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
Code goes in functions.php file of your active child theme (or theme) or also in any plugin file.代码位于活动子主题(或主题)的 functions.php 文件中,也位于任何插件文件中。
Tested and works…经过测试和工作......
Just Edit the product to add the content.只需编辑产品即可添加内容。 and that content will show on its detail page.
并且该内容将显示在其详细信息页面上。
The code upstairs working perfect.楼上的代码完美运行。 I want to ask, how is possible to moderate the function and to give permission to appearing a custom co
我想问一下,如何调节功能并允许出现自定义 co
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.