2016-12-20 5 views
0

Ich versuche, die Attributwertnamen eines Produktattributs als Klassenname für jede <span> Element festlegen.Woocommerce add Attribut Wertnamen als Klasse

So kann ich z. B. material als Produktattribut festlegen. Und dann legen Werte wie stone, paper usw.

Wie kann ich stone und paper als Klassenname? Normalerweise werden Werte wie ->Material: stone, paper gedruckt. Ich möchte einen Klassennamen festlegen, damit ich Stein und Papier in ein kleines Bild umwandeln kann.

So in woocommerce/single-product/add-to-cart/simple.php Das habe ich hinzugefügt:

<?php 
if ($attribute['is_taxonomy']) { 
    $values = wc_get_product_terms($product->id, $attribute['name'], array('fields' => 'names'));   

    ///////////////this is added////////////////////////// 
    if($attribute['name'] == 'pa_material'){ // I only want to set a classname if attribute name == material    

    foreach($values as $value){ 
     $value_classname = $value; // how can I get the value name?? 
    }      
    echo apply_filters('woocommerce_attribute', wpautop(wptexturize('<span class="lbl ' . $value_classname . '">'. implode('</span><span class="lbl ' . $value . '">', $values).'</span>')), $attribute, $values); 
    //////////////////////////////////////////// 
    } else{ 
     echo apply_filters('woocommerce_attribute', wpautop(wptexturize(implode(', ', $values))), $attribute, $values); 

    } 

} else { 

// Convert pipes to commas and display values 
    $values = array_map('trim', explode(WC_DELIMITER, $attribute['value'])); 
echo apply_filters('woocommerce_attribute', wpautop(wptexturize(implode(', ', $values))), $attribute, $values); 

} 
?> 

Momentan befindet sich der Klassenname offensichtlich leer zurück.

Antwort

1

Um die Attributwerte für das Produkt zugewiesen bekommen nur für das Material Attribut:

global $product; 
//this will get the names 
$values = wc_get_product_terms($product->id, 'pa_material', array('fields' => 'names'));