I am developing a custom theme for a client of mine selling mainly mobile devices. I am using WP All Import Pro to import products 4 times a day with a cron job and I am updating the stock levels every hour also using cron jobs both from an external xml link.
All that works fine but my client wants a new menu to display all the products by Brand Name.
There are plenty of plugins like WooCommerce brands, yith and perfect brands but they all require me to add the brands manually.
When there are 550+ products and there updated at least 4 times a day this becomes impossible.
I have added the brand in custom fields as you can see in this screengrab...
I have added a menu called brands but there just custom links and go nowhere. I am unsure how to code the brand custom field into this menu. Here is the website I am developing so you have an idea of what's needed..
Many thanks for any pointers you may have.
Instead of a default WordPress sidebar you can use your own custom sidebar with a new custom navigation.
Please, check the following code and you'll see the categories list:
<nav>
<ul>
<?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 '<li>'. $cat->name .'</li>';
$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 ;
}
}
}
}
?>
</ul>
</nav>
Just set the Category (as a brand) for the products and you'll see a simple result.
Related
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/
Well, it may sound easy but I searched a lot but could not find the appropriate answer. I want to check if a category contains any sale products.
For example, there are 10 categories. I want to get categories that contain products with sale and exclude those categories that don't contain any sale items.
Well, I am new to WP.
I searched a lot about the solution and finally I managed to write my own logic for answering this.
Steps:
Get all categories
Get all ids of sale products
loop through categories of step 1
get products id of this category
loop through ids of ids of step 4
check if that id is present in list of ids of sale product
if found, set flag to true and exit
if flag is false (no product on sale), unset the category
Here is my sample code:
<?php
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => true,
'parent' => 0,
);
$categories = get_terms( 'product_cat', $args );
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$query_args = array(
'post_status' => 'publish',
'post_type' => 'product',
'posts_per_page' => -1,
'paged' => $paged,
'orderby' => 'ASC',
'meta_query' => WC()->query->get_meta_query(),
'post__in' => array_merge( array( 0 ), wc_get_product_ids_on_sale() )
);
$sale_products = new WP_Query( $query_args );
$products_ids_on_sale = wc_get_product_ids_on_sale();
if($sale_products->have_posts()) {
foreach ($categories as $key => $category) {
//get product ids
$category_product_ids = get_posts( array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish',
'fields' => 'ids',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $category->term_id,
'operator' => 'IN',
)
),
) );
$is_product_exist = false;
if ( ! empty($category_product_ids) ) {
foreach ($category_product_ids as $product_id) {
if (in_array($product_id, $products_ids_on_sale)) {
$is_product_exist = true;
break;
}
}
if ( $is_product_exist === false ) {
unset($categories[$key]);
}
}
}
wp_reset_query();
}
I have written a article on my blog and explain in more detail about the produces and code at https://www.kodementor.com/get-only-categories-that-contain-products-on-sale/
This code display only on sale product in your store.
Please add below code in your active theme functions.php file
add_action( 'woocommerce_product_query', 'only_sale_product_in_store' );
function only_sale_product_in_store( $q ){
$product_ids_on_sale = wc_get_product_ids_on_sale();
$q->set( 'post__in', $product_ids_on_sale );
}
May is useful for you. Thanks
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 }
}
?>
I am working on a online shop project. I this i created a post type i.e Products and taxonomy products_categories Men Wearing, Women wearing and kids etc. In sidebar i created a widget and display all the custom taxonomies. Now i want to display all products related to that particular category. So when i click on any category suppose men then my url looks like "htp://mydomain/mysite/product_categories/kids-clothing/". I created a taxonomy.php but still 404.php is running. Can somebody tell me whats wrong in ht code.
'
<?php
$taxonomy = 'product_categories';
$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
);
?>
<h4 class="title1">Show By Category</h4>
<ul id="list">
<?php
wp_list_categories( $args );
?>
</ul>'
Thanks.
I have 2 custom post types called 'project' and 'client' that share a taxonomy called 'sector'.
if (!is_taxonomy('sector')) {
register_taxonomy(
'sector', array('project', 'client'), array(
'hierarchical' => true,
'label' => 'Sector',
'query_var' => true,
'rewrite' => array( 'slug' => 'sector' ),
'with_front' => false
) );
wp_insert_term('Health', 'sector');
wp_insert_term('Clubs', 'sector');
wp_insert_term('Commercial', 'sector');
}
I have created a taxonomy archive template with a sidebar nav that lists links to my taxonomy archives using:
//list terms in a given taxonomy using wp_list_categories
$orderby = 'name';
$show_count = 1; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 0; // 1 for yes, 0 for no
$show_option_none='';
$title = '';
$args_sector = array(
'taxonomy' => 'sector',
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title
);
<ul id="sideNav" class="rightSubMenu">
<h3 class="rightSubNav">SECTOR</h3>
<ul id="sideNav" class="rightSubMenu">
<?php wp_list_categories( $args_sector ); ?>
</ul>
</ul>
The problem is if I have a project that is linked to 'clubs' and a client that is linked to 'clubs' the output count shows 2. Also the archive page shows 2 posts - 1 for project and one for client. But there is only one project.
I am mainly concerned with the project page and would like to filter the results by my 'project' post type. I looked through the codex and the wp_list_categories function doesn't seem to accept a parameter to do this.
Can anyone help? Is there a better way to do this?
I have had a similar problem. I did this by cloning the wp_list_categories function, giving it a different name and putting in this code after the line: $categories=get_categories($r):
foreach ($categories as $key => $category){
$temp = array ( 'post_type'=>$r['type'], 'tax_query' => array(
array (
'taxonomy' => $category->taxonomy,
'field' => 'slug',
'terms' => $category->slug
)
)
);
$pauli = new wp_query($temp);
if($pauli->post_count==0){
unset($categories[$key]);
}
}
As you can see, it removes categories that do not have any of the post type you need, and then continues the process as wp_list_categories does normally.