2 Layouts for a WooCommerce shop - php

I have to categories named "Collectie" and "Shop", what I want is different layouts for the children of both categories.
I already tried this with the template_include function like this:
function lab5_template_include( $template ) {
if ( category_has_children('collectie')) {
$template = get_stylesheet_directory() . '/woocommerce/archive-product-collectie.php';
}elseif ( is_product_category('shop')) {
$template = get_stylesheet_directory() . '/woocommerce/archive-product-shop.php';
}
return $template;
}
How do I do that?
EDIT
I solved it with the solution from https://wordpress.org/support/topic/different-page-layouts-for-category-vs-subcategory
i added the next lines to taxonomy-product_cat.php
// We need to get the top-level category so we know which template to load.
$get_cat = $wp_query->query['product_cat'];
// Split
$all_the_cats = explode('/', $get_cat);
// How many categories are there?
$cat_count = count($all_the_cats);
//
// All the cats say meow!
//
// Define the parent
$parent_cat = $all_the_cats[0];
// Collectie
if ( $parent_cat == 'collectie' ) woocommerce_get_template( 'archive-product-collectie.php' );
// Shop
elseif ( $parent_cat == 'shop' ) woocommerce_get_template( 'archive-product.php' );

I think you'd be better off running a while loop to determine the parent level category name. Then you can match it against the different parent categories you want to display differently.
edit lab5_template_include() should use is_product_category() instead of is_product_taxonomy() which returns true on product category and tag archives.
function lab5_template_include( $template ) {
if( is_product_category()){
$slug = get_query_var('product_cat');
$cat = get_term_by( 'slug', $slug, 'product_cat' );
$catParent = so_top_product_category_slug($cat);
// locate the new templates
if ( 'collectie' == $catParent ) {
$new_template = locate_template( array( 'woocommerce/archive-product-collectie.php' ) );
} elseif ( 'shop' == $catParent ) ) {
$new_template = locate_template( array( 'woocommerce/archive-product-shop.php' ) );
}
// set the new template if found
if ( '' != $new_template ) {
$template = $new_template ;
}
}
return $template;
}
add_filter('template_include', 'lab5_template_include', 20 );
edit bug fixes and improved efficiency of so_top_product_category_slug(). Now tested and working.
// get the top-level parent product category from a term object or term slug
function so_top_product_category_slug($cat) {
if( is_object( $cat ) ){
$catid = $cat->term_id;
} else {
$cat = get_term_by( 'slug', $cat, 'product_cat' );
$catid = $cat->term_id;
}
$parentId = $cat->parent;
$parentSlug = $cat->slug;
while ($parentId > 0) {
$cat = get_term_by( 'id', $parentId, 'product_cat' );
$parentId = $cat->parent; // assign parent ID (if exists) to $catid
$parentSlug = $cat->slug;
}
return $parentSlug;
}

Related

Why can't I add a second term to my function (add taxonomy to body class)

I need to see the categories in the body to call certain CSS. I tried to add a second category term to the allready excisting working code. But it won't work for the newly added category (portfolio_category). What am I doing wrong?
//add taxonomy to body class
add_filter( 'body_class', 'themeprefix_add_taxonomy_class' );
// Add taxonomy terms name to body class
function themeprefix_add_taxonomy_class( $classes ){
if( is_singular() ) {
global $post;
$taxonomy_terms = get_the_terms($post->ID, 'arbocategory', 'portfolio_category' );
if ( $taxonomy_terms ) {
foreach ( $taxonomy_terms as $taxonomy_term ) {
$classes[] = 'tax_' . $taxonomy_term->slug;
}
}
}
return $classes;
}
https://developer.wordpress.org/reference/functions/get_the_terms/
get_the_terms() accepst two paramters $post, $taxonomy, you can't just dump multiple taconomies to the function and expect it to work. you need to run get_the_terms() twice.
function themeprefix_add_taxonomy_class( $classes ){
if( is_singular() ) {
global $post;
$terms = array('arbocategory', 'portfolio_category');
foreach ($terms as $t) {
$taxonomy_terms = get_the_terms($post->ID, $t );
if (is_array($taxonomy_terms) && count($taxonomy_terms)>0) {
foreach ( $taxonomy_terms as $taxonomy_term ) {
$classes[] = 'tax_' . $taxonomy_term->slug;
}
}
}
}
return $classes;
}

Change WooCommerce product description tab button and title

I want to change the WooCommerce product description tab button and title "Description" into "Tracklist" when the page has a body class of "parent-product_cat-vinyl".
Here is my code so far:
<?php
if ( is_singular() ) {
$classes = get_body_class();
if (in_array('parent-product_cat-vinyl',$classes)) {
add_filter( 'woocommerce_product_description_tab_title','ps_rename_description_product_tab_label');
function ps_rename_description_product_tab_label() {
return 'Tracklist';
}
}
}
But it doesn't seem to work.
You can use the following composite filter hooks (where $tab_key is in your case description):
woocommerce_product_{$tab_key}_tab_title
woocommerce_product_{$tab_key}_heading
It can be done in 2 ways:
Conditionally with a defined body class:
add_filter( 'woocommerce_product_description_tab_title', 'change_product_description_tab_text' );
add_filter( 'woocommerce_product_description_heading', 'change_product_description_tab_text' );
function change_product_description_tab_text( $title ) {
global $product;
if( in_array( 'parent-product_cat-vinyl', get_body_class() ) ) {
return __('Tracklist', 'woocommerce');
}
return $title;
}
Or conditionally for a product category (including parent product categories):
// Custom conditional function that handle parent product categories too
function has_product_categories( $categories, $product_id = 0 ) {
$parent_term_ids = $categories_ids = array(); // Initializing
$taxonomy = 'product_cat';
$product_id = $product_id == 0 ? get_the_id() : $product_id;
if( is_string( $categories ) ) {
$categories = (array) $categories; // Convert string to array
}
// Convert categories term names and slugs to categories term ids
foreach ( $categories as $category ){
$result = (array) term_exists( $category, $taxonomy );
if ( ! empty( $result ) ) {
$categories_ids[] = reset($result);
}
}
// Loop through the current product category terms to get only parent main category term
foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
if( $term->parent > 0 ){
$parent_term_ids[] = $term->parent; // Set the parent product category
$parent_term_ids[] = $term->term_id; // (and the child)
} else {
$parent_term_ids[] = $term->term_id; // It is the Main category term and we set it.
}
}
return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true : false;
}
add_filter( 'woocommerce_product_description_tab_title', 'change_product_description_tab_text' );
add_filter( 'woocommerce_product_description_heading', 'change_product_description_tab_text' );
function change_product_description_tab_text( $title ) {
global $product;
// Here set in the array the targeted product categories
$categories = array('Vinyl');
if ( has_product_categories( $categories, $product->get_id() ) ) {
return __('Tracklist', 'woocommerce');
}
return $title;
}
Code goes in functions.php file of your active child theme (or active theme) . Tested and works.

WordPress child, grandchild, great grandchild categories must use parent category template

I have created a template named category-south-africa.php. I would like all child, grandchild, great grandchild categories to automatically use this template. For example, all these categories must use the category-south-africa.php template:
/category/south-africa/
/category/south-africa/stellenbosch/
/category/south-africa/stellenbosch/wine-farm/
/category/south-africa/stellenbosch/wine-farm/wine-varietal/
Does anyone perhaps have a php function solution?
You can create you own template for this. You can check the category_template filter and do something like this.
add_filter('category_template', 'add_top_term_template');
function add_top_term_template($template) {
$term = get_queried_object();
$parent = $term->parent;
if($parent === 0) {
return $template;
}
$topLevel = null;
while($parent !== 0) {
$topLevel = get_term($parent);
$parent = $topLevel->parent;
}
$templates = ["term-top-level-{$topLevel->slug}.php"];
return locate_template($templates);
}
You can find more info about the filter here: https://codex.wordpress.org/Plugin_API/Filter_Reference/category_template
get_ancestors() that returns an array containing the parents of any given category.
use like get_ancestors( child_category_id, 'category' );
Check the value is in the array or not. if yes then define template name.
You can use this function to check if the current category is a child category and set the template to the same you have for the parent category. Put this in the functions.php of your theme.
function childcategory_template( $template ) {
$cat = get_queried_object();
if( 0 < $cat->category_parent )
$template = locate_template( 'category-south-africa.php');
return $template;
}
add_filter( 'category_template', 'childcategory_template' );
Edit after comment: You can also check the name (slug) and set the $template variable taking the name (slug) of the parent into account.
function childcategory_template( $template ) {
$cat = get_queried_object();
if( 0 < $cat->category_parent )
$parent_id = $cat->category_parent;
if ( get_category( $parent_id )->slug == "south-africa" ) {
$template = locate_template( 'category-south-africa.php');
} else if ( get_category( $parent_id )->slug == "north-africa" ) {
$template = locate_template( 'category-north-africa.php');
}
return $template;
}
add_filter( 'category_template', 'childcategory_template' );

How to display taxonomy parent for selected taxonomy

I want to show on the site the parent of the selected taxonomy. For that I have the above code but the problem with this is that it is showing all the selected taxonomy in a list with now structure.
global $post;
$features = get_the_terms( $post->ID, 'property-feature' );
if ( !empty( $features ) && is_array( $features ) && !is_wp_error( $features ) ) {
?>
<div class="property-features">
<?php
global $inspiry_options;
$property_features_title = $inspiry_options[ 'inspiry_property_features_title' ];
if( !empty( $property_features_title ) ) {
?><h4 class="fancy-title"><?php echo esc_html( $property_features_title ); ?></h4><?php
}
?>
<ul class="property-features-list clearfix">
<?php
foreach( $parent_term as $single_feature ) {
echo '<li>'. $single_feature->name . '</li>';
}
?>
</ul>
</div>
<?php
}
Example:
Parent: option 1, option 2
Parent 2 : option 3, option 4
If I select option 2 and option 3 I want to see
Parent: option 2
Parent 2 : option 3
Updare 2
global $post;
$features = get_the_terms( $post->ID, 'property-feature' );
$featuresss = get_the_terms( $post->ID, 'property-feature' );
// determine the topmost parent of a term
function get_term_top_most_parent($term_id, $taxonomy){
// start from the current term
$parent = get_term_by( 'id', $term_id, $taxonomy);
// climb up the hierarchy until we reach a term with parent = '0'
while ($parent->parent != '0'){
$term_id = $parent->parent;
$parent = get_term_by( 'id', $term_id, $taxonomy);
}
return $parent;
}
// so once you have this function you can just loop over the results returned by wp_get_object_terms
function hey_top_parents($taxonomy, $results = 1) {
// get terms for current post
$terms = wp_get_object_terms( get_the_ID(), $taxonomy );
$y = get_the_terms($terms->term_id, $taxonomy);
// set vars
$top_parent_terms = array();
foreach ( $terms as $term ) {
//get top level parent
$top_parent = get_term_top_most_parent( $term->term_id, $taxonomy );
//check if you have it in your array to only add it once
if ( !in_array( $top_parent, $top_parent_terms ) ) {
$top_parent_terms[] = $top_parent;
}
}
// build output (the HTML is up to you)
foreach( $top_parent_terms as $single_feature ) {
echo '<li>'. $single_feature->name . '</li>';
foreach( $terms as $single ) {
echo '<ul><li>'. $single->name . '</li></ul>';
}
}
//return $top_parent_terms;
}
I managed to display the top parent for the selected taxonomy but now the problem is that I need now to display in the top parent anly the selected taxonomy from that parent
global $post;
$taxonomy = "property-feature";
// determine the topmost parent of a term
function get_term_top_most_parent($term_id, $taxonomy){
// start from the current term
$parent = get_term_by( 'id', $term_id, $taxonomy);
// climb up the hierarchy until we reach a term with parent = '0'
while ($parent->parent != '0'){
$term_id = $parent->parent;
$parent = get_term_by( 'id', $term_id, $taxonomy);
}
return $parent;
}
// so once you have this function you can just loop over the results returned by wp_get_object_terms
function hey_top_parents($taxonomy, $results = 1) {
// get terms for current post
$terms = wp_get_object_terms( get_the_ID(), $taxonomy );
// set vars
$top_parent_terms = array();
foreach ( $terms as $term ) {
//get top level parent
$top_parent = get_term_top_most_parent( $term->term_id, $taxonomy );
//check if you have it in your array to only add it once
if ( !in_array( $top_parent, $top_parent_terms ) ) {
$top_parent_terms[] = $top_parent;
}
}
// build output (the HTML is up to you)
foreach( $top_parent_terms as $single_feature )
{
echo '<li>'. $single_feature->name . '</li>';
foreach( $terms as $single ) {
if( $single_feature->term_id == $single->parent ) {
echo '<ul><li>'. $single->name . '</li></ul>';
}
}
}
//return $top_parent_terms;
}
CHANGES:
1) Moved taxonomy name to variable (used in echo's).
$taxonomy = "property-feature";
2) Added if condition to the code responsible for displaying sub terms.
if( $single_feature->term_id == $single->parent ) {
echo '<ul><li>'. $single->name . '</li></ul>';
}
REMOVED (not used lines of codes):
1)
$features = get_the_terms( $post->ID, 'property-feature' );
$featuresss = get_the_terms( $post->ID, 'property-feature' );
2)
$y = get_the_terms($terms->term_id, $taxonomy);
CONSIDER:
1) Removing $results variable from:
function hey_top_parents($taxonomy, $results = 1) {
it is not used anywhere or maybe should be to determine if results should be returned or displayed by the function.

If has_term, load other Wordpress theme

This is what I want to accomplish. In Wordpress I've created a taxonomy called categorie with the terms app, web and branding. When a project has the term app, I want to load another theme / blog. When a project has the term web or branding, I want to load single.php. The last one works just fine.
This is my code so far
function load_single_template($template) {
$new_template = '';
if( is_single() ) {
global $post;
if( has_term('app', 'categorie', $post) ) {
$new_template = get_theme_roots('themeApp');
} else {
$new_template = locate_template(array('single.php' ));
}
}
return ('' != $new_template) ? $new_template : $template;
}
add_action('template_include', 'load_single_template');
So when a project has the term app, I want to load the theme themeApp. Any suggestions? Thanks in advance.
We had to accomplish a similar task in our plugin, AppPresser. You can see our solution here: https://github.com/WebDevStudios/AppPresser/blob/master/inc/theme-switcher.php
Basically, you need to change the theme name in 3 filters: 'template', 'option_template', 'option_stylesheet'.
Getting the category is not so simple though, because the template check happens early enough in the WordPress process that the global $post and $wp_query objects are not available.
Here is one way that can be accomplished:
<?php
add_action( 'setup_theme', 'maybe_theme_switch', 10000 );
function maybe_theme_switch() {
// Not on admin
if ( is_admin() )
return;
$taxonomy = 'category';
$term_slug_to_check = 'uncategorized';
$post_type = 'post';
// This is one way to check if we're on a category archive page
if ( false !== stripos( $_SERVER['REQUEST_URI'], $taxonomy ) ) {
// Remove the taxonomy and directory slashes and it SHOULD leave us with just the term slug
$term_slug = str_ireplace( array( '/', $taxonomy ), '', $_SERVER['REQUEST_URI'] );
// If the term slug matches the one we're checking, do our switch
if ( $term_slug == $term_slug_to_check ) {
return yes_do_theme_switch();
}
}
// Try to get post slug from the URL since the global $post object isn't available this early
$post = get_page_by_path( $_SERVER['REQUEST_URI'], OBJECT, $post_type );
if ( ! $post )
return;
// Get the post's categories
$cats = get_the_terms( $post, $taxonomy );
if ( ! $cats )
return;
// filter out just the category slugs
$term_slugs = wp_list_pluck( $cats, 'slug' );
if ( ! $term_slugs )
return;
// Check if our category to check is there
$is_app_category = in_array( $term_slug_to_check, $term_slugs );
if ( ! $is_app_category )
return;
yes_do_theme_switch();
}
function yes_do_theme_switch( $template ) {
// Ok, switch the current theme.
add_filter( 'template', 'switch_to_my_app_theme' );
add_filter( 'option_template', 'switch_to_my_app_theme' );
add_filter( 'option_stylesheet', 'switch_to_my_app_theme' );
}
function switch_to_my_app_theme( $template ) {
// Your theme slug
$template = 'your-app-theme';
return $template;
}

Categories