Skip to content

Instantly share code, notes, and snippets.

@helgatheviking
Last active January 18, 2022 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save helgatheviking/1d34f84fc93b8a08c258a37768802625 to your computer and use it in GitHub Desktop.
Save helgatheviking/1d34f84fc93b8a08c258a37768802625 to your computer and use it in GitHub Desktop.
Adds a [display_attributes] shortcode that will display all the terms for each attribute. Use [display_attributes attributes="some_taxonomy"] to show a particular attribute's terms.
<?php
/**
* Plugin Name: Attributes shortcode
* Plugin URI: https://gist.github.com/helgatheviking/1d34f84fc93b8a08c258a37768802625
* Description: Create a shortcode to display product attribute taxonomies
* Version: 6.8.0
* Author: Kathy Darling
* Author URI: https://kathyisawesome.com/
*
* Requires PHP: 7.4
*
* Requires at least: 5.8
* Tested up to: 5.8
*
* WC requires at least: 5.8
* WC tested up to: 6.0
*
* Copyright: © 2022 Kathy is Awesome
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
/**
* Attributes shortcode callback.
*
* Attributes {
* @param 'attributes' => string - specific attributes to display, separated by | character. Defaults to all attributes.
* @param 'show_list' => string "true" or "false" - Display as unordered list <ul>. Defaults to true.
* @param 'show_label' => string "true" or "false" - Prepend with taxonomy label. Defaults to true.
* @param 'show_links' => string "true" or "false" - Wrap terms with link to term archive. Defaults to true.
* }
*
* Example: [display_attributes] to show all atttributes as a list with links, and labels.
* Example: [display_attributes attributes="color" show_links="false"] to display single attribute without links.
*/
function kia_attributes_shortcode( $atts ) {
global $product;
if( ! is_object( $product ) || ! $product->has_attributes() ){
return;
}
// parse the shortcode attributes
$args = shortcode_atts( array(
'attributes' => array_keys( $product->get_attributes() ), // by default show all attributes
'show_list' => 'true',
'show_label' => 'true',
'show_links' => 'true',
), $atts );
// If receive an attributes param, turn into array.
if( is_string( $args[ 'attributes' ] ) ){
$args[ 'attributes' ] = array_map( 'trim', explode( '|' , $args[ 'attributes' ] ) );
}
// Start with a null string because shortcodes need to return not echo a value.
$html = '';
if( ! empty( $args[ 'attributes' ] ) ) {
foreach ( $args[ 'attributes' ] as $attribute ) {
$terms_list = '';
// Get the WC-standard attribute taxonomy name.
$taxonomy = strpos( $attribute, 'pa_' ) === false ? wc_attribute_taxonomy_name( $attribute ) : $attribute;
if( taxonomy_is_product_attribute( $taxonomy ) ) {
// Get the attribute label.
$attribute_label = wc_attribute_label( $taxonomy );
if ( 'true' === $args[ 'show_links' ] ) {
$before = $args[ 'show_label' ] ? $attribute_label . ': ' : '';
$after = '';
if ( 'true' === $args[ 'show_list' ] ) {
$before = '<li>' . $before;
$after = $after . '</li>';
}
$html .= get_the_term_list( $product->get_id(), $taxonomy, '<li class="bullet-arrow">' . $attribute_label . ': ' , ', ', '</li>' );
} else {
// Get the terms.
$terms = get_the_terms( $product->get_id(), $taxonomy );
if ( $terms ) {
$last_key = array_key_last( $terms );
// Build the terms list.
foreach ( $terms as $i => $term ) {
if( is_wp_error( $term ) ) {
continue;
}
$terms_list .= $term->name;
$terms_list .= $last_key === $i ? '' : ', ';
}
// Prepend tax label.
if ( 'true' === $args[ 'show_label' ] ) {
$terms_list = $attribute_label . ': ' . $terms_list; // Currently only LTR languages.
}
// Wrap in <li> markup.
if ( 'true' === $args[ 'show_list' ] ) {
$terms_list = '<li>' . $terms_list . '</li>';
}
// Build the html string.
$html .= $terms_list;
}
}
}
}
// If we have anything to display, wrap it in a <ul> for proper markup
if( $html && 'true' === $args[ 'show_list' ] ) {
$html = '<ul class="product-attributes">' . $html . '</ul>';
}
}
return $html;
}
add_shortcode( 'display_attributes', 'kia_attributes_shortcode' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment