phpwordpressfunctionwoocommerce

Automatically assign products to categories based on their product tags in Woocommerce


I currently have a function which successfully adds products to Woocommerce using wp_insert_post().

I'm now trying to assign products to relevant categories based on their product tags. For example, if the product is added with the product tags 'ring' or 'necklace' then it is auto assigned to the 'Jewellery' category.

Using the following function, I'm able to achieve the correct functionality on posts, but have had no luck in attempting to make this work for the products post type used in woocommerce.

Works for posts:

function auto_add_category ($post_id = 0) {
 if (!$post_id) return;
 $tag_categories = array (
   'ring' => 'Jewellery',
   'necklace' => 'Jewellery',
   'dress' => 'Clothing',
 );
 $post_tags = get_the_tags($post_id);
 foreach ($post_tags as $tag) {
   if ($tag_categories[$tag->name] ) {
     $cat_id = get_cat_ID($tag_categories[$tag->name]);
     if ($cat_id) {
       $result =  wp_set_post_terms( $post_id, $tags = $cat_id, $taxonomy = 'category', $append = true );
     }
   }
   }
 }
 add_action('publish_post','auto_add_category');


I've tried to repurpose the code to work for products as follows:

function auto_add_category ($product_id = 0) {
    if (!$product_id) return;
 $tag_categories = array (
    'ring' => 'Jewellery'
    'necklace' => 'Jewellery',
    'dress' => 'Clothing',
 );
 $product_tags = get_terms( array( 'taxonomy' => 'product_tag') );
 foreach ($product_tags as $tag) {
    if ($tag_categories[$tag->name] ) {
        $cat = get_term_by( 'name', $tag_categories[$tag->name], 'product_cat' );
        $cat_id = $cat->term_id;
        if ($cat_id) {
            $result =  wp_set_post_terms( $product_id, $tags = $cat_id, $taxonomy = 'product_cat', $append = true );
        }
    }
 }
}
add_action('publish_product','auto_add_category');


However, it doesn't assign the relevant categories upon product creation. Any assistance would be greatly appreciated by this novice coder muddling his way through wordpress!


Solution

  • Try this code:

    function auto_add_category ($product_id = 0) {
    
        if (!$product_id) return;
    
        // because we use save_post action, let's check post type here
        $post_type = get_post_type($post_id);
        if ( "product" != $post_type ) return;
    
        $tag_categories = array (
            'ring' => 'Jewellery'
            'necklace' => 'Jewellery',
            'dress' => 'Clothing',
        );
    
        // get_terms returns ALL terms, so we have to add object_ids param to get terms to a specific product
        $product_tags = get_terms( array( 'taxonomy' => 'product_tag', 'object_ids' => $product_id ) );
        foreach ($product_tags as $term) {
            if ($tag_categories[$term->slug] ) {
                $cat = get_term_by( 'name', $tag_categories[$term->slug], 'product_cat' );
                $cat_id = $cat->term_id;
                if ($cat_id) {
                    $result =  wp_set_post_terms( $product_id, $cat_id, 'product_cat', true );
                }
            }
        }
    }
    add_action('save_post','auto_add_category');