Site logo

Archived topic

Category template customising

21 replies · Started by ustar on August 18, 2019

Viewing posts 1–15 of 22

Hello, there!

I'd like to add some additional information to the WC category page.

In another theme I redefined the file theme_name/woocommerce/layouts/category.php in the child theme.
But there is no such template in the parent theme Generatepress. Also, it is not in the Woocommerce plugin files.

I'd like to add some code to display similar products on category pages with low count of products.

Wich template should i add to the Generatepress-child folder to add there my code?

Best regarsd, Sergey.

Hi, David!

Yes, maybe hook is it really good idea.
I'll try this today and let you know.

Thanks for advice!

Best Wishes, Sergey.

You're welcome

Hi, David!

I have tried to use hook, but i'm not sure that i use a right code around.
I see info where the hook should be shown, but there only code displaying.
So, i soppouse, hook is working, but not working code inside the hook.

Can i sent to you this code privately to have a look?

Regards, Sergey.

What type of code is it?
If it is PHP or a Shortcode then you need to check Enable PHP / Enable Shortcodes in the Hook Element.

Let us know.

I soppouse this is php code wich show additional product block with 9 products on woocommerce category page, when this current category contains less then 10 products.
Example, how it should be here:
https://u-star.cz/boty/fialova/

I have used this code for the hook:

<?php

if ( fl_woocommerce_version_check( '3.4.0' ) ? woocommerce_product_loop() : have_posts() ) {

/**
* Hook: woocommerce_before_shop_loop.
*
* @hooked wc_print_notices - 10
* @hooked woocommerce_result_count - 20 (FL removed)
* @hooked woocommerce_catalog_ordering - 30 (FL removed)
*/
do_action( 'woocommerce_before_shop_loop' );

woocommerce_product_loop_start();

if ( wc_get_loop_prop( 'total' ) ) {
while ( have_posts() ) {
the_post();

/**
* Hook: woocommerce_shop_loop.
*
* @hooked WC_Structured_Data::generate_product_data() - 10
*/
do_action( 'woocommerce_shop_loop' );

wc_get_template_part( 'content', 'product' );
}
}

woocommerce_product_loop_end();

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();
}
}

/**
* Hook: woocommerce_after_shop_loop.
*
* @hooked woocommerce_pagination - 10
*/
do_action( 'woocommerce_after_shop_loop' );
} else {
/**
* Hook: woocommerce_no_products_found.
*
* @hooked wc_no_products_found - 10
*/
do_action( 'woocommerce_no_products_found' );
}
?>

But, when i mark Execute PHP in settings of the hook, it braking the site.
Looks like i'm mistaken somewhere.

Regards, Sergey.

Hi there,

Where did you get this code, exactly? For example, where does the fl_woocommerce_version_check() function come from?

Hello, Tom.

This is part of code from category.php of the previews theme.
Can i send this file to you somehow privately?

Regards, Sergey.

I realised! This is from Flatsome theme.
It is costomised template category.php
How i can sand to you this file?

Regards, Sergey.

Is there a specific error you encounter when you try to use the template?

Hey, Tom!

I think hook works, because hook displaying an additional onfo, but function insode the hook wich i put inside, doesn't.

Because i see code like a text istead an additional product block:

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 '
Zbývající produkty našeho katalogu
'; 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 '
Zbývající produkty našeho katalogu
'; 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(); } } /** * Hook: woocommerce_after_shop_loop. * * @hooked woocommerce_pagination - 10 */ do_action( 'woocommerce_after_shop_loop' ); } else { /** * Hook: woocommerce_no_products_found. * * @hooked wc_no_products_found - 10 */ do_action( 'woocommerce_no_products_found' ); } ?>

And no errors in browser console.

Regards, Sergey.

And "Execute PHP" is checked? It's super difficult to debug all of that code in the forum, unfortunately.

So I’m using the support forum, isn’t it?
What to do?

regards, Sergey.

Hi Tom.

I seem to get excited and wrote not about that))

Of course, my question does not concern the quality of the theme. I just wanted to ask for advice on how best to deal with my situation.

Regards, Sergey.

This archived topic is closed to new replies.