Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add a disclaimer on product pages by category
<?php
#Read article here https://worcesterwideweb.com/woocommerce-add-single-product-notice-on-a-per-category-basis-with-hierarchy/
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($terms){
#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') ) ;
if($term_parents){
$term_parent_array = explode("|", $term_parents);
#if we found term parents then lets check if the trigger exists
if(count($term_parent_array)>0){
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;
}else{
$custom_content .= $pricing_message_text;
}
$custom_content .= '</div>';
// Inserting the custom content at the end
$content .= $custom_content;
}
}
return $content;
}
@anthonysbrown

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