Woocommerce Sub Category Template - php

So I have decided I wanted 2 different templates for my Woocommerce shop.
I found a nice little chunk that basically told me to edit taxonomy-product_cat.php
All of my shop pages are category/archive pages. And My Parent Categories have about 50 Sub Cats a piece.
So My code is as follows in taxonomy-product_cat.php :
if (is_product_category( 'outdoor-furniture' ) ){ wc_get_template( 'archive-product.php' );
}
else { wc_get_template( 'archive-product-list.php' );
}
The problem is that archive-product.php is only being applied to the "outdoor-furniture" category and none of its subcategories.
I understand I could list || is_product_category( 'another-cat' ) but to do it for 50 is ridiculous.
I have tried several things for several hours. Stuff that involved filters to the functions file. If statements for this taxonomy-product_cat file. I cant seem to get anything working.
As always any help is greatly appreciated.
Cheers

Just testing your code:
if (is_product_category( 'books' ) || is_product_category( 'magazines' ) ):
wc_get_template( 'archive-product.php' );
else:
wc_get_template( 'archive-product-list.php' );
echo 'Hellllllooooo world';
endif;
Actually worked for me in switching the Woocommerce templates depending on the product category in view.

High All,
I found a working solution:
if ( has_term( 'outdoor-furniture', 'product_cat' ) ) {
wc_get_template( 'archive-product.php' );
}
else { wc_get_template( 'archive-product-list.php' );
}
Thanks for anyone who viewd. Hope this helps someone in the future!
Cheers

Related

How to hide a specific page instead all pages from search

I am trying to hide a few pages from WordPress Search results. For that, I have written a code.
function jp_search_filter( $query ) {
if ( ! $query->is_admin && $query->is_search && $query->is_main_query() ) {
$query->set( 'post__not_in', array( 10,11,20,105 ) );
}
}
add_action( 'pre_get_posts', 'jp_search_filter' );
However, this code doesn't seem to be working. The pages that I wanted to hide from WordPress search results are still showing. What I have done in the above code is I passed the page IDs as an array.
Can anyone help me with the code? Thank you so much.

WordPress Pagination problem for one category - pagination fix

somebody tried to fix pagination problem on my site by adding a function to your functions.php file[![screenshot - console][1]][1]
unfortunately, after updating wordpress or acf, this function does not work and when you try to go to the next page in the "atom" category, it displays 404 - Sorry, this page does not exist.
The pagination problem concerns only one category (subcategory). In the functions.php file I found a function like this:
function fix_atom_category_paged_query( $q ) {
if ( ! is_admin() && is_category( 'atom' ) && $_GET['debug'] == 1 ) {
$q->set( 'post_type', 'post' );
$q->set( 'posts_per_page', 9 );
// wp_die( var_dump( $q ) );
return $q;
}
}
add_action( 'pre_get_posts', 'fix_atom_category_paged_query', 2, 1 ); ```
[1]: https://i.stack.imgur.com/rKqzT.png
I suspect that the problem with the category called "atom" is due to the canonical link building and the wordpress core build itself. Names such as rss, feed, rss2, rdf, atom may conflict.
Ok, I tested it on another site. If category is called "atom", then wordpress pagination does not work for the archive of this category. I think this is WordPress problem.

Multiple Woocommerce Product Templates

I am developing a wordpress website which uses woocommerce for e-commerce functionality. I have 3 categories on the website and each one will have it's own template assigned for products within these categories.
I have created the templates and have got two of them working fine. However I'm not sure how to call the third template within my single-product.php file which contains the following code to change the templates depending on what category the product is assigned to:
<?php while ( have_posts() ) : the_post(); ?>
<?php global $post;
$terms = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ) $categories[] = $term->slug;
if ( in_array( 'legal', $categories ) ) {
woocommerce_get_template_part( 'content', 'single-product-legal' );
} else {
woocommerce_get_template_part( 'content', 'single-product-merc' );
} ?>
<?php endwhile; // end of the loop. ?>
the templates i have are:
single-product-legal (custom template)
single-product-merc (default woocommerce template)
single-product-show (custom template)
The categories are legal, show and merchandise.
I need help with the php code so I can switch between the 3 templates. Not sure if I should use a switch statement, or how to implement it or if I could use elseif or how to implement that. Even if there's a completely different way to achieve this, I'd love to know.
Any pointers would be appreciated.
The best it to use elseif:
if ( in_array( 'legal', $categories ) ) {
woocommerce_get_template_part( 'content', 'single-product-legal');
}elseif (in_array('show', $categories)){
woocommerce_get_template_part('content', 'single-product-show');
}else {
woocommerce_get_template_part( 'content', 'single-product-merc');
}
Rather than adding a seperate elseif for merchandise you can just do it in the else like above because by default if it is not legal or show it must be merchandise, but you may just add another elseif as well with the same basic structure.

Woocommerce Multiple single product templates using single-product.php to redirect

I've been pulling my hair out with this all day, please forgive the short description, I just need to validate my sanity!!
As the title says, I'm trying to create two or three different single-product layouts within woocommerce. The minimum is trying to achieve would be to have multiple single-product folders each with their own name and configurations.
No matter which way I try to override the single-product.php and make this file use logic to check for the product_cat and give out templates accordingly, I either the page not loading or what I write is skipped over and the default is loaded.
So far I've been through the following methods multiple times, trying to piece together what may be outdated code or otherwise causing all the fuss:
WooCommerce - How to create multiple single product template based on category?
Woocommerce single product - template by categories
Creating a different template file for certain Product Categories - Wordpress/Woocommerce?
I was more hoping someone may know something about this that I'm obviously missing as there are many articles out there on what to try and most claim success but I'm unable to do so.
[Update] using template_include code from #helgatheviking
No success just yet but here's where I'm up to;
File structure
team-shops is the category I'm trying to get
/mytheme/woocommerce/single-product.php - no changes
/mytheme/woocommerce/content-single-product.php
/mytheme/woocommerce/single-product-team-shops.php - changed line 37 to<?php wc_get_template_part( 'content', 'single-product-team-shops' ); ?>
/mytheme/woocommerce/content-single-product-team-shops.php - added additional id to #product-id (line 39)
/mytheme/woocommerce/single-product-team-shops/ folder with all single product files to change.
As I said above this isn't working but hopefully with what I've provided the problem may be more obvious.
Thanks again for any help :)
[Think I've got it]
Ok so I think I've something that works, at least for now it seems to, still have some further testing to do but any thoughts more than welcome, this is what I've got so far along with a single-product-team-shops folder in my theme
add_filter( 'woocommerce_locate_template', 'so_25789472_locate_template', 10, 3 );
function so_25789472_locate_template( $template, $template_name, $template_path ){
$term_id = 2854;
$taxonomy_name = 'product_cat';
$term_children = get_term_children( $term_id, $taxonomy_name );
foreach ( $term_children as $child ) {
// on single posts with mock category and only for single-product/something.php templates
if( is_product() && has_term( $child, 'product_cat' ) && strpos( $template_name, 'single-product/') !== false ){
// replace single-product with single-product-mock in template name
$mock_template_name = str_replace("single-product/", "single-product-team-shops/", $template_name );
// look for templates in the single-product-mock/ folder
$mock_template = locate_template(
array(
trailingslashit( $template_path ) . $mock_template_name,
$mock_template_name
)
);
// if found, replace template with that in the single-product-mock/ folder
if ( $mock_template ) {
$template = $mock_template;
}
}}
return $template;
}
Use a single-product-custom.php template for any product in the "custom" category:
add_filter( 'template_include', 'so_43621049_template_include' );
function so_43621049_template_include( $template ) {
if ( is_singular('product') && (has_term( 'custom', 'product_cat')) ) {
$template = get_stylesheet_directory() . '/woocommerce/single-product-custom.php';
}
return $template;
}
NB: If you use the same action hooks in your single-product-custom.php template you will get the same look as the default single-product.php. You could 'rename' all the hooks and then could add existing functions (such as those for add to cart buttons, etc) to the new hooks in order to achieve a totally custom look.

Creating a different template file for certain Product Categories - Wordpress/Woocommerce?

I am trying to make different template file depending on the category a certain Woocommerce product is listed in.
I have Googled endlessly to find a solution. I have tried creating a template with the name "taxonomy-product_cat-[slugnamehere].php" and it didn't work. I have tried making making a template file, then, using if/else statements setting the template file in single_product.php. It isn't working either.
The category for my product is called 'box'. Below is the code from single_product; it isn't seeming to work. Any advice would be appreciated:
} else if ( is_product_category() ) {
if ( is_product_category( 'box' ) ) {
woocommerce_get_template_part( 'content', 'single-product-sample-box-4');
}
}
else {
woocommerce_get_template_part( 'content', 'single-product');
}
I want, if the category is 'box' is selected, for the content-single-product-sample-box-4 template to be displayed - instead only the content-single-product template is being displayed.
I found an answer! For anyone else struggling with this I will post my solution... I was only trying to change the template for ONE product (it was a sample box and therefore different from all other items).
Instead of this:
} else if ( is_product_category() ) {
if ( is_product_category( 'box' ) ) {
woocommerce_get_template_part( 'content', 'single-product-sample-box-4');
}
}
else {
woocommerce_get_template_part( 'content', 'single-product');
}
I did this:
} else if ( get_the_ID() == [PAGE ID GOES HERE] ) {
woocommerce_get_template_part( 'content', 'single-product-sample-box-4');
}
else {
woocommerce_get_template_part( 'content', 'single-product');
}

Categories