Automate WooCommerce Product Categorization with a Custom Plugin
Managing an online store can be time-consuming, especially when it comes to organizing products into the right categories. If you’re running a WooCommerce store and want to automate the process of categorizing products based on their titles, you’re in the right place. In this blog post, we’ll walk you through creating a custom WordPress plugin that assigns products to categories dynamically.
Why Automate Product Categorization?
Automatically categorizing products can save you a significant amount of time, reduce human error, and ensure your products are always organized correctly. This is particularly useful if your product titles contain keywords that correspond to your category names.
Step-by-Step Guide to Creating the Plugin
Step 1: Create the Plugin File
First, create a new PHP file called product-categorization.php
and add the following code:
<?php
/**
* Plugin Name: Product Categorization
* Description: Automatically assigns products to categories based on keywords in product titles.
* Version: 1.0
* Author: Your Name
*/
if (!defined(‘ABSPATH’)) {
exit; // Exit if accessed directly
}
// Function to categorize products
function categorize_products() {
// Get all categories
$categories = get_terms(array(
‘taxonomy’ => ‘product_cat’,
‘hide_empty’ => false,
));
// Create an associative array of categories and keywords
$categories_keywords = array();
foreach ($categories as $category) {
$categories_keywords[$category->term_id] = $category->name; // Store category id and name
}
// Get all products
$args = array(
‘post_type’ => ‘product’,
‘posts_per_page’ => -1,
);
$products = get_posts($args);
foreach ($products as $product) {
$product_id = $product->ID;
$product_name = $product->post_title;
// Prepare an array to hold categories to be assigned
$categories_to_assign = array();
foreach ($categories_keywords as $category_id => $keyword) {
if (stripos($product_name, $keyword) !== false) {
// Add category ID to the list of categories to assign
$categories_to_assign[] = $category_id;
}
}
if (!empty($categories_to_assign)) {
// Assign categories to product
wp_set_object_terms($product_id, $categories_to_assign, ‘product_cat’, true);
}
}
}
// Hook the function to a specific admin action or trigger it manually
add_action(‘admin_menu’, ‘product_categorization_menu’);
function product_categorization_menu() {
add_submenu_page(‘tools.php’, ‘Product Categorization’, ‘Product Categorization’, ‘manage_options’, ‘product-categorization’, ‘product_categorization_page’);
}
function product_categorization_page() {
?>
<div class=”wrap”>
<h2>Product Categorization</h2>
<form method=”post” action=””>
<input type=”hidden” name=”categorize_products” value=”1″>
<?php submit_button(‘Categorize Products’); ?>
</form>
</div>
<?php
if (isset($_POST[‘categorize_products’])) {
categorize_products();
echo ‘<div class=”updated”><p>Products have been categorized.</p></div>’;
}
}
Step 2: Upload and Activate the Plugin
- Upload the
product-categorization.php
file to the/wp-content/plugins/
directory of your WordPress installation. - In your WordPress dashboard, navigate to “Plugins” and activate the “Product Categorization” plugin.
Step 3: Run the Plugin
- In the WordPress dashboard, go to “Tools” > “Product Categorization”.
- Click the “Categorize Products” button to start the categorization process.
The plugin will scan all product titles and assign them to categories based on the keywords that match category names.
Customizing the Plugin
You can customize the plugin to match more complex rules or additional categories. Simply modify the logic within the categorize_products
function as needed.
Conclusion
By following these steps, you can automate the process of categorizing products in your WooCommerce store, saving time and ensuring consistency. Feel free to adjust the code to better suit your specific needs. Happy coding!
By sharing this guide, you can help other WooCommerce store owners streamline their product management process. If you found this helpful, consider buying me a coffee or making a donation. Enjoy!