I have a Wordpress (version 4.2.2) eCommerce site running WooCommerce (version 2.3.8).
On my individual product page I wish to set the title of the product to also include the custom categories I have created in WooCommerce and that this product belongs to.
I find the following file (wp-content/themes/mytheme/woocommerce/single-product/title.php) that relates to the title of the individual product and edit it as below to try and include the categories that this product belongs to in the title as well.
With the code below I manage to display the categories, but the problem is that I am displaying ALL categories, and not just the categories that this product belongs to.
How do I limit the categories returned to only the categories that the product belongs to please?
<?php
if ( ! defined( 'ABSPATH' ) )
exit; // Exit if accessed directly
?>
<!-- Original Product Title START -->
<h1 itemprop="name" class="product-title entry-title">
<?php the_title(); ?>
</h1>
<!-- Original Product Title END -->
<!-- New Product Title START -->
<h1 itemprop="name" class="product-title entry-title">
<?php
$taxonomy = 'product_cat';
$orderby = 'name';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$empty = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
?>
<?php
$all_categories = get_categories( $args );
foreach ($all_categories as $cat)
{
if($cat->category_parent == 0)
{
$category_id = $cat->term_id;
?>
<?php
echo '<br />'. $cat->name .'';
?>
<?php
$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => 0,
'parent' => $category_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$sub_cats = get_categories( $args2 );
if($sub_cats)
{
foreach($sub_cats as $sub_category)
{
echo '<br/>'. $sub_category->name .'';
echo apply_filters( 'woocommerce_subcategory_count_html', ' <span class="cat-count">' . $sub_category->count . '</span>', $category );
}
}
?>
<?php
}
}
?>
</h1>
<!-- New Product Title END -->
you can simply get all the categories assign to product by using get_the_terms
$terms = get_the_terms( get_the_ID(), 'product_cat' );
foreach ($terms as $term) {
echo '<h1 itemprop="name" class="product-title entry-title">'.$term->name.'</h1>';
}
Use the get_categories function to retrieve categories name :-
You can use this code to display product categories name -
<?php global $post, $product;
$categ = $product->get_categories();
echo $categ; ?>
Related
Im building a quick order form/ order table in Woocommerce
What I'm going for is something like this
Main category one
Subcategory
product
product
Subcategory
product
product
Main category two
Subcategory
product
product
Subcategory
product
product
My question is how do I query products under their correct categories
This is what i have so far
<?php
$custom = array(
'post_type' => 'product',
'posts_per_page' => 100,
);
$cust_query = new WP_Query( $custom );
if ( $cust_query->have_posts() ) :
while( $cust_query->have_posts() ) : $cust_query->the_post();
$cat = get_the_terms( get_the_ID(), 'product_cat' );
?>
<?php echo $cat[0]->name; ?>
<div><?php the_title();?></div>
<?php endwhile;
endif;
This is querying each category one at a time I want all product that have the same category to be under the correct category with the category only printed once
Try with below code. Here i have assumed that you are having one level category only.
Main category one //level 0
Subcategory //level 1
product
product
Subcategory
product
product
Main category two //level 0
Subcategory //level 1
product
product
Subcategory
product
product
<ul class="category-sidebar">
<?php
$get_parent_cats = array(
'parent' => '0', //get top level categories only
'taxonomy'=>'product_cat',
'hide_empty' => false
);
$all_categories = get_categories( $get_parent_cats );//get parent categories
foreach( $all_categories as $single_category ){
//for each category, get the ID
$catID = $single_category->cat_ID;
echo '<li>' . $single_category->name . ''; //category name & link
$get_children_cats = array(
'child_of' => $catID, //get children of this parent using the catID variable from earlier
'taxonomy'=>'product_cat',
'hide_empty' => false
);
wp_reset_postdata();
$child_cats = get_categories( $get_children_cats );//get children of parent category
if(count($child_cats)>0){
echo '<ul class="children">';
foreach( $child_cats as $child_cat ){
//for each child category, get the ID
$childID = $child_cat->cat_ID;
//for each child category, give us the link and name
echo '' . $child_cat->name . '';
wp_reset_postdata();
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $childID,
),
),
);
$loop = new WP_Query($args);
echo "<ul>";
if($loop->have_posts()) {
while ( $loop->have_posts() ) {
$loop->the_post();
// do something
echo "<li> Product Name : ".get_the_title()."</li>";
}
}
echo "</ul>";
}
echo '</ul></li>';
}else{
//$catID
wp_reset_postdata();
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $catID,
),
),
);
$loop = new WP_Query($args);
echo "<ul>";
if($loop->have_posts()) {
while ( $loop->have_posts() ) {
$loop->the_post();
// do something
echo "<li> Product Name : ".get_the_title()."</li>";
}
}
echo "</ul>";
}
} //end of categories logic ?>
I found a nice code(shown below) from this Website.
It works great but I need to get a specific child category by 'id'.
For example, if the output from the code is:
Red
Blue
Green
Yellow
How to get only Green because I need to make another query to use it in
'tax_query'=>array('field'=>'id')
Here is the function:
//woocommerce get sub categories from parent id
function woocommerce_subcats_from_parentcat_by_ID($parent_cat_ID) {
$args = array(
'hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => 0,
'parent' => $parent_cat_ID,
'taxonomy' => 'product_cat'
);
$subcats = get_categories($args);
foreach ($subcats as $sc) {
$link = get_term_link( $sc->slug, $sc->taxonomy );
echo $sc->name.'</br>';
}
}
Here is the code where I have to get the specific id of the category:
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
global $product;
$product = $cart_item['data'];
if ( has_term( 'phone-model', 'product_cat', $product->id ) ) {
$cat_check = true;
$term_list = wp_get_post_terms( $product->id,'product_cat',array('fields'=>'ids'));
$cat_id = (int)$term_list[0];
$funspecificsub = woocommerce_subcats_from_parentcat_by_ID($cat_id);
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id', //This is optional, as it defaults to 'term_id'
'terms' => $funspecificsub,
'include_children' =>true
)
)
);
$loop = new WP_Query( $args );
$i=1;
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
if($product->is_visible()){
echo '<li style=">';
echo '<a id="cover_'.$i.'" class=" '.$product->id.'" >';
echo '<div class="">'.get_site_url().'/?add-to-cart='.$product->id.'</div>';
echo '<h5>'.get_the_title().'</h5>';
echo '<h6>'.wc_price($product->get_price_including_tax(1,$product->get_price())).'</h6>';
echo '</a>';
echo '</li>';
}else{}
$i++;
endwhile;
wp_reset_query();
}
}
So I guess the main question is how do I get 'Green' category id in variable $funspecificsub above. At the moment its outputting all the Sub categories. I want to be able to select specific Sub Category.
I am listing out product categories on my website but for some reason the permalink to the product inside the loop is just leading the user to a blog post and not to the product or category etc:
<div class="mobile-show">
<?php
$args = array(
'number' => $number,
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
'include' => $ids
);
$product_categories = get_terms( 'product_cat', $args );
foreach( $product_categories as $cat ) { ?>
<?php echo '<ul class="cat_list_mobile">'; ?>
<a href="<?php echo get_permalink(); ?>">
<?php echo '<li><div class="col-group-2">' . $cat->name . '</div><div class="col-group-2 text-right"><i class="fa fa-chevron-right"></i></div></li>
</a>
</ul>';
}
?>
</div>
It is displaying the categories as expected, maybe I am missing something here?
If you need to get permalink of the product category then use get_category_link( $category_id );
More in the codex: https://codex.wordpress.org/Function_Reference/get_category_link
I need to show lists in this order.. parent-sub-category-title => sub-category-title => sub-category posts. The following code gets me the parent title but doesn't give me the posts. Can anyone tell me why?
parent category title
sub-category title
sub-category-post
//get all categories then display all posts in each term
$taxonomy = 'commongood-categories'; //change this name if you have taxonomy
$param_type = 'category__in';
$term_args=array(
'orderby' => 'name',
'order' => 'ASC'
);
$terms = get_terms($taxonomy,$term_args);
if ($terms) {
foreach($terms as $term){ //this foreach is for top level
if($term->parent == 0){
echo '<h2>'.$term->name.' </h2>'; //get top level category name
$term_args=array(
'orderby' => 'name',
'order' => 'ASC',
'child_of' => $term->term_id
);
$termss = get_terms($taxonomy,$term_args);
foreach( $termss as $terms ) {
$args=array(
"$param_type" => array($terms->term_id),
'post_type' => 'commongood',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = new WP_Query($args);
<?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
<li class="series-bubble">
<div class="stext">
<span class="stitle"><?php if(get_field('optional_title')) { echo get_field('optional_title'); } else echo get_the_title(); ?></span>
<span class="scontent"><?php echo get_the_excerpt(); ?></span>
</div>
</li>
<?php endwhile;
}
}
}
}
You'll have to use a tax_query arg for custom taxonomies, category__in only works for categories: http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
Also, caller_get_posts has been deprecated for a while, use ignore_sticky_posts instead.
I am trying to get the woocommerce subcategories with products to show under the main categories.
<ul class="wsubcategs">
<?php
$wsubargs = array(
'hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => 0,
'parent' => $category->term_id,
'taxonomy' => 'product_cat'
);
$wsubcats = get_categories($wsubargs);
foreach ($wsubcats as $wsc):
?>
<li><?php echo $wsc->name;?>
</li>
<?php
endforeach;
?>
</ul>
So far this has gotten me the corrent subcategories under the correct main categories, but no products are being shown under them.
Any advice would be appreciated.
O.
There are many ways to do this in Wordpress. You could do a custom query using the WP_Query object to get all of the products in that category, which would be the most flexible option, but there is an easier way.
Woocommerce provides shortcodes specifically for showing products in a specific category. The output would use the templates that are already built in to Woocommerce.
<?php echo do_shortcode('[product_category category="appliances"]');?>
This will give you the products under a specific category.
In your code, you might do something like this:
<ul class="wsubcategs">
<?php
$wsubargs = array(
'hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => 0,
'parent' => $category->term_id,
'taxonomy' => 'product_cat'
);
$wsubcats = get_categories($wsubargs);
foreach ($wsubcats as $wsc):
?>
<li>
<?php echo $wsc->name;?>
<div class="products">
<?php echo do_shortcode('[product_category category="'.$wsc->slug.'"]');?>
</div>
</li>
<?php endforeach;?>
</ul>
I noticed in your question that you're outputting a list. It seems likely to me that you wouldn't want to actually output the products (detailed template) below each of the categories, but you might rather want to show the number of products or product titles in a sublist.
Here's how you would show the number of products:
count;?>
You would use this anywhere in the foreach loop you have above.
Here's how you would show a sublist of titles of products in the list element for each category:
<?php $subcategory_products = new WP_Query( array( 'post_type' => 'product', 'product_cat' => $wsc->slug ) );
if($subcategory_products->have_posts()):?>
<ul class="subcat-products">
<?php while ( $subcategory_products->have_posts() ) : $subcategory_products->the_post(); ?>
<li>
<a href="<?php echo get_permalink( $subcategory_products->post->ID ) ?>">
<?php the_title(); ?>
</a>
</li>
<?php endwhile;?>
</ul>
<?php endif; wp_reset_query(); // Remember to reset ?>
And here's how that would look in your code above:
<ul class="wsubcategs">
<?php
$wsubargs = array(
'hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => 0,
'parent' => $category->term_id,
'taxonomy' => 'product_cat'
);
$wsubcats = get_categories($wsubargs);
foreach ($wsubcats as $wsc):
?>
<li>
<?php echo $wsc->name;?>
<?php $subcategory_products = new WP_Query( array( 'post_type' => 'product', 'product_cat' => $wsc->slug ) );
if($subcategory_products->have_posts()):?>
<ul class="subcat-products">
<?php while ( $subcategory_products->have_posts() ) : $subcategory_products->the_post(); ?>
<li>
<a href="<?php echo get_permalink( $subcategory_products->post->ID ) ?>">
<?php the_title(); ?>
</a>
</li>
<?php endwhile;?>
</ul>
<?php endif; wp_reset_query(); // Remember to reset ?>
</li>
<?php endforeach;?>
</ul>
**Show Category and Sub Category in search Box**
----------
<?php
//template name:house
?>
<style>
.abc {
margin-left: 10px;
}
</style>
<?php
$taxonomy = 'product_cat';
$orderby = 'name';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$empty = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
?>
<select>
<?php
$all_categories = get_categories( $args );
foreach ($all_categories as $cat)
{
if($cat->category_parent == 0)
{
$category_id = $cat->term_id;
$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
$abc=$cat->name;
if($abc=="") {
?>
<option value="<?php echo $cat->slug;?>"><?php echo $abc; ?></option>
<?php }
$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => 0,
'parent' => $category_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$sub_cats = get_categories( $args2 );
if($sub_cats)
{
foreach($sub_cats as $sub_category)
{
?>
<?php
if($sub_cats->$sub_category == 0)
{
$suv= $sub_category->cat_name;?>
<option class="abc" value="<?php echo $sub_category->slug; ?>"><span class="abc"><?php echo $suv; ?></span></option>
<?php } } } } } ?>
</select>