Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Garconis/15056f53a4788ef38f1f90f0542db219 to your computer and use it in GitHub Desktop.
Save Garconis/15056f53a4788ef38f1f90f0542db219 to your computer and use it in GitHub Desktop.
WooCommerce | Shortcode to output text of a custom ACF options field, based on if a particular product attribute has a certain term
<?php
// Check if product is set to New = Yes and/or Featured = Yes
// then, display the Featured or New product attribute terms if = Yes
add_shortcode( 'fs-product-featured-new', 'fs_product_featured_new_shortcode' );
function fs_product_featured_new_shortcode( $atts ) {
// begin output buffering
ob_start();
// grab the terms of the product's attributes
$new_terms = get_the_terms($post->ID, 'pa_new');
$featured_terms = get_the_terms($post->ID, 'pa_featured');
$new_term_label = get_field('plant_new_label', 'option');
$featured_term_label = get_field('plant_featured_label', 'option');
//echo $new_term_label;
if ( ! empty( $new_terms ) && ! is_wp_error( $new_terms ) ){
foreach ( $new_terms as $new_term ) {
if ($new_term->slug == 'yes') {
echo '<div class="fs-new-'. $new_term->slug .'" title="'. $new_term_label .'">'. $new_term_label .'</div>';
}
}
}
if ( ! empty( $featured_terms ) && ! is_wp_error( $featured_terms ) ){
foreach ( $featured_terms as $featured_term ) {
if ($featured_term->slug == 'yes') {
echo '<div class="fs-featured-'. $featured_term->slug .'" title="'. $featured_term_label .'">'. $featured_term_label .'</div>';
}
}
}
// end output buffering, grab the buffer contents, and empty the buffer
return ob_get_clean();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment