Woocommerce Produktattribute auf Archivloops-Ausgaben mit OceanWP Theme darstellen

Woocommerce bietet die Möglichkeit eigene Produkteigenschaften (Attribute) zu definieren. Diese werden bei Standard-Darstellungen der Archiveloops nicht automatisch angezeigt. In den Woocommerce Docs ist ein Snippet dazu veröffentlicht. “Display product attribute archive links“:

/**
 * Display product attribute archive links 
 */
add_action( 'woocommerce_shop_loop_item_title', 'wc_show_attribute_links' );
// if you'd like to show it on archive page, replace "woocommerce_product_meta_end" with "woocommerce_shop_loop_item_title"

function wc_show_attribute_links() {
    global $post;
    $attribute_names = array( '<ATTRIBUTE_NAME>', '<ANOTHER_ATTRIBUTE_NAME>' ); // Add attribute names here and remember to add the pa_ prefix to the attribute name
        
    foreach ( $attribute_names as $attribute_name ) {
        $taxonomy = get_taxonomy( $attribute_name );
        
        if ( $taxonomy && ! is_wp_error( $taxonomy ) ) {
            $terms = wp_get_post_terms( $post->ID, $attribute_name );
            $terms_array = array();
        
            if ( ! empty( $terms ) ) {
                foreach ( $terms as $term ) {
                   $archive_link = get_term_link( $term->slug, $attribute_name );
                   $full_line = '<a href="' . $archive_link . '">'. $term->name . '</a>';
                   array_push( $terms_array, $full_line );
                }
                echo $taxonomy->labels->name . ' ' . implode( $terms_array, ', ' );
            }
        }
    }
}

Nur den Namen ohne Archivlink darstellen: wc_show_attribute_links durch wc_show_attribute_name ersetzen.

Beim Einsatz von OceanWP die Positionierung der Darstellung anpassen: woocommerce_shop_loop_item_title durch ocean_after_archive_product_title oder eine andere Position von OceanWP ersetzen.

Kommentare

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert