Alle Produktattribute und ihre Bedingungen in WooCommerce abrufen

Lesezeit: 1 Minute

Benutzeravatar von Stanley Ngumo
Stanley Ngumo

Ich habe den ganzen Tag darüber recherchiert, kann aber keine eindeutige Antwort finden. Wie kann ich die festgelegten Produktattribute zusammen mit den jeweils konfigurierten Bedingungen erhalten?

Das ist es, was ich jetzt habe

//get the  terms
$attribute_taxonomies = wc_get_attribute_taxonomies();
$taxonomy_terms = array();

if ( $attribute_taxonomies ) {
    foreach ($attribute_taxonomies as $tax) {
        
        //dont know what to add here
    }
}

var_dump($taxonomy_terms);

Hier unten erhalten Sie eine Liste aller Produktattribute und ihrer jeweiligen Begriffsnamen:

echo '<ul>';
// Loop through WooCommerce registered product attributes
foreach( wc_get_attribute_taxonomies() as $values ) {
    // Get the array of term names for each product attribute
    $term_names = get_terms( array('taxonomy' => 'pa_' . $values->attribute_name, 'fields' => 'names' ) );
    echo '<li><strong>' . $values->attribute_label . '</strong>: ' . implode(', ', $term_names);
}
echo '</ul>';

Wenn Sie lieber ein Array der WP_terms-Objekte erhalten möchten, verwenden Sie:

// Get the array of the WP_Terms Object for the each product attribute
$terms = get_terms( array('taxonomy' => 'pa_' . $values->attribute_name );

Das ermöglicht die Verwendung einer foreach-Schleife, um aus jedem Begriff das zu bekommen, was Sie wollen …

Überprüfen Sie zunächst die Funktion wc_get_attribute_taxonomies() mit var_dump().

1451200cookie-checkAlle Produktattribute und ihre Bedingungen in WooCommerce abrufen

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy