In a recent Business Bloomer Club discussion, a WooCommerce user sought a dynamic solution for displaying product categories on product pages. They wanted to use the [product_categories]
shortcode to display subcategories but faced a challenge: the parent category ID (parent="3470"
) was not consistent across all products.
The goal was to automatically determine the parent category of the current product and display its subcategories without manually setting the parent ID each time. This approach is especially beneficial for stores with complex category hierarchies or a large number of products, where manual updates would be impractical.
By utilizing custom PHP code and WooCommerce functions, it’s possible to create a dynamic shortcode that adapts to each product’s category structure, enhancing both user experience and store management efficiency.
Understanding the Challenge
The standard [product_categories]
shortcode in WooCommerce allows you to display product categories by specifying parameters such as parent
, columns
, and others. However, when products belong to various categories, and you wish to display subcategories dynamically based on the current product’s parent category, a static parent ID isn’t sufficient.
In this scenario, the objective was to:
- Automatically identify the parent category of the current product.
- Use that parent category ID dynamically within the
[product_categories]
shortcode.
Creating a Dynamic Shortcode for WooCommerce Product Categories
To address this requirement, you can create a custom shortcode that retrieves the parent category of the current product and displays its subcategories. This involves crafting a PHP function that leverages WordPress and WooCommerce functions.
Step 1: Retrieve the Current Product’s Categories
First, obtain the categories assigned to the current product using the wp_get_post_terms()
function.
$product_id = get_the_ID();
$categories = wp_get_post_terms( $product_id, 'product_cat' );
This code fetches all categories associated with the current product.
Step 2: Identify the Primary Category
Assuming the first category in the array is the primary category, select it:
$primary_category = $categories[0];
$primary_category_id = $primary_category->term_id;
Step 3: Find the Parent Category
To obtain the parent category of the primary category, use the get_ancestors()
function:
$ancestors = get_ancestors( $primary_category_id, 'product_cat' );
$first_ancestor_id = $ancestors[0];
This code retrieves the ancestors of the primary category, and selects the immediate parent category ID.
Step 4: Create the Custom Shortcode
Now, create a custom shortcode that integrates everything and utilizes the [product_categories]
shortcode with the dynamic parent ID:
add_shortcode( 'current_product_categories', function() {
$product_id = get_the_ID();
$categories = wp_get_post_terms( $product_id, 'product_cat' );
$primary_category = $categories[0];
$primary_category_id = $primary_category->term_id;
$ancestors = get_ancestors( $primary_category_id, 'product_cat' );
$first_ancestor_id = $ancestors[0];
return do_shortcode( '[product_categories parent="' . $first_ancestor_id . '"]' );
});
This code defines a new shortcode [current_product_categories]
that you can place on your product pages. It dynamically determines the parent category ID and uses it to display the relevant subcategories.
Implementing the Solution
To apply this solution:
- Add the PHP Code: Insert the above code into your theme’s
functions.php
file or a custom plugin. - Use the New Shortcode: Replace instances of
[product_categories parent="3470" columns="5"]
with[current_product_categories]
on your product pages. - Customize Parameters: If needed, adjust the shortcode to include additional parameters like
columns
:
return do_shortcode( '[product_categories parent="' . $first_ancestor_id . '" columns="5"]' );
- Test the Shortcode: Verify that the shortcode correctly displays the subcategories on your product pages.
Considerations and Best Practices
- Category Structure: Ensure that your products are assigned to subcategories with parent categories. If a product is directly assigned to a top-level category, the ancestor array may be empty.
- Multiple Categories: If products belong to multiple categories, the code selects the first one. Modify the logic if you need to prioritize certain categories.
- Error Handling: Incorporate checks to handle cases where arrays might be empty to prevent PHP warnings:
if ( ! empty( $ancestors ) ) {
$first_ancestor_id = $ancestors[0];
} else {
$first_ancestor_id = $primary_category_id;
}
- Caching: For performance optimization, consider caching the results if your site experiences high traffic.
Conclusion
By creating a custom shortcode that dynamically retrieves the parent category of the current product, you can efficiently display relevant subcategories without manual intervention. This method leverages WooCommerce’s built-in functions, providing a flexible and scalable solution for managing category displays in your online store.