Ich versuche, die benutzerdefinierten Attributwerte der Produkte zu erhalten, versage es jedoch kläglich.
Ich habe versucht zu tun:
global $woocommerce, $post, $product;
$res = get_post_meta($product->id);
print_r(unserialize($res['_product_attributes'][0]));
Und ich bekomme diese Rohdaten:
[pa_koostis] => Array
(
[name] => pa_koostis
[value] =>
[position] => 0
[is_visible] => 1
[is_variation] => 0
[is_taxonomy] => 1
)
Ich weiß, dass es einen Wert gibt, weil er im Attributabschnitt angezeigt wird, aber ich kann einfach keinen Weg finden, ihn in meinem benutzerdefinierten Code anzuzeigen.
Bearbeitet: Der
woocommerce_get_product_terms
ist veraltet seit Woocommerce Version 3
Gehen Sie wie folgt vor als @datafeedr wrote in seiner Antwort :
global $product;
$koostis = array_shift( wc_get_product_terms( $product->id, 'pa_koostis', array( 'fields' => 'names' ) ) );
oder noch kompakter:
global $product;
$koostis = $product->get_attribute( 'pa_koostis' );
Ursprüngliche Antwort:
$result = array_shift(woocommerce_get_product_terms($product->id, 'pa_koostis', 'names'));
Sie können den einzelnen Wert für das Attribut mit dem folgenden Code erhalten:
$pa_koostis_value = get_post_meta($product->id, 'pa_koostis', true);
woocommerce_get_product_terms()
ist jetzt veraltet.
Verwenden Sie stattdessen wc_get_product_terms()
.
Beispiel:
global $product;
$koostis = array_shift( wc_get_product_terms( $product->id, 'pa_koostis', array( 'fields' => 'names' ) ) );
Update für 2018. Sie können Folgendes verwenden:
global $product;
echo wc_display_product_attributes( $product );
Um die Ausgabe anzupassen, kopieren Sie plugins/woocommerce/templates/single-product/product-attributes.php
in themes/theme-child/woocommerce/single-product/product-attributes.php
und ändern Sie diese.
Am häufigsten aktualisiert:
$product->get_attribute( 'your_attr' );
Sie müssen $product
definieren, wenn er nicht auf der Seite ist.
Versuchen Sie Folgendes, um ein Array von Attributnamen => Attributwert (e) zu erhalten:
global $product;
$formatted_attributes = array();
$attributes = $product->get_attributes();
foreach($attributes as $attr=>$attr_deets){
$attribute_label = wc_attribute_label($attr);
if ( isset( $attributes[ $attr ] ) || isset( $attributes[ 'pa_' . $attr ] ) ) {
$attribute = isset( $attributes[ $attr ] ) ? $attributes[ $attr ] : $attributes[ 'pa_' . $attr ];
if ( $attribute['is_taxonomy'] ) {
$formatted_attributes[$attribute_label] = implode( ', ', wc_get_product_terms( $product->id, $attribute['name'], array( 'fields' => 'names' ) ) );
} else {
$formatted_attributes[$attribute_label] = $attribute['value'];
}
}
}
//print_r($formatted_attributes);
return $formatted_attributes;
Es ist wenig ineffizient, macht aber den Trick.
Die Antwort auf "Jede Idee, alle Attribute auf einmal zu bekommen?" Frage ist nur Funktion mit nur Produkt-ID aufzurufen:
$array=get_post_meta($product->id);
schlüssel ist optional, siehe http://codex.wordpress.org/Function_Reference/get_post_meta
Sie erhalten Attribute als Array in "$ formatted_attributes".
$attributes = $product->get_attributes();
foreach($attributes as $attr=>$attr_deets){
$attribute_label = wc_attribute_label($attr);
if ( isset( $attributes[ $attr ] ) || isset( $attributes[ 'pa_' . $attr ] ) ) {
$attribute = isset( $attributes[ $attr ] ) ? $attributes[ $attr ] : $attributes[ 'pa_' . $attr ];
if ( $attribute['is_taxonomy'] ) {
$formatted_attributes[$attribute_label] = wc_get_product_terms( $product->id, $attribute['name']);
} else {
$formatted_attributes[$attribute_label] = $attribute['value'];
}
}
}
print_r($formatted_attributes);
Verwenden Sie den folgenden Code, um alle Attribute mit Details abzurufen
global $wpdb;
$attribute_taxonomies = $wpdb->get_results( "SELECT * FROM " . $wpdb->prefix . "woocommerce_attribute_taxonomies WHERE attribute_name != '' ORDER BY attribute_name ASC;" );
set_transient( 'wc_attribute_taxonomies', $attribute_taxonomies );
$attribute_taxonomies = array_filter( $attribute_taxonomies ) ;
prin_r($attribute_taxonomies);