I create plugin and want to get brand name from woocommerce. In fist time i post product no.1 the plugin it’s work and get product brand as i wish but when i create product post no.2 and no.3 from brand name get from product no.1
whats wrong ?
if ( class_exists( 'WooCommerce' ) ) { $kode = $product->get_sku(); $terms = wp_get_post_terms( $product_id, 'brand', array('orderby'=>'name')); $brands = get_terms( 'brand', array( 'orderby' => 'name' //,'product__in' => $product->id // orderby arguments ('name', 'slug','term_group', 'term_id', 'id', 'description') ) ); foreach ( $brands as $key => $brand ) : $brand_name = $brand->name; endforeach; $merk = $brand_name; //this i want to print the brand name echo "<strong>STOCK ".$merk." ".$kode."</strong><br/>"; } else { echo "please active your WooCommerce plugin."; }
Advertisement
Answer
this should work for you.
if ( class_exists( 'WooCommerce' ) ) { $kode = $product->get_sku(); $tax = array( 'STOCK'=>'brand', ); foreach ($tax as $mk_label => $mk_value) { $make_array = wp_get_post_terms(get_the_ID(), $mk_value, array("fields" => "names")); foreach ($make_array as $value) { $mk .= $value.", "; } echo '<strong>'.$mk_label.' <a href="javascript:void(0)">'.rtrim($mk,", ").' '.$kode.'</a></strong>'; $mk =""; } } else { echo "please active your WooCommerce plugin."; }
We are taking terms dynamically based on current product id and fetching the brand accordingly. Let me know if you need any more help.
Thanks