WooCommerce: Automatically Add Tag To Purchased Products

This functionality can be helpful to those who need to differentiate purchased products from non-purchased ones. Think about a way to automatically discount non-tagged products, in order to entice more sales; or a function that only shows purchased products via a custom shortcode.

No matter the application, “tagging” products upon purchase is super easy. Of course, make sure to create a custom product tag first, and get its ID, so that you can use this in the code below. Enjoy!

Now that I’ve created the “Sold” product tag, the snippet below automatically assigns this to purchased products once the customer reaches the thank you page. I have now 7 products tagged as “Sold” and can use this for marketing purposes.

PHP Snippet: Auto-tag Products Upon Purchase @ WooCommerce Thank You Page

/**
 * @snippet       Programmatically Tag Products
 * @how-to        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 7
 * @community     https://businessbloomer.com/club/
 */

add_action( 'woocommerce_thankyou', 'bbloomer_auto_tag_product' );

function bbloomer_auto_tag_product( $order_id ) {
	$order = wc_get_order( $order_id );
	$auto_tag_id = array( 12345 ); // YOUR TAG ID HERE
	foreach ( $order->get_items() as $item_id => $item ) {
		$product = $item->get_product();
		$tags = $product->get_tag_ids();
		if ( ! array_intersect( $tags, $auto_tag_id ) ) {
			$product->set_tag_ids( array_merge( $tags, $auto_tag_id ) );
			$product->save();
		}
	}
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

2 thoughts on “WooCommerce: Automatically Add Tag To Purchased Products

  1. Hi there,
    Great code. But is there a way to add a tag to out of stock products? I’d quite like to filter out of stock products using a tag.
    Thanks!

    1. Colin, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *