Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Add a disclaimer on product pages by category
#Read article here
add_filter( 'the_content', 'customizing_woocommerce_description' );
function customizing_woocommerce_description( $content ) {
global $post;
// Only for single product pages (woocommerce)
if ( is_product() ) {
# get the terms for this post
$terms = get_the_terms( $post->ID, 'product_cat' );
$show_pricing_message = false;
$show_pricing_message_parent = false;
#if we have some terms lets loop through them
foreach($terms as $term){
$field = get_field('pricing_disclaimer',$term);
#if the field trigger is found then get the text
if($field == true){
$show_pricing_message = true;
$pricing_message_text= get_field('pricing_disclaimer_text',$term);
#get the term parents and put them into an array
$term_parents = get_term_parents_list($term->term_id, 'product_cat' ,array('separator'=>'|','link'=>false,'format'=>'slug') ) ;
$term_parent_array = explode("|", $term_parents);
#if we found term parents then lets check if the trigger exists
foreach($term_parent_array as $term_parent){
$term_object = get_term_by('slug', $term_parent,'product_cat');
#if the trigger exists lets set the text to overide the parent
$field = get_field('pricing_disclaimer',$term_object );
if($field == true){
$show_pricing_message_parent = true;
$pricing_message_text_parent = get_field('pricing_disclaimer_text',$term_object);
if($show_pricing_message_parent == true || $show_pricing_message == true){
#The custom content
$custom_content ='<div class="custom-content-wrapper">';
#if the child has overide otherwise display parent text
if($pricing_message_text_parent != ''){
$custom_content .= $pricing_message_text_parent;
$custom_content .= $pricing_message_text;
$custom_content .= '</div>';
// Inserting the custom content at the end
$content .= $custom_content;
return $content;

This comment has been minimized.

Copy link
Owner Author

@anthonysbrown anthonysbrown commented Jan 3, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment