Skip to content

Instantly share code, notes, and snippets.

@yratof
Created September 20, 2019 09:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yratof/72763000481988485825b00789ab1bdd to your computer and use it in GitHub Desktop.
Save yratof/72763000481988485825b00789ab1bdd to your computer and use it in GitHub Desktop.
Create brand taxonomy in Woocommerce
<?php
/**
* Plugin Name: Brand taxonomy
* Plugin URI: https://drivdigital.no
* Description: Force a brand taxonomy to thrive
* Version: 1.0.0
*/
class brand_taxonomy {
// Nope. brand is still a taxonomy
static function setup() {
add_action( 'init', __CLASS__ . '::product_brand', 15 );
add_shortcode( 'brands', __CLASS__ . '::list_brands_by_letter', 1 );
}
/**
* Product Brand
* hooked on 'init'
*/
static function product_brand() {
$labels = array(
'name' => _x( 'Brands', 'Taxonomy General Name', 'brand-mu-plugin' ),
'singular_name' => _x( 'Brand', 'Taxonomy Singular Name', 'brand-mu-plugin' ),
'menu_name' => __( 'Brand', 'brand-mu-plugin' ),
'all_items' => __( 'All Brands', 'brand-mu-plugin' ),
'parent_item' => __( 'Parent Brand', 'brand-mu-plugin' ),
'parent_item_colon' => __( 'Parent Brand:', 'brand-mu-plugin' ),
'new_item_name' => __( 'New Brand Name', 'brand-mu-plugin' ),
'add_new_item' => __( 'Add New Brand', 'brand-mu-plugin' ),
'edit_item' => __( 'Edit Brand', 'brand-mu-plugin' ),
'update_item' => __( 'Update Brand', 'brand-mu-plugin' ),
'view_item' => __( 'View Brand', 'brand-mu-plugin' ),
'separate_items_with_commas' => __( 'Separate brands with commas', 'brand-mu-plugin' ),
'add_or_remove_items' => __( 'Add or remove brands', 'brand-mu-plugin' ),
'choose_from_most_used' => __( 'Choose from the most used', 'brand-mu-plugin' ),
'popular_items' => __( 'Popular Brands', 'brand-mu-plugin' ),
'search_items' => __( 'Search Brands', 'brand-mu-plugin' ),
'not_found' => __( 'Not Found', 'brand-mu-plugin' ),
);
$rewrite = array(
'slug' => _x( 'brand', 'Taxonomy slug', 'brand-mu-plugin' ),
'with_front' => true,
'hierarchical' => false,
);
$capabilities = array(
'manage_terms' => 'manage_product_terms',
'edit_terms' => 'edit_product_terms',
'delete_terms' => 'delete_product_terms',
'assign_terms' => 'assign_product_terms',
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'query_var' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'rewrite' => $rewrite,
'capabilities' => $capabilities,
);
register_taxonomy( 'brand', array( 'product' ), $args );
}
// Shortcode function to output a list of the brands
static function list_brands_by_letter( $atts ) {
$atts = shortcode_atts(
[ 'title' => __( 'Our Brands', 'brand-mu-plugin' ) ],
$atts, 'brands' );
$args = [ 'hide_empty' => false ]; // Hide brands with no products
$taxonomy = 'brand';
$tags = get_terms( $taxonomy, $args );
$count = wp_count_terms( $taxonomy, $args );
$list = '';
$groups = [];
if ( $tags && is_array( $tags ) ) {
// Group tags by their first letter
foreach ( $tags as $tag ) {
$first_letter = mb_substr( $tag->name, 0, 1, 'utf-8' );
$first_letter = mb_strtoupper( $first_letter, 'UTF-8' );
$groups[ $first_letter ][] = $tag;
}
// If we have groups available...
if ( ! empty( $groups ) ) {
$list .= '<div class="brand__aphabet cf">';
$list .= '<div class="brand__aphabet--heading cf">';
$list .= '<h2 class="brand--title">' . $atts['title'] . '</h2>';
$list .= '<span class="brand--count">' . sprintf( __( 'Showing %d brands', 'brand-mu-plugin' ) , $count ) . '</span>';
$list .= '</div>';
// List each letter and their tags
foreach ( $groups as $letter => $tags ) {
$list .= '<ul class="brand__group">';
$title = apply_filters( 'the_title', $letter );
if ( is_numeric( $title ) ) { $title = '123'; }
$list .= '<li class="brand__group--letter letter__is__' . $title . '">' . $title . '</li>';
foreach ( $tags as $tag ) {
$name = apply_filters( 'the_title', $tag->name );
$link = get_term_link( $tag, 'brand' );
$list .= '<li class="brand__group--item">';
$list .= '<a href="' . $link . '" class="term-name">' . $name . '</a>';
$list .= '</li>';
}
$list .= '</ul>';
}
$list .= '';
$list .= '</div>';
$list .= '<script>jQuery(function($){ $( ".brand__group" ).equalise(); });</script>';
}
} else {
// If there are no brands, we need to output a message saying sorry
$list .= '<div class="brand--empty">' . __( 'No brands are available', 'brand-mu-plugin' ) . '</div>';
}
return $list;
}
}
brand_taxonomy::setup();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment