0

so this is something I am trying to figure out for quite some time about my custom archive page for a custom taxonomy 'workplace'. There is many workplaces, and what i want is to display the products ( custom post type ) associated with the workplace by their categories, and also there descendents ( line 232 is an example of this. ) So for The 6 sections of categories, it finds each product category associated with each product and does not display the main category as it is already a tab ( it would be like a duplicate ). the code finds unique categories and then only displays them within their relevant sections.

all of this is working fine, until I tried to implement ajax load more.

the problem i am having, is that the page loop is initializing with the original product quantity/count, whereas I need it to only take account of the category count for each section, for pagination purposes for the categories. see, the product number is more than the category count since some products are from the same category.

I think what i need to do is count the categories outside of the page loop, ( or woocommerce loop) and then implement the page loop based on the findings. Should I somehow access the page queried object ( the workplace ) and find the information before the loop? I just can't seem to figure it out.

If anyone can throw some light on this it would be great.

<?php
/**
 * The Template for displaying product archives, including the main shop page which is a post type archive
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/archive-product.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see https://docs.woocommerce.com/document/template-structure/
 * @package WooCommerce\Templates
 * @version 3.4.0
 */
/**
 * THIS TEMPLATE IS FOR DISPLAYING WORKPLACES
 * IT FINDS PRODUCT CATEGORIES FROM EACH SECTION AND SHOWS THEM IF THEY ARE RELEVANT FOR THE WORKPLACE.
 * 
 */
defined('ABSPATH') || exit;

get_header('shop');

/**
 * Hook: woocommerce_before_main_content.
 *
 * @hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
 * @hooked woocommerce_breadcrumb - 20
 * @hooked WC_Structured_Data::generate_website_data() - 30
 */
do_action('woocommerce_before_main_content');

?>
<header class="woocommerce-products-header background_diamond">
    
    <style type="text/css">
    .major_cat_archive.custom_banner_style::before {
        <?php $term = get_queried_object();
// vars
$image = get_field('banner_image', $term);

if ($image)
{

?>background-image: url(<?php echo $image['url']; ?>);
            <?php
}

elseif (!$image)
{
?>background-image: url(<?php woocommerce_category_image();
}

?>);
    }
    </style>
    <?php if (apply_filters('woocommerce_show_page_title', true)): ?>
    <div class="major_cat_archive custom_banner_style example_layout_left">

        <h1 class="woocommerce-products-header__title page-title example_layout" data-aos="fade-right"
            data-aos-offset="-200" data-aos-delay="10" data-aos-duration="1000" data-aos-easing="ease"
            data-aos-mirror="true" data-aos-once="false" data-aos-anchor-placement="top-center">
            <?php woocommerce_page_title(); ?></h1>
    </div>
    <?php
endif; ?>
    <?php
/**
 * Hook: woocommerce_archive_description.
 *
 * @hooked woocommerce_taxonomy_archive_description - 10
 * @hooked woocommerce_product_archive_description - 10
 */
do_action('woocommerce_archive_description');
?>
</header>
<div class="holder " data-aos="fade-up" data-aos-offset="-200" data-aos-delay="10" data-aos-duration="1000"
    data-aos-easing="ease" data-aos-mirror="true" data-aos-once="false" data-aos-anchor-placement="top-center">
    <div class="sub_holder row content">
        <div class="col-12">
            <div class="intro_sec">
                <div class="sub_holder row">
                    <div class="col-lg-6 f-col">
                        <div class="first_col">
                            <h2>Our Value Promise</h2>
                            <div class="custom_content">
                                <?php $term = get_queried_object();
$custom_content = get_field('custom_content', $term);
echo $custom_content; ?>
                            </div>
                        </div>
                    </div>
                    <div class="col-lg-6">

<?php
                    $image = get_field('workplace_thumb', $term);

if ($image)
{
?>
                        <div class="second_col" style="background-image: url(<?php echo $image['url']; ?>);">

                        <?php
}

elseif (!$image)
{?>
                        <div class="second_col" style="background-image: url(<?php woocommerce_category_image();
}

?>);">
                            <div class="gradient_overlay"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<?php
if (woocommerce_product_loop())
{

    /**
     * Hook: woocommerce_before_shop_loop.
     *
     * @hooked woocommerce_output_all_notices - 10
     * @hooked woocommerce_result_count - 20
     * @hooked woocommerce_catalog_ordering - 30
     */
?>
<div class="container notices">
    <div class="row_notices">
        <?php /*notices output in separate div */
?>
        <div class="xoo-wsc-cart-trigger">Trigger Side Cart</div>
        <?php
    do_action('woocommerce_before_shop_loop'); ?>
    </div>
</div>
</div>
</div>
</div>


<div class="container cat_view">
    <div class="row"><?php
    woocommerce_product_loop_start();

    if (wc_get_loop_prop('total'))
    {

        while (have_posts())
        {

            the_post();
            $product_cats = wp_get_post_terms(get_the_ID() , 'product_cat');
            global $post;
            //Get the current workplace slug
            foreach ($product_cats as $collection)
            {
                
                $category_slugs[] = $collection->slug;          

            }          
        

            /**
             * Hook: woocommerce_shop_loop.
             */
            do_action('woocommerce_shop_loop');

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

        }

    }
    

    /*at this point, all relevant category slugs have been gathered and then sorted to remove duplicates.*/
?>
       
         <?php $category_slugs = array_unique($category_slugs);
  //  print_r($category_slugs); 
  
     

        
/**
 * INITIALISE EMPTY ARRAYS FOR PROCESSING.
 * ADD TO ARRAY IF SLUG AT $check IS A SIBLING OF RELEVANT CATEGORY.
 * IT ALSO WILL CHECK WITH || OR IF IT IS == TO RELEVANT CATEGORY.
 * THIS IS IN THE EVENT A PRODUCT IS ASSIGNED TO THE PARENT CATEGORY.
 * EACH ARRAY IS PLACED WITHIN IT'S RELEVANT TAB.
 * AN 'OTHER' TAB WILL ONLY APPEAR IF A CATEGORY IS ABSTRACT TO THE RELEVANT CATEGORIES.
 * THIS USES workplace_construct_categories(); & prod_cat_is_ancestor_of(); FUNCTIONS IN FUNCTIONS.PHP.
 */

 /*Initialise each relevant array */
    $array_of_window_cleaning = [];
    $array_of_hygiene_products = [];
    $array_of_catering_supplies = [];
    $array_of_hygiene_equipment = [];
    $array_of_workwear = [];
    $abstract_categories = [];
if( $category_slugs ){
    foreach ($category_slugs as $check)
    {
        $value = get_term_by('slug', $check, 'product_cat');
        $workwear_safety = get_term_by('slug', 'workwear-safety', 'product_cat');
        $hygiene_products = get_term_by('slug', 'hygiene-products', 'product_cat');
        $hygiene_equipment = get_term_by('slug', 'hygiene-equipment', 'product_cat');
        $catering_supplies = get_term_by('slug', 'catering-supplies', 'product_cat');
        $window_cleaning = get_term_by('slug', 'window-cleaning', 'product_cat');

        if (prod_cat_is_ancestor_of($workwear_safety, $value) || ($value->slug == $workwear_safety->slug))
        {
            if ($value->slug != $workwear_safety->slug)
                $array_of_workwear[] = $value->slug;
        }
    
        elseif (prod_cat_is_ancestor_of($hygiene_products, $value) || ($value->slug == $hygiene_products->slug))
        {
            
            if ($value->slug != $hygiene_products->slug)
                $array_of_hygiene_products[] = $value->slug;
                
        }
    
        elseif (prod_cat_is_ancestor_of($hygiene_equipment, $value) || ($value->slug == $hygiene_equipment->slug))
        {
            if ($value->slug != $hygiene_equipment->slug)
                $array_of_hygiene_equipment[] = $value->slug;
        }
    
        elseif (prod_cat_is_ancestor_of($catering_supplies, $value) || ($value->slug == $catering_supplies->slug))
        {
            if ($value->slug != $catering_supplies->slug)
                $array_of_catering_supplies[] = $value->slug;
        }
    
        elseif (prod_cat_is_ancestor_of($window_cleaning, $value) || ($value->slug == $window_cleaning->slug))
        {
            if ($value->slug != $window_cleaning->slug)
                $array_of_window_cleaning[] = $value->slug;
        }
    
        else
        {
            global $post;
            $terms = get_the_terms( $post->ID, 'product_cat' );      
            foreach ($terms  as $term  ) {
                $product_cat_id = $term->term_id;
                $product_cat_name = $term->slug;
                break;
            }
    
         
            if($value->slug != $product_cat_name)
                $abstract_categories[] = $value->slug;
            
        }
    }
    } 
?>




    </div>
</div>
</div>
</div>
</div>
<pre><?php
//print_r( $array_of_hygiene_products );
?></pre>
<!-- Display section of categories -->

<div class="container" data-aos="fade-up" data-aos-offset="-300" data-aos-delay="10" data-aos-duration="1000"
    data-aos-easing="ease" data-aos-mirror="true" data-aos-once="false" data-aos-anchor-placement="top-center">
    <div class="row">
        <h2 class="main_headings cat_titles"><?php woocommerce_page_title(); ?>: Relevant Categories </h2>
    </div>
</div> <?php
?>

<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <ul class="nav nav-tabs justify-content-center">
                <li class="active" data-aos="fade-up" data-aos-offset="-300" data-aos-delay="10"
                    data-aos-duration="1000" data-aos-easing="ease" data-aos-mirror="true" data-aos-once="true"
                    data-aos-anchor-placement="top-center"><a class="active" href="#tab1" data-bs-toggle="tab">Workwear
                        & Safety</a></li>
                <li data-aos="fade-up" data-aos-offset="-300" data-aos-delay="300" data-aos-duration="1000"
                    data-aos-easing="ease" data-aos-mirror="true" data-aos-once="true"
                    data-aos-anchor-placement="top-center"><a href="#tab2" data-bs-toggle="tab">Hygiene Products</a>
                </li>
                <li data-aos="fade-up" data-aos-offset="-300" data-aos-delay="600" data-aos-duration="1000"
                    data-aos-easing="ease" data-aos-mirror="true" data-aos-once="true"
                    data-aos-anchor-placement="top-center"><a href="#tab3" data-bs-toggle="tab">Hygiene Equipment</a>
                </li>
                <li data-aos="fade-up" data-aos-offset="-300" data-aos-delay="900" data-aos-duration="1000"
                    data-aos-easing="ease" data-aos-mirror="true" data-aos-once="true"
                    data-aos-anchor-placement="top-center"><a href="#tab4" data-bs-toggle="tab">Catering Supplies</a>
                </li>
                <li data-aos="fade-up" data-aos-offset="-300" data-aos-delay="1200" data-aos-duration="1000"
                    data-aos-easing="ease" data-aos-mirror="true" data-aos-once="true"
                    data-aos-anchor-placement="top-center"><a href="#tab5" data-bs-toggle="tab">Window Cleaning</a></li>
                <?php
    if (!empty($abstract_categories))
    {
?>
                <li class="tab6_zindex" data-aos="fade-up" data-aos-offset="-300" data-aos-delay="1200" data-aos-duration="1000"
                    data-aos-easing="ease" data-aos-mirror="true" data-aos-once="true"
                    data-aos-anchor-placement="top-center"><a href="#tab6" data-bs-toggle="tab">More</a></li>
                <?php
    } ?>
            </ul>
        </div>
    </div>
</div>
<div class="tab-content container">
    <div class="tab-pane active" id="tab1">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h4 class="panel-title">
                    <a data-bs-toggle="collapse" data-bs-parent=".tab-pane" href="#collapseOne">

                        Workwear & Safety
                    </a>
                </h4> 
            </div>
            <div id="collapseOne" class="panel-collapse collapse in">
                <div class="panel-body">
                    <?php $term = get_queried_object();
    $safteyworkwear = get_field('workwear-safety', $term);
    echo $safteyworkwear;

    //$newarray = [];
    if (!empty($array_of_workwear))
    {
       
        echo '<div class=" woocommerce columns-5"><ul class="products columns-5">';
        foreach ($array_of_workwear as $check)
        {
            $value = get_term_by('slug', $check, 'product_cat');
            workplace_construct_categories($value);
        }

    }
    if (!empty($array_of_workwear)) echo '</ul></div>';

?>
                </div>
            </div>
        </div>
    </div>
    <div class="tab-pane" id="tab2">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h4 class="panel-title">
                    <a data-bs-toggle="collapse" data-bs-parent=".tab-pane" href="#collapseTwo">
                        Hygiene Products
                    </a>
                </h4>
            </div>
            <div id="collapseTwo" class="panel-collapse collapse">
                <div class="panel-body">
                    <?php $hyg_products = get_field('hygiene_products', $term);
    echo $hyg_products;

    if (!empty($array_of_hygiene_products))
    {
       $array_of_hygiene_products = array_unique( $array_of_hygiene_products);
             
        echo '<div class=" woocommerce columns-5"><ul class="custom_load products columns-5">';
   
        foreach ($array_of_hygiene_products as $check)
        {
            $value = get_term_by('slug', $check, 'product_cat');           
            workplace_construct_categories($value);
           //wc_get_template_part( 'content', 'product_cat' );
        }

    }
    if (!empty($array_of_hygiene_products)) echo '</ul></div>';
?>

                </div>
            </div>
        </div>
    </div>
    <div class="tab-pane" id="tab3">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h4 class="panel-title">
                    <a data-bs-toggle="collapse" data-bs-parent=".tab-pane" href="#collapseThree">
                        Hygiene Equipment
                    </a>
                </h4>
            </div>
            <div id="collapseThree" class="panel-collapse collapse">
                <div class="panel-body">
                    <?php $hyg_equip = get_field('hygiene_equipment', $term);
    echo $hyg_equip;

    if (!empty($array_of_hygiene_equipment))
    {
        echo '<div class="woocommerce columns-5"><ul class="products columns-5">';
        foreach ($array_of_hygiene_equipment as $check)
        {
            $value = get_term_by('slug', $check, 'product_cat');
            workplace_construct_categories($value);
        }

    }
    if (!empty($array_of_hygiene_equipment)) echo '</ul></div>'; ?>
                </div>
            </div>
        </div>
    </div>
    <div class="tab-pane" id="tab4">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h4 class="panel-title">
                    <a data-bs-toggle="collapse" data-bs-parent=".tab-pane" href="#collapseFour">
                        Catering Supplies
                    </a>
                </h4>
            </div>
            <div id="collapseFour" class="panel-collapse collapse">
                <div class="panel-body">
                    <?php /*do_action( 'woocommerce_after_shop_loop' ); */ ?>
                    <?php $catering = get_field('catering_supplies', $term);
    echo $catering;

    if (!empty($array_of_catering_supplies))
    {
        echo '<div class=" woocommerce columns-5"><ul class="products columns-5">';
        foreach ($array_of_catering_supplies as $check)
        {
            $value = get_term_by('slug', $check, 'product_cat');
            workplace_construct_categories($value);
        }

    }
    if (!empty($array_of_catering_supplies)) echo '</ul></div>';
?>
                </div>
            </div>
        </div>
    </div>
    <div class="tab-pane" id="tab5">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h4 class="panel-title">
                    <a data-bs-toggle="collapse" data-bs-parent=".tab-pane" href="#collapseFive">
                        Window Cleaning
                    </a>
                </h4>
            </div>
            <div id="collapseFive" class="panel-collapse collapse">
                <div class="panel-body">
                    <?php /*do_action( 'woocommerce_after_shop_loop' ); */ ?>
                    <?php $win_cleaning = get_field('window_cleaning', $term);
    echo $win_cleaning;
    if (!empty($array_of_window_cleaning))
    {
        echo '<div class=" woocommerce columns-5"><ul class="products columns-5">';
        foreach ($array_of_window_cleaning as $check)
        {
            $value = get_term_by('slug', $check, 'product_cat');
            workplace_construct_categories($value);
        }

    }
    if (!empty($array_of_window_cleaning)) echo '</ul></div>';
?>
                </div>
            </div>
        </div>
    </div>
    <!--tab 5 close -->

    <?php
    if (!empty($abstract_categories))
    {
?>
    <div class="tab-pane" id="tab6">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h4 class="panel-title">
                    <a data-bs-toggle="collapse" data-bs-parent=".tab-pane" href="#collapseSix">
                        More
                    </a>
                </h4>
            </div>
            <div id="collapseSix" class="panel-collapse collapse">
                <div class="panel-body">
                    <?php /*do_action( 'woocommerce_after_shop_loop' ); */ ?>
                    <?php
                   
        echo '<div class=" woocommerce columns-5"><ul class="products columns-5">';
        foreach ($abstract_categories as $check)
        {
            $value = get_term_by('slug', $check, 'product_cat');
            workplace_construct_categories($value);
        }

        echo '</ul></div>';
?>
                </div><!-- close -->
            </div><!-- close -->
        </div><!-- close -->

     
    </div><!-- more div close -->


    
</div><!-- close -->







</div><!-- close -->
<?php
    }
    else
    {
        echo '</div>';
    } ?>

<!--end of display section -->
<?php
echo '<pre>';
    print_r( $array_of_hygiene_products);
    echo '</pre>';?>

<?php
    woocommerce_product_loop_end();
    /**
     * 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');
}
/**
 * Hook: woocommerce_after_main_content.
 *
 * @hooked woocommerce_output_content_wrapper_end - 10 (outputs closing divs for the content)
 */
do_action('woocommerce_after_main_content');
/**
 * Hook: woocommerce_sidebar.
 *
 * @hooked woocommerce_get_sidebar - 10
 */
do_action('woocommerce_sidebar');
get_footer('shop');

My function you will see within the separate tabs to construct categories:

/*For use on WORKPLACE TAX PAGE*/
function workplace_construct_categories($value){

    global $wp_query;
    $wp_slug = $wp_query->query_vars['workplaces'];   
    $cat_slug = $value->slug;                        
    $cat_name = $value->name;
    echo '<li class="product-category product brand_cats"><div class="banner-box"><div class="banner-box__image"> 
    <a href="' . esc_url ( get_bloginfo( 'url' ) ) . '/'. 'product-category'. '/' . esc_html($cat_slug) . '?workplaces=' . esc_html($wp_slug) . '"';
    echo the_subcategory_thumbnail( $value);          
    echo '</a></div><div class="banner-box__info"><a href="' . get_bloginfo( 'url' ) . '/'. 'product-category'. 
    '/' . esc_html($cat_slug) . '?workplaces=' . esc_html($wp_slug) . '/' . '">' . '<h2 itemprop="name" class="woocommerce-loop-category__title"><span>' . 
    esc_html($cat_name) . '</a></span></h2></li>'; 
}

1 Answer 1

0

I solved my problem with:

https://stackoverflow.com/questions/61622017/get-woocommerce-product-categories-that-contain-a-specific-product-brand/61624358#61624358

turns out wp_query wasn't the right choice for my requirements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.