Ich versuche, einen einzelnen Attributwert ('Größe') auf der Shop-Seite anzuzeigen. Ich habe den folgenden Code verwendet, um alle Werte anzuzeigen. Ich habe versucht, mich anzupassen, um ein einzelnes Attribut anzuzeigen, aber ohne Erfolg ...
Können Sie mir bitte helfen, den Code so anzupassen, dass nur Werte des Attributs 'Größe' angezeigt werden?
// Get the attributes
$attributes = $product->get_attributes();
// Start the loop
foreach ( $attributes as $attribute ) :
// Check and output, adopted from /templates/single-product/product-attributes.php
if ( $attribute['is_taxonomy'] ) {
$values = wc_get_product_terms( $product->id, $attribute['name'], array( 'fields' => 'names' ) );
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 );
}
endforeach;
Kannst du mir bitte mitteilen, wie ich es ändern soll?
Verwenden Sie einfach global $product
und dann die Methode get_attribute()
für dieses Produktobjekt, wie unten dargestellt.
$size = $product->get_attribute( 'pa_size' );
Und Sie können das auch erhalten, indem Sie unter Code-
global $product;
$size = array_shift( wc_get_product_terms( $product->id, 'pa_size', array( 'fields' => 'names' ) ) );
Denken Sie daran, dass Sie den global $product
verwenden müssen.