skip to Main Content

I’ve been trying for about three days now to get 4 of my woocommerce product attribute slugs instead of names to display underneath the products.

So far I’ve been using this code that does seem to do exactly what I want except for taking the attribute name instead of the value.

    /**
 * Display available attributes.
 * 
 * @return array|void
 */
function iconic_available_attributes() {
    global $product;

    if ( ! $product->is_type( 'variable' ) ) {
        return;
    }

    $attributes = iconic_get_available_attributes( $product );

    if ( empty( $attributes ) ) {
        return;
    }

    foreach ( $attributes as $attribute ) {
        ?>
        <div class="iconic-available-attributes">
            <p class="iconic-available-attributes__title"><?php _e( 'Available', 'iconic' ); ?> <strong><?php echo $attribute['name']; ?></strong></p>

            <ul class="iconic-available-attributes__values">
                <?php foreach ( $attribute['values'] as $value ) { ?>
                    <li class="iconic-available-attributes__value <?php echo $value['available'] ? '' : 'iconic-available-attributes__value--unavailable'; ?>"><?php echo $value['name']; ?></li>
                <?php } ?>
            </ul>
        </div>
        <?php
    }
}


/**
 * Get available attributes.
 *
 * @param WC_Product_Variable $product
 *
 * @return array
 */
/**

 * @snippet       Display Custom Products Attributes on the Products Page

*/

function cw_woo_attribute(){

    global $product;

    $attributes = $product->get_attributes();

    if ( ! $attributes ) {

        return;

    }

    $display_result = '';

    foreach ( $attributes as $attribute ) {

        if ( $attribute->get_variation() ) {

            continue;

        }

        $name = $attribute->get_name();

        if ( $attribute->is_taxonomy() ) {

            $terms = wp_get_post_terms( $product->get_id(), $name, 'all' );

            $cwtax = $terms[0]->taxonomy;

            $cw_object_taxonomy = get_taxonomy($cwtax);

            if ( isset ($cw_object_taxonomy->labels->singular_name) ) {

                $tax_label = $cw_object_taxonomy->labels->singular_name;

            } elseif ( isset( $cw_object_taxonomy->label ) ) {

                $tax_label = $cw_object_taxonomy->label;

                if ( 0 === strpos( $tax_label, 'Product ' ) ) {

                    $tax_label = substr( $tax_label, 8 );

                }

            }
            $display_result .="<span class='attribute'>" .  $tax_label . "</span>";

            $tax_terms = array();

            foreach ( $terms as $term ) {

                $single_term = esc_html( $term->name );

                array_push( $tax_terms);

            }
            $display_result .= implode(', ', $tax_terms);

        } else {

            $display_result .= $name;

            $display_result .= esc_html( implode( ', ', $attribute->get_options() ) );

        }
    }

    echo "<span class='attributeline'>" .  "| " . "</span>" .  $display_result;

}
add_action('woocommerce_shop_loop_item_title', 'cw_woo_attribute', 25);

I’m not a PHP coder in any way so I’ve been struggling to get it to work.

Here is a sample of the current situation showing the name: "plant type" instead of the value: "annual".

enter image description here

Looking forward to your replies so I can move on with the rest of the shop!

2

Answers


  1. Add the following code snippet to functions.php to display a coma separated string of term names under product on shop archive.

    add_filter( 'woocommerce_after_shop_loop_item_title', 'loop_display_attr', 15 );
    function loop_display_attr() {
    global $product;
    // The attribute slug
    $attribute = 'attr';
    // Get attribute term names in a coma separated string
    $term_names = $product->get_attribute( $attribute );
    
    // Display a coma separted string of term names
    echo '<p>' . $term_names . '</p>';
    }
    
    Login or Signup to reply.
  2. Can you check this existing answer?

    WordPress Woocommerce Show attributes on shop page

    I just tested it on a recent Woocommerce install, and the accepted answer seems to still work fine.

    See if you can get it to work by predefining which attributes you want to show like they do in that question: pa_country, pa_class etc. You can see it in the following part.

    // Define you product attribute taxonomies in the array
    $product_attribute_taxonomies = array( 'pa_country', 'pa_class','pa_faction', 'pa_gender' );
    

    If you don’t want to predefine the attributes, you can still get them like below (also tested). But for testing, it might be helpful to just use the predefined strings so you are sure it’s working.

    $attributes = $product->get_attributes();
    $attrs = [];
    foreach($attributes as $key => $attribute) :
      $attrs[] = $key;
    endforeach;
    // just replace the array with attribute names with $attrs
    $product_attribute_taxonomies = $attrs;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search