Year End Sale!!! - Save 50%

USE COUPON CODE:

DP2020BF

WooCommerce Product Category Archive


This document explains how to use the custom query option available in the Divi FilterGrid plugin to display the current product category results. We are also using the custom filters option to display any child product categories of the current product category and product tags as filters in the grid.

The first step is to add the Divi FilterGrid module to the product category archive pages. To do this, log into your WordPress site and go to Divi -> Theme Builder. Click on the Add New Template box and then select the All Product Category Pages option in the popup and then the Create Template button at the bottom of the popup.

Next click on the Add Custom Body box and then Build Custom Body in the next popup. You should now see the standard Divi Page Builder options that appear when creating a new page. Create the layout you want and add the Divi FilterGrid module to the layout.

Configure the Divi FilterGrid module however you want as usual. There are two options we need turn on. In the Query Options section, choose the Custom Query option under the Query Type dropdown. In the Filters Options, turn on the Use Custom Filters option. These two options are required to enable our custom query and custom filters.

Before we save our module, we need to give it a special CSS ID, which we’ll then use in our functions below to target this specific module. Go to the module’s Advanced tab and give the module the CSS ID dfg-product-category.

Next we need to save our module, save our template, go back to the Divi Theme Builder page, and click the Save Changes button in the top left corner. The Divi FilterGrid module will now load on every product category archive page. But we still need to tell the module which product and filters to load.

The last step is to add the custom query and filters functions and filters to your functions.php file located in your child theme.

Copy and paste the code below to your functions.php file:

// DFG CUSTOM QUERY - PRODUCT CATEGORY ARCHIVE PAGE
function dpdfg_custom_query($query_args, $props) {
    // GET THE ID OF THE CURRENT CATEGORY
    $wp_queried_object_id = $props['the_ID'];
    // ONLY USE THIS CUSTOM QUERY ON MODULES WITH ID "dfg-product-category"
    if (isset($props['module_id']) && $props['module_id'] == "dfg-product-category") {
        return array(
            'post_type' => 'product',
            'post_status' => 'publish',
            'posts_per_page' => 12,
            'tax_query' => array(
                array(
                    'taxonomy' => 'product_cat',
                    'terms' => array($wp_queried_object_id),
                    'field' => 'term_id',
                    'operation' => 'IN',
                    'include_child' => true
                )
            )
        );
    } else {
	// IF MODULE ID IS NOT "dfg-product-category", USE THE QUERY ARGUMENTS FROM THE MODULE SETTINGS
        return $query_args;
    }
}
add_filter('dpdfg_custom_query_args', 'dpdfg_custom_query', 10, 2);

// DFG CUSTOM FILTERS FILTER - PRODUCT CATEGORY ARCHIVE PAGE

function dpdfg_custom_filters_taxonomies($taxonomies, $props) {
	// ONLY USE THIS CUSTOM FILTER ON MODULES WITH ID "dfg-product-category"
	if (isset($props['module_id']) && $props['module_id'] == "dfg-product-category") {
    	return 'product_cat,product_tag';
	}
}

add_filter('dpdfg_custom_filters_taxonomies', 'dpdfg_custom_filters_taxonomies', 10, 2);

function dpdfg_custom_filters($filters, $props) {
    // GET THE ID OF THE CURRENT CATEGORY
    $wp_queried_object_id = $props['the_ID'];
    // ONLY USE THIS CUSTOM FILTER ON MODULES WITH ID "dfg-product-category"
    if (isset($props['module_id']) && $props['module_id'] == "dfg-product-category") {
        $custom_filters = array();
		// GET THE CHILD CATEGORIES OF THE CURRENT PRODUCT CATEGORY
		$children_terms = get_term_children($wp_queried_object_id, "product_cat");
		// GET ALL AVAILABLE PRODUCT TAGS
		$tags = get_terms('product_tag');
		// IF PRODUCT CATEGORY HAS CHILD CATEGORIES OR IF PRODUCT TAGS EXIST, DISPLAY THE CUSTOM FILTERS.
		if(!empty($children_terms) || !empty($tags)) {
			// ADD THE DEFAULT ALL FILTER WHICH DISPLAYS ALL RESULTS
			$custom_filters[] = array('id' => "all", 'name' => 'ALL');
			// IF PRODUCT CATEGORY HAS CHILD CATEGORIES, ADD EACH CHILD CATEGORY TO THE CUSTOM FILTERS ARRAY
			if(!empty($children_terms)) {
				foreach ($children_terms as $term) {
					$term_object = get_term($term);
					$custom_filters[] = array(
						'id' => $term,
						'name' => $term_object->name
					);
				}
			}
			// IF PRODUCT TAGS EXIST, ADD EACH TAG TO THE CUSTOM FILTERS ARRAY
			if(!empty($tags)) {
				foreach ($tags as $tag) {
					$custom_filters[] = array(
						'id' => $tag->term_id,
						'name' => $tag->name
					);
				}
			}
		}
        return $custom_filters;
    } else {
        return $filters;
    }
}
add_filter('dpdfg_custom_filters', 'dpdfg_custom_filters', 10, 2);

Pin It on Pinterest

Share This