So und nun noch einmal aufgehübscht als fertige Copy and Paste Lösung für Deine functions.php.
Features:
1. Radiobuttons zur Hersteller Auswahl bei den Produkten im Editor, damit nur ein Hersteller pro Produkt zugewiesen werden kann.
2. Ausgabe des Herstellers in der Metazeile bei der Single Ansicht
3. Shortcode zur vertikalen (z.B. in der Sidebar) oder horizontalen (auf Seitn oder in Beiträgen) Ausgabe der Herstellerliste inkl. Link um alle Produkte dieses Herstellers anzuzeigen. Einbindung als
[manufacturer taxonomie="manufacturer" direction="horizontal"]
<?php
add_action( 'init', 'mmx_register_manufacturer_taxonomie_for_products' );
function mmx_register_manufacturer_taxonomie_for_products() {
$labels = array(
'name' => __( 'Hersteller', 'mmx-woo-addon' ),
'singular_name' => __( 'Hersteller', 'mmx-woo-addon' ),
'add_new' => __( 'Hersteller hinzufügen', 'mmx-woo-addon'),
'add_new_item' => __( 'Neuen Hersteller hinzufügen', 'mmx-woo-addon' ),
'edit_item' => __( 'Hersteller bearbeiten', 'mmx-woo-addon' ),
'new_item' => __( 'Neuer Hersteller', 'mmx-woo-addon' ),
'view_item' => __( 'Betrachte Hersteller', 'mmx-woo-addon' ),
'search_items' => __( 'Suche Hersteller', 'mmx-woo-addon' ),
'not_found' => __( 'Kein Hersteller gefunden', 'mmx-woo-addon' ),
'not_found_in_trash' => __( 'Kein Hersteller im Papierkorb', 'mmx-woo-addon' ),
);
$args = array(
'labels' => $labels,
'singular_label' => __('Hersteller', 'mmx-woo-addon'),
'public' => true,
'show_ui' => true,
'hierarchical' => true,
'show_tagcloud' => false,
'show_in_nav_menus' => true,
'rewrite' => array('slug' => 'manufacturer', 'with_front' => true ),
);
register_taxonomy('manufacturer', 'product', $args);
register_taxonomy_for_object_type( 'manufacturer', 'product' );
}
add_action( 'woocommerce_product_meta_end', 'mmx_render_manufacturer_taxonomie' );
function mmx_render_manufacturer_taxonomie() {
echo '<span class="tagged_as">' . __('Hersteller', 'mmx-woo-addon') . ': ' . get_the_term_list( get_the_ID(), 'manufacturer' ) . '</span>';
}
add_action( 'admin_menu', 'mmx_manufacturer_meta_boxes' );
function mmx_manufacturer_meta_boxes() {
remove_meta_box( 'manufacturerdiv', 'product', 'side' );
add_meta_box( 'manufacturerdiv', __( 'Hersteller', 'mmx-woo-addon' ), 'mmx_render_manufacturer_meta_box', 'product', 'side', 'high', array( 'taxonomie' => 'manufacturer' ));
}
function mmx_render_manufacturer_meta_box( $post, $metabox ) {
$taxonomy = $metabox['args']['taxonomie'];
$manufacturer_ids = wp_get_object_terms( $post->ID, $taxonomy, array('fields' => 'ids') );
$terms = get_terms( $taxonomy, array( hide_empty => 0 ) );
echo '<div id="mmx-' . $taxonomy . '-select">';
echo '<ul class="list:' . $taxonomy . ' categorychecklist form-no-clear">';
foreach ( $terms as $key => $value ) {
$checked = ( $manufacturer_ids[0] == $value->term_id ) ? 'checked="checked"' : '';
echo '<li id="' . $taxonomy . '-' . $value->term_id . '"><label class="selectit">';
echo '<input type="radio" id="in-' . $taxonomy . '-' . $value->term_id . '" name="tax_input['. $taxonomy . '][]" value="' . $value->term_id . '" ' . $checked . '/>' . $value->name;
echo '</label></li>';
}
echo '</ul>';
echo '</div>';
}
add_shortcode( 'manufacturer', 'mmx_render_manufacturer_list' );
function mmx_render_manufacturer_list( $attr ) {
$output = '<style>
ul.mmx_manufacturer_horizontal, ul.mmx_manufacturer_vertical {
list-style-type: none;
list-style-position: initial;
list-style-image: none;
margin-left: 0;
padding-left: 0;
}
ul.mmx_manufacturer_horizontal li {
padding: 0 1em 0 0;
margin-left: 0;
display: inline-block;
}
ul.mmx_manufacturer_vertical li {
padding: 0 1em 0 0;
margin-left: 0;
display: block;
}
</style>';
extract( shortcode_atts( array(
'direction' => 'horizontal',
'taxonomie' => 'category',
), $attr) );
$terms = get_terms( $taxonomie );
$class = ( $direction == 'horizontal' ) ? 'mmx_manufacturer_horizontal' : 'mmx_manufacturer_vertical';
$output .= '<ul class="' . $class . '">';
foreach ( $terms as $term ) {
$term_link = get_term_link( $term );
if ( is_wp_error( $term_link ) ) {
continue;
}
$output .= '<li><a href="' . esc_url( $term_link ) . '">' . $term->name . '</a></li>';
}
$output .= '</ul>';
return $output;
}
Alles anzeigen
Den Code kann man jetzt mit zusätzlichen Funktionen erweitern, das CSS auslagern oder sogar in ein eigenes Plugin packen. Viel Spass!