Come ottenere il Link della Categoria Prodotto WooCommerce tramite ID?

3 ott 2014, 21:20:12
Visualizzazioni: 101K
Voti: 7

Le categorie prodotto di WooCommerce sono una tassonomia personalizzata chiamata product_cat. In una funzione che sto scrivendo, sto usando get_categories con il parametro taxonomy impostato su product_cat. Tutto funziona bene e riesco a ottenere l'ID del termine, il nome e anche lo slug. Quello che non riesco a capire è come ottenere il link da visualizzare. A quanto pare get_category_link non funziona con le tassonomie personalizzate e get_term_link non funziona neanche, ricevo un errore. Ecco quello che ho:

$prod_cat_args = array(
  'taxonomy'     => 'product_cat', //woocommerce
  'orderby'      => 'name',
  'empty'        => 0
);

$woo_categories = get_categories( $prod_cat_args );

foreach ( $woo_categories as $woo_cat ) {
    $woo_cat_id = $woo_cat->term_id; //ID categoria
    $woo_cat_name = $woo_cat->name; //nome categoria

    $return .= '<a href="' . get_category_link( $woo_cat_id ) . '">' . $woo_cat_name . '</a>';
}//fine del foreach $woo_categories  

Suggerimenti?

0
Tutte le risposte alla domanda 4
4
18

Un altro aggiornamento (sett. 2015):

Alla fine posso usare get_term_link. Il problema era che la stringa doveva essere convertita in un intero. Ho utilizzato un suggerimento da Stack Overflow per il modo più veloce per convertirlo usando (int)$value in PHP.

Quindi apparirebbe così se non vuoi usare lo slug nel ciclo foreach:

$woo_cat_id_int = (int)$woo_cat_id; //conversione

Questo valore convertito viene utilizzato al posto dello slug in get_term_link. Spero possa essere utile a qualcuno. :-)


Sembra che l'abbia risolto.

Ho usato get_term_link. E ricevevo un errore perché lo stavo usando in questo modo:

get_term_link( $woo_cat_id, 'product_cat' );

Che mi dava questo errore:

L'oggetto della classe WP_Error non può essere convertito in stringa

Quindi ho seguito questa strada invece con lo slug e ha funzionato:

$prod_cat_args = array(
  'taxonomy'     => 'product_cat', //woocommerce
  'orderby'      => 'name',
  'empty'        => 0
);

$woo_categories = get_categories( $prod_cat_args );

foreach ( $woo_categories as $woo_cat ) {
    $woo_cat_id = $woo_cat->term_id; //ID categoria
    $woo_cat_name = $woo_cat->name; //nome categoria
    $woo_cat_slug = $woo_cat->slug; //slug categoria


    $return .= '<a href="' . get_term_link( $woo_cat_slug, 'product_cat' ) . '">' . $woo_cat_name . '</a>';
}//fine del foreach $woo_categories  
3 ott 2014 21:27:34
Commenti

Anche se ancora non capisco perché non accetti l'ID ma accetta lo slug. Il Codex dice che get_term_link dovrebbe accettare l'ID...

RachieVee RachieVee
3 ott 2014 21:28:09

Non ha alcun senso - dovrebbe funzionare con l'id infatti... grazie mille

akmur akmur
19 nov 2014 12:46:24

Term_id è una stringa nell'oggetto. Per usarlo con la funzione get_term_link devi prima convertirlo in intero get_term_link( intval($woo_cat->term_id), 'product_cat' )

forsvunnet forsvunnet
28 gen 2015 16:54:49

La soluzione proposta da forsvunnet ha funzionato perfettamente per me

Shane Jones Shane Jones
1 set 2016 01:34:03
0

Grazie, io uso

foreach ( $terms as $term ) {
$term_link = get_term_link( $term );
 echo '<li><a href="' . esc_url( $term_link ) . '">' . $term->name . '</a></li>';
}

Funziona perfettamente.

16 dic 2014 09:57:45
0
$prod_cat_args = array(
'taxonomy'     => 'product_cat', //woocommerce
            'orderby'      => 'name',
            'empty'        => 0
            );

            $terms = get_categories( $prod_cat_args );
            //$term_id=6;
            foreach ( $terms as $term ) {
            $term_link = get_term_link( $term );
            echo '<li><a class="shopping-now" href="' . esc_url( $term_link ) . '">' . $term->name . '</a></li>';
            }

get_term_link() funziona correttamente quando viene utilizzato l'oggetto restituito da get_categories().

18 set 2015 07:51:19
0

Ho provato questo codice e funziona.

<?php $catTerms = get_terms('product_cat', array('hide_empty' => 0, 'orderby' => 'ASC', 'exclude' => '17,77')); ?>

<?php foreach($catTerms as $catTerm) : 
       // Ottiene l'ID dell'immagine in miniatura per la categoria
       $wthumbnail_id = get_woocommerce_term_meta( $catTerm->term_id,'thumbnail_id', true );
       // Recupera l'URL dell'immagine
       $wimage = wp_get_attachment_url( $wthumbnail_id );
       // Ottiene il link alla pagina della categoria
       $wcategory =$term_link = get_term_link( $catTerm );
?>
<ul>
    <li><a class="tr" href="<?php echo esc_url( $term_link ); ?>"><?php if($wimage!=""):?><img src="<?php echo $wimage?>" alt="<?php echo esc_attr($catTerm->name); ?>" title="<?php echo esc_attr($catTerm->name); ?>"><?php endif;?><?php echo $catTerm->name; ?></a></li>
</ul>
25 ago 2022 23:16:14