Archived topic
Category template customising
21 replies · Started by ustar on August 18, 2019
It is, but that's code from another theme all bunched up into a massive wall of text. It's not possible to look through and debug without proper formatting.
We're happy to help with custom coding, but only if they're somewhat quick questions.
Perhaps it would be best to find an alternative block of code. That one looks massive and is likely over-complicated.
What parameters are we looking for? If there are less than x products, so x amount of products from another category?
Hi Tom!
Thank you very much for your complicity! I really appreciate your help.
This code is not even from another theme, it is a custom code that was added to the template
wp-content / themes / flatsome-child / woocommerce / layouts / category.php
The logic was laid down as follows:
1. check the number of products on the page
a) If there are more than or equal to 10, then nothing needs to be done.
b) If there are less than 10, then you need to create an additional block.
2. To create an additional block, the script must check the number of products on the page, which will have a URL one slash less (this can be a filter, or a subcategory).
a) If there are 9 or more products on this parent page, then they are displayed in an additional block.
b) If there are less than 9 products on this parent page, then the algorithm should check the page with an even shorter URL. And do a product quantity check.
An example of how this is done on Flatsome, can be seen on the live website:
Page
https://u-star.cz/boty/nizke/vans/damske/leto/cerna/39/
has only 1 product, so an additional block of goods is needed.
Checking the parent page
https://u-star.cz/boty/nizke/vans/damske/leto/cerna/
but, it has less than 10 products.
Checking the page
https://u-star.cz/boty/nizke/vans/damske/leto/
but, it has less than 10 products.
Checking the page
https://u-star.cz/boty/nizke/vans/damske/
There are 10 products on this page. Ok, we take any products and use for an additional block on the page
https://u-star.cz/boty/nizke/vans/damske/leto/cerna/39/
And for categories that do not have a parent page, for example, https://u-star.cz/modni-doplnky/
for an additional block, you can use products from the BOTS category, or any other category.
Best wishes, Sergey.
Ok, so maybe we can tweak that code a bit. Any chance you can share it in a more read-able format? Maybe via a GitHub Gist?: https://gist.github.com/
It needs to be formatted correctly for me to be able to read it.
Hi, Tom!
I have created repository on GitHub
You can find it by search user Sergiomak or here:
https://github.com/Sergiomak/Ustar/tree/category-dev
Let me know, if you'll have any problems, because i did it first time.
Thanks in advance!
Regards, Sergey.
Can you try this code in the hook?:
<div class="row category-page-row">
<div class="col large-9">
<?php
global $wp_query;
$globalTaxQuery = $wp_query->query_vars['tax_query'];
$isFilterUse = count($globalTaxQuery) > 2;
$productIds = array_map(function ($product)
{
return $product->ID;
}, $wp_query->posts);
if (is_tax('product_cat')) {
$queriedObject = get_queried_object();
if (wc_get_loop_prop( 'total' ) < 10 && $queriedObject->parent != 0) {
$args = [
'post_type' => 'product',
'posts_per_page' => 9,
'tax_query' => [
'relation' => 'AND',
[
'taxonomy' => 'product_cat',
'terms' => $queriedObject->parent,
],
[
'taxonomy' => 'product_cat',
'terms' => $queriedObject->term_id,
'operator' => 'NOT IN'
]
],
'meta_query' => [
[
'key' => '_stock_status',
'value' => 'instock'
]
]
];
if ($isFilterUse) {
unset($globalTaxQuery['relation']);
$args['tax_query'] = [
'relation' => 'AND',
[
'taxonomy' => 'product_cat',
'terms' => $queriedObject->term_id
]
];
$args['post__not_in'] = $productIds;
/*foreach ($globalTaxQuery as $key => $value) {
if ($value['taxonomy'] == 'product_visibility') {
continue;
}
$value['operator'] = 'NOT IN';
$args['tax_query'][] = $value;
}*/
}
$loop = new WP_Query( $args );
if ($isFilterUse && $loop->post_count < 9) {
$args = [
'post_type' => 'product',
'posts_per_page' => 9,
'tax_query' => [
'relation' => 'AND',
[
'taxonomy' => 'product_cat',
'terms' => $queriedObject->parent,
],
[
'taxonomy' => 'product_cat',
'terms' => $queriedObject->term_id,
'operator' => 'NOT IN'
]
],
'meta_query' => [
[
'key' => '_stock_status',
'value' => 'instock'
]
]
];
$loop = new WP_Query( $args );
}
if ($loop->post_count < 9) {
$currentCat = $queriedObject;
$prevCat = $queriedObject;
while ($loop->post_count < 9) {
if ($currentCat->parent == 0) {
$args = [
'post_type' => 'product',
'posts_per_page' => 9,
'tax_query' => [
'relation' => 'AND',
[
'taxonomy' => 'product_cat',
'field' => 'name',
'terms' => 'Boty'
]
],
'meta_query' => [
[
'key' => '_stock_status',
'value' => 'instock'
]
]
];
$loop = new WP_Query( $args );
break;
}
$currentCat = get_term($currentCat->parent, 'product_cat');
$args = [
'post_type' => 'product',
'posts_per_page' => 9,
'tax_query' => [
'relation' => 'AND',
[
'taxonomy' => 'product_cat',
'terms' => $currentCat->term_id,
],
[
'taxonomy' => 'product_cat',
'terms' => $prevCat->term_id,
'operator' => 'NOT IN'
]
],
'meta_query' => [
[
'key' => '_stock_status',
'value' => 'instock'
]
]
];
$loop = new WP_Query( $args );
$prevCat = $currentCat;
}
}
if ( $loop->have_posts() ) {
echo '<h2>Zbývající produkty našeho katalogu</h2>';
woocommerce_product_loop_start();
while ($loop->have_posts() ) {
$loop->the_post();
wc_get_template_part( 'content', 'product' );
}
woocommerce_product_loop_end();
} else {
//echo __( 'No products found' );
}
wp_reset_postdata();
} else if (wc_get_loop_prop( 'total' ) < 10) {
$args = [
'post_type' => 'product',
'posts_per_page' => 9,
'tax_query' => [
'relation' => 'AND',
[
'taxonomy' => 'product_cat',
'field' => 'name',
'terms' => 'Boty'
]
],
'meta_query' => [
[
'key' => '_stock_status',
'value' => 'instock'
]
]
];
if ($isFilterUse) {
unset($globalTaxQuery['relation']);
$args['tax_query'] = [
'relation' => 'AND',
[
'taxonomy' => 'product_cat',
'terms' => $queriedObject->term_id
]
];
$args['post__not_in'] = $productIds;
/*foreach ($globalTaxQuery as $key => $value) {
if ($value['taxonomy'] == 'product_visibility') {
continue;
}
$value['operator'] = 'NOT IN';
$args['tax_query'][] = $value;
}*/
}
$loop = new WP_Query( $args );
if ($isFilterUse && $loop->post_count < 9) {
$args = [
'post_type' => 'product',
'posts_per_page' => 9,
'tax_query' => [
'relation' => 'AND',
[
'taxonomy' => 'product_cat',
'field' => 'name',
'terms' => 'Boty'
]
],
'meta_query' => [
[
'key' => '_stock_status',
'value' => 'instock'
]
]
];
$loop = new WP_Query( $args );
}
if ( $loop->have_posts() ) {
echo '<h2>Zbývající produkty našeho katalogu</h2>';
woocommerce_product_loop_start();
while ($loop->have_posts() ) {
$loop->the_post();
wc_get_template_part( 'content', 'product' );
}
woocommerce_product_loop_end();
} else {
//echo __( 'No products found' );
}
wp_reset_postdata();
}
}
?>
</div>
</div>
Hi, Tom!
Wow! This is so cool! Works perfectlty!
You just a super! I didn't expected that code will work imidiately with 1-st run! )))
It's just amazing! Thank you so much!
I'll go to sleep happy ))
Exited,
Sergey.
Awesome! Glad I could help :)