Add manufacturer tag to WooCommerce product from title

I had to tag more than 4000 products so I came up with the following WP action:

/**
 * Add manufacturer product_tag to WooCommerce product from post title
 *
 * @return void
 **/
function mz_add_manufacturer_tag() {

    // Get manufacturer from product title
    for ($i = 1; $i < 6; $i++) {
        $args = array(
            'post_type' => 'product',
            'posts_per_page' => 1000,
            'page' => $i,
        );
        $products = new WP_Query( $args );
        while ( $products->have_posts() ) {
            $products->the_post();

            // First word of title is manufacturer
            $terms = explode( ' ', get_the_title() );

            // Append tag
            if ( ! has_term( $terms[0], 'product_tag' ) ) {
                wp_set_post_terms( get_the_ID(), $terms[0], 'product_tag', true );
            }
        }

    }

}
add_action( 'init', 'mz_add_manufacturer_tag' );
Advertisements

Author: Michal Zuber

Full stack developer, biker and rollerblader. Owner and developer at https://nevilleweb.sk/ Co-founded http://neville.sk/ Blog at https://michalzuber.wordpress.com/

3 thoughts on “Add manufacturer tag to WooCommerce product from title”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s