PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, May 10, 2022

[FIXED] How to display custom taxonomy of the WooCommerce product?

 May 10, 2022     custom-taxonomy, php, product, woocommerce, wordpress     No comments   

Issue

I would like to display a custom taxonomy name with its URL everywhere that WooCommerce products appear:

  1. on Homepage (https://rockers.pl/) - I have the best selling and the newest products shortcode
  2. on Archive pages
  3. on Single Product page

I created custom product taxonomy for the artist:

name: artysci
label: Artyƛci
singular_label: Artysta
description: ""
public: true
publicly_queryable: true
hierarchical: true
show_ui: true
show_in_menu: true
show_in_nav_menus: true
query_var: true
query_var_slug: ""
rewrite: true
rewrite_slug: ""
rewrite_withfront: true
rewrite_hierarchical: true
show_admin_column: true
show_in_rest: true
show_in_quick_edit: ""
rest_base: ""
rest_controller_class: ""
meta_box_cb: ""

and now - near the title of the product - I need to display the artist name with URL to its taxonomy. Many thanks for any help.


Solution

For archive pages:

add_action( 'woocommerce_before_shop_loop_item_title', 'add_artist_term');
function add_artist_term() {
    $terms = wp_get_post_terms(get_the_ID(), 'artysci');
    if (!is_wp_error($terms) && !empty($terms)) { ?>
        <div class="artist-term-title"><a href="<?php echo esc_url(get_term_link($terms[0])); ?>"><?php echo esc_html($terms[0]->name); ?></a></div>
    <?php }
}

You could also use the woocommerce_after_shop_loop_item_title action to put the term title after the product title

For single product page you need to add your action either before or after the title is added to the hook below.

add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );

So you would hook the same function from above onto woocommerce_single_product_summary but with a priority of 4 or 6 instead of 5.



Answered By - mrben522
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing