I am having trouble adding the parent category to my URL in this code snippet. Any help would be appreciated.
Code is to show categories underneath products in woocommerce.
remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10 );
add_action( 'woocommerce_shop_loop_item_title', 'VS_woo_loop_product_title', 10 );
function VS_woo_loop_product_title() {
echo '<h3>' . get_the_title() . '</h3>';
$terms = get_the_terms( $post->ID, 'product_cat' );
if ( $terms && ! is_wp_error( $terms ) ) :
// only displayed if the product has at least one category
$cat_links = array();
foreach ( $terms as $term ) {
$cat_links[] = ''.$term->name.'';
}
$on_cat = join( ", ", $cat_links );
?>
<div class="label-group">
<div class="categories-link"><?php echo $on_cat; ?></div>
</div>
<?php endif;
}
Related
I have been trying to display the product sub-category name above the product title on the Shop or Product Archive page.
I've tried the code below but I am not sure how to get sub-categories instead of the category:
function category_single_product(){
$product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );
if ( $product_cats && ! is_wp_error ( $product_cats ) ){
$single_cat = array_shift( $product_cats ); ?>
<h2 itemprop="name" class="product_category_title"><span><?php echo $single_cat->name; ?></span></h2>
you can use get_term_children() function
$list_sub_cats = get_term_children($single_cat->term_id,'ca');
I hope this will help you, Visit to get more info https://developer.wordpress.org/reference/functions/get_term_children/
You can use woocommerce_single_product_summary and set priority. Try the below code. Code will go in your active theme functions.php file.
function show_sub_category_before_title(){
$terms = get_the_terms( get_the_ID(), 'product_cat' );
if ( $terms && ! is_wp_error( $terms ) ) {
$product_cat = array();
foreach ( $terms as $term ) {
if( $term->parent != 0 ) {
$product_cat[] = $term->name;
}
}
$product_cat = join( ", ", $product_cat );
echo $product_cat;
}
}
add_action( 'woocommerce_single_product_summary', 'show_sub_category_before_title', 1, 1 );
Tested and Works
This code is outputting the Product (Brand) Category in (almost) the right spot - above the product title - on the Single Product Page.
How can I hyperlink it (Product Category) to the Product Category page? I essentially need to make this <?php echo $single_cat->name; ?> a dynamic hyperlink.
/** Output Product (Brand) Category on single product page **/
function add_brand_category(){
$product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );
if ( $product_cats && ! is_wp_error ( $product_cats ) ){
$single_cat = array_shift( $product_cats ); ?>
<h3 itemprop="name" class="product_category_title"><span><?php echo $single_cat->name; ?></span></h3>
<?php }
}
add_action( 'woocommerce_single_product_summary', 'add_brand_category', 2 );
You can use wc_get_product_category_list() – which returns the product categories in a list + adding a hyperlink to the product category page.
So you get:
function action_woocommerce_single_product_summary() {
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
echo '<h3 itemprop="name" class="product_category_title">';
echo wc_get_product_category_list( $product->get_id(), ', ', '<span>' . _n( 'Category:', 'Categories:', count( $product->get_category_ids() ), 'woocommerce' ) . ' ', '</span>' );
echo '</h3>';
}
}
add_action( 'woocommerce_single_product_summary', 'action_woocommerce_single_product_summary', 2 );
I am using WooCommerce and I want to get the current product attribute URL to be used on the single product page, replacing the code <category_url>/?filter_preco= by the URL.
Here is my code:
add_action( 'woocommerce_single_product_summary', 'cj_show_attribute_links', 4 );
function cj_show_attribute_links() {
global $post, $product;
$attribute_names = array( 'pa_preco' ); // Insert attribute names here
foreach ( $attribute_names as $attribute_name ) {
$taxonomy = get_taxonomy( $attribute_name );
if ( $taxonomy && ! is_wp_error( $taxonomy ) ) {
$terms = wp_get_post_terms( $post->ID, $attribute_name );
$terms_array = array();
if ( ! empty( $terms ) ) {
foreach ( $terms as $term ) {
$archive_link = $term->slug;
$base = '<category_url>?filter_preco=';
$full_line = '<h4 style="font-size: 15px; color: #4E4E4E;">'. $term->name . '</h4><div class="is-divider small"></div>';
array_push( $terms_array, $full_line );
}
echo ' ' . implode( $terms_array);
}
}
}
}
Any idea?
There are some mistakes in your code… Use this replacement code that uses get_term_link() WordPress function to get the term link as follows:
add_action( 'woocommerce_single_product_summary', 'display_linked_product_attributes', 4 );
function display_linked_product_attributes() {
global $post, $product;
$taxonomy = array( 'pa_preco' ); // Here define product attribute(s) taxonomy(ies)
foreach ( $attribute_names as $attribute_name ) {
$terms = wp_get_post_terms( get_the_ID(), $taxonomy );
$output = '';
foreach ( $terms as $term ) {
$link = get_term_link( $term, $taxonomy );
$output .= '<h4 style="font-size: 15px; color: #4E4E4E;">'. $term->name . '</h4><div class="is-divider small"></div>';
}
echo ' ' . $output;
}
}
Code goes in functions.php file of the active child theme (or active theme). It should better work.
I got the solution by using this (I am getting a error when the product has no category)
function pagina_produto_embaixo_titulo() {
global $post;
$attribute_names = array( 'pa_preco' ); // Insert attribute names here
foreach ( $attribute_names as $attribute_name ) {
$taxonomy = get_taxonomy( $attribute_name );
if ( $taxonomy && ! is_wp_error( $taxonomy ) ) {
$terms = wp_get_post_terms( $post->ID, $attribute_name );
$terms_array = array();
if ( ! empty( $terms ) ) {
foreach ( $terms as $term ) {
$archive_link = $term->slug;
$terms = get_the_terms( $post->ID, 'product_cat' );
$link = get_term_link( $terms[0]->term_id, 'product_cat' );
$base = $link .'?filter_preco=';
//erro nas 2 linhas acima, quando o produto nao tem categoria, e na pagina da loja, deixa de funcuonar algus coisas.
$full_line = '<h4 style="font-size: 15px; color: #4E4E4E;">O que é µStar+? | '. $term->name . '</h4><div>';
array_push( $terms_array, $full_line );
}
echo ' ' . implode( $terms_array);
}
}
}
}
I need an imploded list of terms from three custom Woocommerce taxonomies including the taxonomy name to display in the product loop. I'll also need to be able to display multiple terms for each taxonomy. However, I can only get it to work with one taxonomy. And I can't figure out how to display the corresponding taxonomy name.
My custom taxonomies are 'artists', 'illustrators' and 'authors'. Here's what I'm trying to accomplish:
Robert Douglas (Author), Bill Johnston (Illustrator), Kyle McBeth (Artist)
function list_author_terms() {
global $post;
$person = get_the_terms(get_the_ID(), 'authors', 'artists', 'illustrators');
if ( $person
&& !is_wp_error( $person )
) {
#usort( $person, function ( $a, $b )
{
return strcasecmp(
$a->slug,
$b->slug
);
});
// Display your terms as normal
$term_list = [];
foreach ( $person as $term )
$term_list[] = '' . esc_html( $term->name ) . '<span class="attribute"> (Author)</span> ';
$term_names[] = $term->name;
echo implode( ', ', $term_list);
echo '<br>';
}
}
Add follows code snippet to achieve your task -
add_action( 'woocommerce_after_shop_loop_item', 'list_author_terms', 6 );
function list_author_terms(){
$taxonomies = array( 'authors', 'artists', 'illustrators' );
$pro_list_terms = array();
foreach ( $taxonomies as $taxonomy ) {
$term_obj_list = get_the_terms( get_the_ID(), $taxonomy );
$tax_obj = get_taxonomy( $taxonomy );
if( $term_obj_list && ! is_wp_error( $term_obj_list ) ){
foreach ( $term_obj_list as $term ) {
$link = get_term_link( $term, $taxonomy );
$pro_list_terms[] = '' . $term->name . ' (' .$tax_obj->labels->singular_name . ')';
}
}
}
echo join( ', ', $pro_list_terms );
}
I've searched a lot and I did not find any specific to this, or at least, my development skills are yet not great, if someone can help I appreciate.
My goal is to show a text on each product that is inside the category X or sub-category of that same X.
For example:
Fruits
-Bananas
If the bellow product is inside Bananas it will show the code too because belongs to Fruits.
add_filter( 'woocommerce_short_description', 'single_product_short_description', 10, 1 );
function single_product_short_description( $post_excerpt ){
if ( is_product_category(array(140,20)) )
$post_excerpt = $post_excerpt. '<br><div class="product-message"><p>' . __( "Whats the measure? <a href='/#right-measure' target='_blank'>Check</a>.", "woocommerce" ) . '</p></div>';
return $post_excerpt;
}
I know my code is not good because each time the client add a sub-category I have to fix it...
I managed doing this:
add_filter( 'woocommerce_short_description', 'single_product_short_description', 10, 1 );
function single_product_short_description( $post_excerpt ){
global $post;
$terms = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ) $categories[] = $term->slug;
if ( in_array( 'piercings', $categories ) || in_array( 'piercings-en', $categories ) ) {
$post_excerpt = $post_excerpt. '<br><div class="product-message"><p>' . __( "Bla bla bla", "woocommerce" ) . '</p></div>';
}
return $post_excerpt;
}
Hope this helps someone