J'ai besoin d'obtenir le nom de marque du produit, j'ai ce code
$product = wc_get_product(); $type = $product->get_type(); $name = (string)$product->get_name(); $id = (int)$product->get_id(); $sku = (int)$product->get_sku(); $precio = (int)$product->get_price(); $brand_name = $product->get_brand(); ---> ???
3 Réponses :
Utiliser get_the_terms code>
est préférable d'utiliser wc_get_post_terms () code>
à partir d'un produit ID (qui permet d'obtenir des noms à terme à la place des objets WP_TERM) et en fonction de quel plugin que vous utilisez, la taxonomie sera différente:
produit_brand code> pour le plugin de marques WOOCOMMERCE LI>
-
YITH_PRODUCT_BRAND CODE> pour Yith WooCommerce Brands Plugin Li>
-
pa_brand code> pour un attribut de produit personnalisé li>
ul> Ainsi, par exemple avec le plug-in Woocommerce Brands, vous utiliserez: P>
$product_id = get_the_id();
$product = wc_get_product( $product_id );
$taxonomy = `product_brand`;
$brand_names = wp_get_post_terms( $product_id, $taxonomy, array( 'fields' => 'names' ) );
// Get the brand name
$brand_name = reset( $brand_names );
Merci pour l'aide, j'ai utilisé ce code et cela fonctionne.
$terms = get_the_terms( get_the_ID(), 'product_brand' ); foreach ( $terms as $term ){ if ( $term->parent == 0 ) { $brand_name= $term->slug; } } echo $brand_name;
Oui, cela dépend du plugin comme @Loictheaztec expliqué
Semble que vous utilisez des plugins pour la marque, pouvez-vous nommer ce plugin? Je suppose que la marque restera des termes de cette certaine taxonomie ...