[Resolved] Category template customising

Home Forums Support Category template customising

  • This topic has 21 replies, 3 voices, and was last updated 1 year ago by Tom.
Viewing 15 posts - 1 through 15 (of 22 total)
  • Author
    Posts
  • #987128
    ustar

    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.

    #987323
    David
    Staff
    Customer Support

    Hi there,

    the woo templates can be found here, there is no longer a specific category template just the archive-product template:

    https://github.com/woocommerce/woocommerce/tree/master/templates

    You may want to look at using Hooks to add your custom content instead of rewriting your own template.

    #987335
    ustar

    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.

    #987339
    David
    Staff
    Customer Support
    #988739
    ustar

    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.

    #988789
    David
    Staff
    Customer Support

    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.

    #988815
    ustar

    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.

    #989293
    Tom
    Lead Developer
    Lead Developer

    Hi there,

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

    #989367
    ustar

    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.

    #989464
    ustar

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

    Regards, Sergey.

    #989891
    Tom
    Lead Developer
    Lead Developer

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

    #989942
    ustar

    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.

    #990154
    Tom
    Lead Developer
    Lead Developer

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

    #991501
    ustar

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

    regards, Sergey.

    #991504
    ustar

    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.

Viewing 15 posts - 1 through 15 (of 22 total)
  • You must be logged in to reply to this topic.