I'm new to the PHP world, I wanted to ask, how can I add more product ids to this code?
Unfortunately I don't know how to separate multiple IDs.
I would like to enter multiple ids so that specific content is only displayed for certain products.
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 >
<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
}
CodePudding user response:
If you want to the content to show on specific products, you can use in_array():
add_action( 'woocommerce_single_product_summary', 'add_custom_content_for_specific_product', 15 );
function add_custom_content_for_specific_product() {
global $product;
/* Add the product ids to the array separated by commas that you want the content below to display on
* in_array() arguments:
* $product->get_id() is the needle - what you're looking for in the array
* [1,2,3,4,5] are the product ids you want to show the content on. Replace those with your actual ids
* TRUE just makes sure it's the same type of data. In this case we set it to TRUE, since $product->get_id() is an integer and so is the data in the array.
*/
if( in_array( $product->get_id(), [37,1,2,3,6,7], TRUE ) ) :
// The content start below (with translatables texts)
?>
<div >
<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
/* If the ids are not in the array above, return the function without output. */
else :
return;
endif;
}