Home > Software engineering >  Woocommerce show product brand in cart page
Woocommerce show product brand in cart page

Time:11-07

What I trying to do is showing brand name under product title in cart page, for testing I didn't use function in function.php, and did direct change to cart.php but no success:

$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
$brands = wp_get_post_terms( $product_id, 'pwb-brand' );
foreach( $brands as $brand ) echo '<p>'.$brand->name.'</p>';

but nothing shows, I used var_dump($brands) in return:

object(WP_Error)#37768 (3) { ["errors"]=> array(1) { ["invalid_taxonomy"]=> array(1) { [0]=> string(22) "Invalid category" } } ["error_data"]=> array(0) { } ["additional_data":protected]=> array(0) { } }

I also tried other ways:

wp_get_post_terms($product_id, 'product_brand', ['fields' => 'names'])

return nothing and same error. What I have done wrong?

CodePudding user response:

invalid_taxonomy means there is no such taxonomy with pwb-brand or product_brand name, actually it depends on which plugin or theme are you using, for finding correct taxonomy name go to plugin page, check for your brand plugin or if it's on your theme check functions, but simple way is go to brand page in admin check url, for example:

/wp-admin/edit-tags.php?taxonomy=ourbrandname&post_type=our
//-------------------------------^

This is your target taxonomy, now try:

wp_get_post_terms($product_id, 'ourbrandname', ['fields' => 'names'])
  • Related