0

I'm wanting to display products for a specific category with products organized by their subcategory. I'm also using FacetWP to filter those products, I've got the products sorted like I want but I'm unable to get FacetWP to filter those products. Any assistance would be greatly appreciated!

archive-product-track.php

<?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
 */

defined( 'ABSPATH' ) || exit;

/**
 * Add 'no-padding to '.content-area'
 *
 * @param string $open, opening markup
 * @param array $args, markup args
 * @return string
 */
function cw_class_content_area_wrap( $attributes ) {
    $attributes['class'] .= ' no-padding';
    return $attributes;
}
add_filter( 'genesis_attr_content-sidebar-wrap', 'cw_class_content_area_wrap' );

remove_action( 'genesis_loop', 'genesis_do_loop' );
remove_action( 'genesis_before_loop', 'genesis_do_taxonomy_title_description', 15 );
remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20 );
remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10 );
remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10 );
remove_action( 'genesis_sidebar', 'genesis_do_sidebar' );

/**
 * Output Shop sidebar.
 *
 */
function cw_shop_sidebar() {
    dynamic_sidebar( 'shop-sidebar' );
}

/**
 * Add Shop sidebar.
 *
 */
function cw_change_shop_sidebar() {
    add_action( 'genesis_sidebar', 'cw_shop_sidebar' );
}
add_action( 'genesis_sidebar', 'cw_shop_sidebar' );

add_filter( 'genesis_pre_get_option_site_layout', 'genesiswooc_archive_layout' );
/**
 * Manage page layout for the Product archive (Shop) page.
 *
 * Set the layout in the Genesis layouts metabox in the Page Editor.
 *
 * @since 0.9.0
 *
 * @param string $layout Current Genesis page layout, such as 'content-sidebar'.
 *
 * @return string Page layout if set for the shop page, otherwise the default site layout.
 */
function genesiswooc_archive_layout( $layout ) {

    $post_layout = get_post_meta( wc_get_page_id( 'shop' ), '_genesis_layout', true );

    if ( ! $post_layout || 'default_layout' === $post_layout ) {
        return $layout;
    }

    return $post_layout;

}

add_action( 'genesis_before_loop', 'genesiswooc_archive_product_loop' );
/**
 * Display shop items (product custom post archive)
 *
 * This function has been refactored in 0.9.4 to provide compatibility with both WooCommerce 1.6.0
 * and backwards compatibility with older versions.
 *
 * This is needed thanks to substantial changes to WooCommerce template contents introduced in
 * WooCommerce 1.6.0.
 *
 * @uses genesiswooc_content_product() if WooCommerce is version 1.6.0+
 * @uses genesiswooc_product_archive() for earlier WooCommerce versions
 *
 * @since 0.9.0
 *
 * @global WooCommerce $woocommerce Current WooCommerce instance.
 */
function genesiswooc_archive_product_loop() {

    /**
     * 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' );

    do_action( 'woocommerce_archive_description' );

    if ( have_posts() ) {
        /**
         * Hook: woocommerce_before_shop_loop.
         *
         * @hooked woocommerce_output_all_notices - 10
         * @hooked woocommerce_result_count - 20
         * @hooked woocommerce_catalog_ordering - 30
         */
        do_action( 'woocommerce_before_shop_loop' );

        woocommerce_product_loop_start();

        /* Category - SubCategory START */
        $term           = get_queried_object();
        $parent_id      = empty( $term->term_id ) ? 0 : $term->term_id;
        $product_categories = get_categories( array( 'taxonomy' => 'product_cat', 'child_of' => $parent_id) );
        if(empty($product_categories)) {
            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();
        } else {
            foreach ($product_categories as $product_category) { ?>
                <div class="track-category">
                    <div class="category-name">
                        <h2><?php echo esc_html($product_category->name); ?></h2>
                    </div>

                   <?php woocommerce_product_loop_start(); //open ul
                    $args = array(
                        'posts_per_page' => 2,
                        'facetwp' => true,
                        'tax_query' => array(
                            'relation' => 'AND',
                            array(
                                'taxonomy' => 'product_cat',
                                'field' => 'slug',
                                'terms' => $product_category->slug
                            ),
                        ),
                        'post_type' => 'product',
                        'orderby' => 'menu_order',
                        'order' => 'asc',
                    );
                    $query = new WP_Query( $args );
                    while ( $query->have_posts() ) : $query->the_post();
                        global $product;

                        $link = apply_filters( 'woocommerce_loop_product_link', get_the_permalink(), $product ); ?>

                        <div <?php wc_product_class( '', $product ); ?>>
                            <div class="product--meta">
                                <a class="product--meta__link" href="<?php echo $link; ?>">
                                    <div class="product--meta__image"><?php woocommerce_template_loop_product_thumbnail(); ?></div>
                                    <div class="product--meta__title"><?php woocommerce_template_loop_product_title(); ?></div>
                                </a>
                            </div>
                            <div class="product--action">
                                <div class="product--action__price"><?php woocommerce_template_loop_price(); ?></div>
                                <div class="product--action__button"><?php woocommerce_template_loop_add_to_cart(); ?></div>
                            </div>
                        </div>

                    <?php
                endwhile; // end of the loop.
                wp_reset_postdata();
                woocommerce_product_loop_end(); //close ul
            }//foreach
            echo '</div>';
        }

        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' );

}

genesis();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.