WordPress / 3 MIN READ
Woo Create Categories PHP 202
Creating WooCommerce categories with PHP
From the original Fervor library. Examples may use older package versions.
Tutorial: Assign Existing WooCommerce Categories to a New Parent Category Programmatically
If you already have categories in WooCommerce and want to group them under a newly created parent category programmatically, this guide is for you. We’ll create a parent category and then reassign existing categories to it as children.
Overview
- Create a new parent category.
- Reassign existing categories as children to this new parent category.
- Verify the updated category hierarchy.
Step 1: Create a New Parent Category
We’ll use wp_insert_term() to create a new parent category.
Code Example: Creating the Parent Category
function create_new_parent_category() {
// Check if the parent category already exists
if (!term_exists('Footwear', 'product_cat')) {
wp_insert_term(
'Footwear', // Category Name
'product_cat', // Taxonomy
array(
'description' => 'A collection of all types of footwear.',
'slug' => 'footwear', // URL-friendly slug
'parent' => 0, // 0 indicates this is a top-level category
)
);
}
}
add_action('init', 'create_new_parent_category');
What’s Happening Here
term_exists()checks if the category already exists to avoid duplicates.parent => 0ensures this is a top-level category.
After running this, you’ll have a new parent category called Footwear.
Step 2: Reassign Existing Categories to the New Parent
We’ll now fetch the IDs of existing categories and set their parent to the new category.
Code Example: Assign Existing Categories to the New Parent
function assign_categories_to_parent() {
// Get the parent category ID
$parent_category = get_term_by('slug', 'footwear', 'product_cat');
if ($parent_category) {
// List of existing category slugs to assign
$child_category_slugs = array('shoes', 'sandals', 'boots');
foreach ($child_category_slugs as $slug) {
$category = get_term_by('slug', $slug, 'product_cat');
if ($category) {
wp_update_term(
$category->term_id, // ID of the category to update
'product_cat', // Taxonomy
array(
'parent' => $parent_category->term_id, // Set the new parent
)
);
}
}
}
}
add_action('init', 'assign_categories_to_parent');
What’s Happening Here
get_term_by()retrieves the new parent category’s ID.wp_update_term()updates each child category’sparentto the new parent category’s ID.- Replace the
child_category_slugsarray with the slugs of your existing categories.
Step 3: Verify the Hierarchy
After running the code:
- Go to Products > Categories in the WordPress admin.
- Check that the selected categories (e.g., Shoes, Sandals, Boots) are now subcategories of the new parent category (e.g., Footwear).
Optional: Display the Updated Category Hierarchy
To confirm everything is working on the front end, you can display the hierarchy dynamically.
Code Example: Display Updated Hierarchy
function display_updated_category_hierarchy() {
$args = array(
'taxonomy' => 'product_cat',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false,
);
$categories = get_terms($args);
if (!empty($categories)) {
echo '<ul class="product-categories">';
foreach ($categories as $category) {
if ($category->parent == 0) {
// Display top-level categories
echo '<li>' . esc_html($category->name);
// Fetch child categories
$child_args = array(
'taxonomy' => 'product_cat',
'parent' => $category->term_id,
'hide_empty' => false,
);
$child_categories = get_terms($child_args);
if (!empty($child_categories)) {
echo '<ul>';
foreach ($child_categories as $child) {
echo '<li>' . esc_html($child->name) . '</li>';
}
echo '</ul>';
}
echo '</li>';
}
}
echo '</ul>';
}
}
add_action('woocommerce_before_shop_loop', 'display_updated_category_hierarchy');
What’s Happening Here
- Top-level categories are fetched and displayed first.
- Child categories are retrieved and nested within their parent category.
Testing the Code
- Activate the Code: Save changes to your theme’s
functions.phpfile or custom plugin. - Visit the Categories Page:
- Verify the hierarchy under Products > Categories.
- View the Store:
- Check the front-end display (if you’ve added the display code).
Conclusion
You’ve now successfully created a new parent category and assigned existing categories as children programmatically. This approach is perfect for automating category organization, especially for large catalogs.
Need help fine-tuning the process or troubleshooting? Let me know! 🚀