Woocommerce specific banner product category and childs - php

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

Related

Adding parent category to url

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;
}

How to displaying product category before product title?

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

Add product category link on WooCommerce single product page

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 );

Adding Woocommerce categories below the shop images

I was working on adding the product categories to under the shop image on my site. I have it working using the code below, but the categories do not have spaces between them. I am a bit of a novice here so wondered if someone can help me where I could add a space of comma between each category listing that would be great! Thanks in advance.
add_action( 'woocommerce_after_shop_loop_item', 'after_shop_loop_item_title', 1 );
function after_shop_loop_item_title() {
global $post;
$terms = get_the_terms( $post->ID, 'videocategories' );
$text = "<h3>Category: ";
foreach ($terms as $term) {
$text .= $term->name;
}
$text .= "</h3>";
echo $text;
}
You can use the PHP implode() function. This will create a string from an array and separate them with a separator of your choosing.
In the example below I first created an array of the categories and then used the implode() function in combination with the printf() function to print a comma separated list enclosed by h3 tags:
add_action( 'woocommerce_after_shop_loop_item', 'after_shop_loop_item_title', 10 );
function after_shop_loop_item_title() {
global $post;
$terms = get_the_terms( $post->ID, 'videocategories' );
$product_cats = array();
if ( !empty( $terms ) ) {
// Get an array of the categories
foreach ( $terms as $key => $term ) {
$product_cats[] = sprintf( '%s', get_term_link( $term->slug, 'videocategories' ), $term->name );
}
// Convert product category array into comma separated string
printf( '<h3>Category: %s</h3>', implode( ', ', $product_cats ) );
}
}

Add a product tag to product name in Woocommerce archive pages

I'm trying to add product tag below product name on Woocommerce. I found a similar code here and tried to customize it but i can't get it work.
/**
* Add product's brand name to product's name.
*/
add_filter( 'the_title', 'mycode_add_brand_to_product_title', 10, 2 );
function mycode_add_brand_to_product_title( $title, $id ) {
$type = get_post_type( $id );
if ( $type != 'product' ) { return $title; }
$terms = wc_get_product_terms( $post->ID, 'product_tag' );
$brand = array_shift( $terms );
if ( !empty( $brand ) ) {
$title = $brand . ' ' . $title;
}
return $title;
}
Could someone please help?
Here's a preview what i want to achieve:
Updated: Try the following:
add_filter( 'the_title', 'mycode_add_brand_to_product_title', 10, 2 );
function mycode_add_brand_to_product_title( $title, $post_id ) {
if ( get_post_type( $post_id ) != 'product' && ! is_archive() )
return $title;
if ( ! ( is_shop() || is_product_category() || is_product_tag() ) )
return $title;
$terms = wp_get_post_terms( $post_id, 'product_tag' );
$term = reset( $terms );
if ( !empty( $term->name ) )
$title .= '<br><small>' . strtoupper( $term->name ) . '</small>';
return $title;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works. It should work for you too.

Categories