Come ottenere tutti gli attributi con i loro termini relativi a una specifica categoria di prodotti Woocommerce

30 set 2018, 00:20:00
Visualizzazioni: 32.5K
Voti: 1

Come sappiamo, per ottenere i termini di uno specifico attributo aggiunto a un prodotto possiamo usare:

$attr_terms = $product->get_attribute( 'attr_slug' );

OPPURE per ottenere tutti i termini di uno specifico attributo indipendentemente da un prodotto possiamo usare

$attr_terms = get_terms( 'pa_attr_slug' );

Ma come ottenere tutti gli attributi con i loro termini aggiunti ai prodotti di una specifica categoria di prodotti?

Qualcosa del tipo:

$cat_attrs = ... ($cat->id);
foreach($cat_attrs as $cat_attr) {
    echo $cat_attr->name; // nome dell'attributo
    foreach($cat_attr->terms as $term) {
        echo $term->name; // nome del termine dell'attributo
    }
}
0
Tutte le risposte alla domanda 1
0

Bene, ho trovato la seguente soluzione:

$args = array(
    'category'  => array( 'slug_categoria' )
    // oppure 'term_taxonomy_id' => 4 cioè l'ID della categoria
);

foreach( wc_get_products($args) as $product ){

    foreach( $product->get_attributes() as $attr_name => $attr ){

        echo wc_attribute_label( $attr_name ); // etichetta attributo
        // oppure get_taxonomy( $attr_name )->labels->singular_name;

        foreach( $attr->get_terms() as $term ){

            echo $term->name;
        }
    }
}
2 ott 2018 23:53:00