Display only subcategory product thumbnail woocommerce - php

I am having some problems displaying thumbnails for subcategory. Can anyone help me?
<?php
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ){
$category_name = $term->name;
$category_thumbnail = get_woocommerce_term_meta($term->term_id, 'thumbnail_id', true);
$image = wp_get_attachment_url($category_thumbnail);
echo '<img src="'.$image.'">';
}?>
I want to show only thumbnails of SUBCATEGORY , and the above code does not help that problem. Thks

Please check following code and try it.
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ){
if( $term->parent != 0 ) {
$category_name = $term->name;
$category_thumbnail = get_term_meta($term->term_id, 'thumbnail_id', true);
$image = wp_get_attachment_url($category_thumbnail);
echo '<img src="'.$image.'">';
}
}
Check if the Parent id is not 0 or you can check like $term->parent > 0. Get the term meta of the term_id.
get_woocommerce_term_meta function may be deprecated and you can use get_term_meta to get term meta.

Related

Get current subcategory product thumb Woocommerce

Im trying to get the product category thumb for a subcategory of a specific parent category on my product page - Im able to retrieve all the images for the subcategory of my given parent category - but I only want to show the image that belongs to the subcategory of the product. Now it shows all images on the product from all the parents child.
How do a set it so it only show the one subcategory im on, but still only from the given parent?
Code:
<?php
$catTerms = get_terms('product_cat', array('hide_empty' => 0, 'orderby' => 'ASC', 'child_of'=>'74'));
foreach($catTerms as $catTerm) : ?>
<?php $thumbnail_id = get_woocommerce_term_meta( $catTerm->term_id, 'thumbnail_id', true );
// get the image URL
$image = wp_get_attachment_url( $thumbnail_id ); ?>
<li><img src="<?php echo $image; ?>" width="152" height="245"/><span><?php echo $catTerm->name; ?></span></li>
<?php endforeach; ?>
Edit: solved by doing it this way:
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ){
if ( $term->parent == '174081' ) {
$category_name = $term->name;
$category_thumbnail = get_woocommerce_term_meta($term->term_id, 'thumbnail_id', true);
$image = wp_get_attachment_url($category_thumbnail);
echo '<img src="'.$image.'">';
}
}

How to limit returned product categories with thumbnails (WooCommerce)

My goal here is to show only child categories under category ID 93 (vendors) with a thumbnail and URL so people can click on the vendor logo and see other products in that category. I have everything mostly working, but I'm not sure how to limit my request to show only one child from my parent. This is admittedly very amateur - I am not a backend developer nor do I really understand how to write PHP.
<?php
echo $wp_query;
$terms_post = get_the_terms($product->ID, 'product_cat');
foreach ($terms_post as $term_cat) {
$term_cat_id = $term_cat->term_id;
$category_url = get_term_link( $term_cat_id, 'product_cat', true );
$thumbnail_id = get_woocommerce_term_meta($term_cat_id, 'thumbnail_id', true );
$image_url = wp_get_attachment_url( $thumbnail_id );
echo '<img src="' . $image_url . '" alt="" width="50" height="50">';
}
?>
To show only 1 child from the parent, just use array_slice().
foreach(array_slice($terms_post, 0, 1) as $term_cat ) {
$term_cat_id = $term_cat->term_id;
$category_url = get_term_link( $term_cat_id, 'product_cat', true );
$thumbnail_id = get_woocommerce_term_meta($term_cat_id, 'thumbnail_id', true );
$image_url = wp_get_attachment_url( $thumbnail_id );
echo '<img src="' . $image_url . '" alt="" width="50" height="50">';
}
Do let me know if it works.
EDITED:
Use the below code to get child categories using a parent category slug.
<?php
global $post;
$category_id = get_term_by('slug', 'PARENT-CAT-SLUG', 'product_cat');
$terms = get_the_terms($post->ID, 'product_cat');
foreach ($terms as $term) {
if($term->parent === $category_id->term_id) { ?>
<span class="product-sub-cats"><?php echo $term->name; ?></span>
<?php break;
}
}
?>
Replace "PARENT-CAT-SLUG" with the slug of your Parent Category.

Get "primary" category image from WooCommerce product

I am trying to get the featured image of the "Primary" selected category of a product.
Getting the images from the categories is not the problem, but how do I get the "primary" one?
Currently this is what I am using and of course getting all the images.
I only want to display one image.
<?php
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ){
$category_thumbnail = get_woocommerce_term_meta($term->term_id, 'thumbnail_id', true);
$image = wp_get_attachment_url($category_thumbnail);
echo '<img src="'.$image.'" alt="" />';
}?>
You need to target the terms with 'parent' argument equal to 0.
As you have set for your products only one "primary" product category for each normally, so we just take the first one anyway (but you can have many for a product):
<?php
global $post;
$term_ids = wp_get_post_terms( $post->ID, 'product_cat', array('fields' => 'ids', 'parent' => '0') );
if( count($term_ids) > 0 ){
echo '<img src="'. wp_get_attachment_url( get_woocommerce_term_meta( $term_ids[0], 'thumbnail_id', true ) ) .'" alt="" />';
}
?>
This is tested and works.

Loop through post member categories, check if desendant of x (by name)

I'm using Wordpress with WooCommerce, on the single product page I want to check if one of the product member categories is a member of a parent 'Brands' and if it is display the description from that brand member category.
I've done the following, however it outputs all the categories the product is a member of;
<?php $terms = get_the_terms( $post->ID, 'product_cat' );
if($terms){
foreach($terms as $term){
$category_parent_id = $term->parent;
$category_parent = get_category($category_parent_id);
$category_description = $term->description;
//if(($category_parent = "Brands")){ // THIS DOESNT WORK OUTPUTS ALL CATS
if( "Brands" == $category_parent ){ // THIS DOESNT WORK EITHER, OUTPUTS NO RESULTS
$category_name = $term->name;
$category_thumbnail = get_woocommerce_term_meta($term->term_id, 'thumbnail_id', true);
$image = wp_get_attachment_url($category_thumbnail); ?>
Do Some HTML Stuff Here
<?php }
}
} ?>
Brands >
Brands > Brand1 >
Brands > Brand1 > Product
Brands > Brand2 >
Brands > Brand2 > Product
Brands > Brand9999 >
Brands > Brand9999 > Product
If 'Product' is member of 'Brandx' which is a member of 'Brands' then output the description from the 'Brandx' category.
hi you just have a typo
if(($category_parent == "Brands")){ // THIS DOESNT WORK
that is why it's good to change a place to put the primitives to the left side
if( "Brands" == $category_parent ){ }
<?php $terms = get_the_terms( $post->ID, 'product_cat' );
if($terms){
foreach($terms as $term){
$category_name = $term->name;
$brand_lookup = get_term_by('name', 'Brands', 'product_cat');
$brand_lookup_id = $brand_lookup->term_id;
$brand_args = array('hierarchical' => 1,'show_option_none' => '','hide_empty' => 0,'parent' => $brand_lookup_id,'taxonomy' => 'product_cat');
$brands = get_categories($brand_args);
foreach ($brands as $brand){
if($category_name==$brand->name){
$category_description = $term->description;
$category_slug = get_term_link( $term );
$category_thumbnail = get_woocommerce_term_meta($term->term_id, 'thumbnail_id', true);
$image = wp_get_attachment_url($category_thumbnail);
echo $category_name; ?>
<h2>Some HTML funk here</h2>
<?php }
}
}
} ?>
You should use "get_term" instead of "get_category".
$category_parent = get_term($category_parent_id, 'product_cat');
This will return term object so your "if" needs to look like this:
if ($category_parent->name === 'Brand) {
...

Display category image as banner on single product page

I want to display the category image as banner in the woocommerce single product page. I'm using this code, but it shows all the category image.
if(is_single()) {
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ){
$category_name = $term->name;
$category_thumbnail = get_woocommerce_term_meta($term->term_id, 'thumbnail_id', true);
$image = wp_get_attachment_url($category_thumbnail);
echo '<img class="absolute '.$category_name.' category-image" src="'.$image.'">';
}
}
I want only to show the category image. when I click on the product, it shows its category image as banner.
Problem in code : What you have done in your coding is, you are fetching all the terms(get_the_terms) assinged to the product.That is why it is displaying all the images.
Solution : Either you can change your code to get banner image of particular product or else you can simply put dummy condition to get only one image.
Code for dummy condition :
if(is_single()) {
$terms = get_the_terms( $post->ID, 'product_cat' );
$i=0; //Variable for dummy condition
foreach ( $terms as $term ){
if($i==0): //Dummy Condition
$category_name = $term->name;
$category_thumbnail = get_woocommerce_term_meta($term->term_id, 'thumbnail_id', true);
$image = wp_get_attachment_url($category_thumbnail);
echo '<img class="absolute '.$category_name.' category-image" src="'.$image.'">';
$i++; //Increment it to make condition false
endif;
}
}
Let me know if you have any doubt.
You slect child category that why display all category image

Categories