In Woocommerce, How can I add a custom content for a specific product on single product pages?
Here is an explicit screen shot:
Call the WordPress add_action function, select the correct WooCommerce Single Product Page hook (a.k.a. the correct position in the page), and trigger your custom function. You can for example add banners, text, variable values, iframes, and so on without even touching the WooCommerce core files.
If you wish to have a different button setting of the products in the shop, you can do it in the editing page or by using “Custom Button Url List” section. Click on “Add products” button you find on top of the page to add new configurations to the button.
As your screenshot is not so clear on where you want this custom content you have 2 options:
1) Under the product price
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) Under the product image:
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 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 );
Code goes in functions.php file of your active child theme (or theme) or also in any plugin file.
Tested and works…
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With