Woocommerce how to get product category - wordpress

I want to create a drop down list for categories in php in a page built in code. I could create a shortcode for it successfully but since shortcode is not friendly with html, I decided not to pursue that route. So without any scripts or jquery, I'm attempting to add php code into a div
<select class="store-search-input form-control" name="dokan_seller_search" value="<?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
);
$all_categories = get_categories( $args );
foreach ($all_categories as $cat) {
if($cat->category_parent == 0) {
$category_id = $cat->term_id;
echo '<br />'. $cat->name .'';
$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 $sub_category->name ;
}
}
}
}
?>" >
</select>
and this happened:
Any help would be appreciated
Update thanks to Alex's help:
I don't have enough points to add the image directly yet:
<input type="select" class="store-search-input form-control" name="dokan_seller_search" value="<?php
$args = array(
'taxonomy' => 'product_cat',
'orderby' => 'name',
'hierarchical' => true,
'hide_empty' => false
);
$all_categories = get_categories( $args );
esc_html('<select class="store-search-input form-control" name="dokan_seller_search">');
foreach($all_categories as $parent){
if ($parent->category_parent == 0) {
echo '<option value="'.$parent->name.'"><a href="'. get_term_link($parent->slug, 'product_cat') .'">'. $parent->name .'</option>';
}
$args2 = array(
'taxonomy' => 'product_cat',
'child_of' => $parent->term_id,
'parent' => $parent->term_id,
'hide_empty' => 'false'
);
$sub_cats = get_categories( $args2 );
if($sub_cats) {
foreach($sub_cats as $sub_category) {
echo $sub_category->name;
}
}
}
esc_html('</select>');
?>">
The result I'm expecting is a

What you are trying to do is a little dirty for a . You would be much better off using a dropdown triggered by javascript so you can better control the DOM in the dropdown. A select should really only be used when you need to submit data to the server.
UPDATE: This is tested and working.
<script type="text/javascript">
//First, some js to trigger the window to relocate when you click an option
jQuery(document).ready(function(){
//When the select is triggered
jQuery('#category-links').on('click', function(){
//Get the URL from the selected option
var url = jQuery(this).val();
//Navigate to the url
window.location.replace(url);
})
})
</script>
<?php
//Get the parent terms
$parents = get_terms([
'taxonomy' => 'product_cat',
'hide_empty' => false,
'parent' => 0
]);
//Start the dom for the select
echo '<select id="category-links">';
echo '<optgroup>';
//The parent option
echo '<option value="'.get_term_link($term->term_id).'">'. $term->name .'</option>';
//Check for children, if there are we'll add them here
$children = get_term_children($term->term_id, 'product_cat');
if (!empty($children)) {
foreach ($children as $child) {
$childterm = get_term_by('id', $child, 'product_cat');
echo '<option value="'.get_term_link($childterm->term_id).'">'. $childterm->name .'</option>';
}
}
echo '</optgroup>';
}
echo '<select>';

Related

Get ACF Image field from Custom Taxonomy loop/list (woocommerce)

Been banging my head for a few hours trying to sort this. Finally got a function together that will list all the terms in a custom taxonomy I created in Woocommerce > Products, which works.
What I want to do:
Get/Display the image for each term next to the title (Created an Image field with ACF for that taxonomy item) and display the description underneath.
I can probs figure out the description part, but having a hard time with getting the image to render.
Here's what I've got so far:
//---------Start ACF code
//
// Define taxonomy prefix
// Replace NULL with the name of the taxonomy eg 'category'
$taxonomy_prefix = 'item';
// Define term ID
// Replace NULL with ID of term to be queried eg '123'
$term_id = NULL;
// Define prefixed term ID
$term_id_prefixed = $taxonomy_prefix .'_'. $term_id;
//----------End ACF Code
$taxonomy = 'item';
$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
);
$all_categories = get_categories( $args );
foreach ($all_categories as $cat) {
if($cat->category_parent == 0) {
$category_id = $cat->term_id;
$image = get_field('image', $taxonomy . '_' . $term_id);
echo ' <img src="'.the_field( 'image', $term_id ) .'" /> ';
echo ''. $cat->name .'<br />';
$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 '<a class="subcat" href="'. get_term_link($sub_category->slug, 'item') .'">'. $sub_category->name .'</a><br/>';
}
}
}
}
You can see the output here:
https://doorsdev.wpengine.com/individual-flower-essence-descriptions/
When I inspect I can see the field rendering but a blank src attribute, and with no errors on the page, I'm not sure what to do next.
I'm guessing it's something to do with the $term_id, becuase it tells me to define it, but since I don't want one specific term, I want it for each term that gets pulled into the loop....I'm not sure how to define that.
Any help appreciated.
Thanks!
as shown in the code your $term_id is null. Try replacing $term_id with $category_id.
Replace this code(EDITED).
$image = get_field('image', 'term_' . $category_id);
echo ' <img src="'.$image .'" /> ';
Reference: https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/

How to display the sub subcategory in woocommerce wordpress

I am trying to get the product categories of subcategory of subcategory
<?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
);
$all_categories = get_categories( $args );
foreach ($all_categories as $cat) {
if($cat->category_parent == 0) {
$category_id = $cat->term_id;
$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 ) {
echo '<li class="title '.$cat->name.'"><a >
';
echo $cat->name ;
echo '<i class="chevron right icon"></i>
</a></li>';
$sub_cats = "";
}else {
echo '<li class="titleVide '.$cat->name.'Hide"><a >
';
echo $cat->name ;
echo '</a></li>';
$sub_cats = "";
}
$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) {
echo '<li class="content"> <p>';
foreach($sub_cats as $sub_category) {
echo '
'. $sub_category->name . '
';
}
echo ' </p></li>';
} else {
echo '';
}
$sub_cats = "";
}
} /* end foreach all_categories cat */
wp_reset_query();
?>
This code list all the top level categories and subcategories under them hierarchically, but i have subcategories of subcategory(sub-sub-category), so how can i list those sub subcategories(with clic).
Categories IN BO
List categories in FO
try this below code by using child_of = current category id
<?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
);
$all_categories = get_categories( $args );
foreach ($all_categories as $cat) {
if($cat->category_parent == 0) {
$category_id = $cat->term_id;
$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 ) {
echo '<li class="title '.$cat->name.'"><a >
';
echo $cat->name ;
echo '<i class="chevron right icon"></i>
</a></li>';
$sub_cats = "";
}else {
echo '<li class="titleVide '.$cat->name.'Hide"><a >
';
echo $cat->name ;
echo '</a></li>';
$sub_cats = "";
}
$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => $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) {
echo '<li class="content"> <p>';
foreach($sub_cats as $sub_category) {
echo '
'. $sub_category->name . '
';
}
echo ' </p></li>';
} else {
echo '';
}
$sub_cats = "";
}
} /* end foreach all_categories cat */
wp_reset_query();
?>
or use wp_list_categories() function for listing category
https://developer.wordpress.org/reference/functions/wp_list_categories/

display woocommerce category list

How can I get the category list in woocommerce?
With this code, I get wordpress category list:
function gaga_lite_category_lists(){
$categories = get_categories(
array(
'hide_empty' => 0,
'exclude' => 1
)
);
$category_lists = array();
$category_lists[0] = __('Select Category', 'gaga-lite');
foreach($categories as $category) :
$category_lists[$category->term_id] = $category->name;
endforeach;
return $category_lists;
}
I want to replace it with woocommerce category.
WooCommerce Product Category are treated as product_cat taxonomy
Here is the code.
function gaga_lite_category_lists()
{
$category_lists = array();
$category_lists[0] = __('Select Category', 'gaga-lite');
$args = array(
'taxonomy' => 'product_cat',
'orderby' => 'name',
'hierarchical' => 0, // 1 for yes, 0 for no
'hide_empty' => 0,
'exclude' => 1 //list of product_cat id that you want to exclude (string/array).
);
$all_categories = get_categories($args);
foreach ($all_categories as $cat)
{
if ($cat->category_parent == 0)
{
$category_lists[$cat->term_id] = $cat->name;
//get_term_link($cat->slug, 'product_cat')
}
}
return $category_lists;
}
$orderby = 'name';
$order = 'asc';
$hide_empty = false ;
$cat_args = array(
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
);
$product_categories = get_terms( 'product_cat', $cat_args );
if( !empty($product_categories) ){
echo '<ul>';
foreach ($product_categories as $key => $category) {
echo '<li>';
echo '<a href="'.get_term_link($category).'" >';
echo $category->name;
echo '</a>';
echo '</li>';
}
echo '</ul>';
}
You can get all Woocommerce categories and sub categories using below code:
$taxonomy = 'product_cat';//Woocommerce taxanomy name
$orderby = 'name';
$show_count = 0; //set 1 for yes, 0 for no
$pad_counts = 0; //set 1 for yes, 0 for no
$hierarchical = 1; //set 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
);
//get all woocommerce categories on the basis of $args
$get_all_categories = get_categories( $args );
foreach ($get_all_categories as $cat) {
if($cat->category_parent == 0) {
$category_id = $cat->term_id;
echo '<br />'. $cat->name .'';
//Create arguments for child category
$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
);
//Get child category
$sub_cats = get_categories( $args2 );
if($sub_cats) {
foreach($sub_cats as $sub_category) {
echo $sub_category->name ;
}
}
}
}
I hope it will help you. Thanks

Show subcategory instead of parent category

I am using the code below for displaying the WordPress post category but it outputs the parent category, not the subcategory. Can this be modified to show the subcategory instead?
<div class="category">' . get_category(get_query_var('cat'))->name . '</div>
use this code to get sub-categories by ID
<?php $cats = get_the_category($post->ID);
$sep = '';
foreach( $cats as $cat ) {
$subcats = get_categories('child_of='.$cat->term_id);
if($subcats) {
foreach( $subcats as $subcat )
{ echo $sep . $subcat->name; $sep = ', '; }
}
}
?>
if you want to show list of WordPress category then use this code
<div class="category"> <?php _e('Categories:'); ?> <?php wp_list_cats(); ?></div>
OR you want to call woocomerce category then call use this code
<div class="category">
<?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
);
$all_categories = get_categories( $args );
foreach ($all_categories as $cat) {
if($cat->category_parent == 0) {
$category_id = $cat->term_id;
echo '<br />'. $cat->name .'';
$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 $sub_category->name ;
}
}
}
}
?>
</div>

woocommerce breadcrumbs show parent and sub categories?

I'm having a really hard time with this... essentially I just want to display breadcrumbs on the product category pages in woocommerce so that it shows the main category and sub categories. Hoping some smart kind soul can help me :)
This normally could be done easily with the following code:
<?php woocommerce_breadcrumb(); ?>
However, I'm using a purchased theme with woocommerce integrated, so it doesn't have that function and I tried to integrate it but no luck.
So, I found someone who had the same issue and made my own breadcrumbs for prordu. Which works, but I just want the product category to show ONLY the main category and sub category each product have. The code shows ALL categories and sub categories, not just the one I want to display.
<?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 );
//print_r($all_categories);
foreach ($all_categories as $cat) {
//print_r($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 $sub_category->name ;
}
} ?>
<?php }
}
?>

Resources