/******* Do not edit this file *******
Simple Custom CSS and JS - by Silkypress.com
Saved: Apr 19 2026 | 17:44:37 */
<?php
/**
 * Display product attributes on shop/category pages (under each product card)
 * and on single product pages.
 *
 * Add this code to your child theme's functions.php
 * CSS is separate — add it in: Appearance > Customize > Additional CSS
 */


/* =========================================================
   1. SHOP / CATEGORY PAGE — under each product card
   ========================================================= */

add_action( 'woocommerce_after_shop_loop_item', 'show_product_attributes_on_loop', 15 );

function show_product_attributes_on_loop() {
    global $product;

    if ( ! $product ) return;

    $attributes = $product->get_attributes();

    if ( empty( $attributes ) ) return;

    echo '<div class="custom-loop-attributes">';

    foreach ( $attributes as $attribute ) {

        if ( $attribute->is_taxonomy() ) {
            $label  = wc_attribute_label( $attribute->get_name() );
            $values = array_map( 'esc_html', wc_get_product_terms(
                $product->get_id(),
                $attribute->get_name(),
                array( 'fields' => 'names' )
            ) );
        } else {
            $label  = esc_html( $attribute->get_name() );
            $values = array_map( 'esc_html', $attribute->get_options() );
        }

        if ( empty( $values ) ) continue;

        echo '<div class="custom-loop-attribute-row">';
        echo '<span class="custom-attr-label">' . $label . ': </span>';
        echo '<span class="custom-attr-value">' . implode( ', ', $values ) . '</span>';
        echo '</div>';
    }

    echo '</div>';
}


/* =========================================================
   2. SINGLE PRODUCT PAGE — below the product summary
   ========================================================= */

add_action( 'woocommerce_single_product_summary', 'show_product_attributes_on_single', 25 );

function show_product_attributes_on_single() {
    global $product;

    if ( ! $product ) return;

    $attributes = $product->get_attributes();

    if ( empty( $attributes ) ) return;

    echo '<div class="custom-single-attributes">';
    echo '<ul class="custom-single-attributes-list">';

    foreach ( $attributes as $attribute ) {

        if ( $attribute->is_taxonomy() ) {
            $label  = wc_attribute_label( $attribute->get_name() );
            $values = array_map( 'esc_html', wc_get_product_terms(
                $product->get_id(),
                $attribute->get_name(),
                array( 'fields' => 'names' )
            ) );
        } else {
            $label  = esc_html( $attribute->get_name() );
            $values = array_map( 'esc_html', $attribute->get_options() );
        }

        if ( empty( $values ) ) continue;

        echo '<li class="custom-single-attribute-row">';
        echo '<span class="custom-attr-label">' . $label . ': </span>';
        echo '<span class="custom-attr-value">' . implode( ', ', $values ) . '</span>';
        echo '</li>';
    }

    echo '</ul>';
    echo '</div>';
}