How to Automatically Tag Purchased Products in WooCommerce

How to Automatically Tag Purchased Products in WooCommerce
On this page
  1. Why Auto-Tag Products on Purchase
  2. Step 1: Create the Tag and Note Its ID
  3. Step 2: Auto-Assign the Tag on Purchase
  4. Troubleshooting
  5. Frequently Asked Questions
  6. Conclusion

You can automatically tag purchased products in WooCommerce by hooking into the thank-you page action and assigning a predefined product tag to every item in the order — useful for marking bestsellers, running tag-based promotions, or building custom “previously purchased” logic.

Why Auto-Tag Products on Purchase

  • Differentiating purchased products from never-sold ones for reporting or targeted discounting
  • Powering a custom shortcode or widget that shows “popular” or “best-selling” products based on real sales activity
  • Feeding tag-based logic elsewhere on the site (like excluding sold-out-often items from a promo) without manual tagging

Step 1: Create the Tag and Note Its ID

Create a product tag (e.g. “Customer Favorite”) under Products > Tags first, and note the numeric term ID shown in the URL when editing it.

Step 2: Auto-Assign the Tag on Purchase

PHP
/** Tag every purchased product with a "Customer Favorite" tag after checkout (on the thank-you page) */
add_action( 'woocommerce_thankyou', 'tag_products_as_customer_favorite' );

function tag_products_as_customer_favorite( $order_id ) {
    $order = wc_get_order( $order_id );
    if ( ! $order ) return;

    $favorite_tag_id = 4821; // Replace with your tag's term ID

    foreach ( $order->get_items() as $item ) {
        $product = wc_get_product( $item->get_product_id() ); // The parent product: variations can't hold tags
        if ( ! $product ) continue;

        $existing_tags = $product->get_tag_ids();
        if ( ! in_array( $favorite_tag_id, $existing_tags, true ) ) {
            $product->set_tag_ids( array_merge( $existing_tags, array( $favorite_tag_id ) ) );
            $product->save();
        }
    }
}
Automatically tag purchased products in WooCommerce: the page before adding the code
Before
Automatically tag purchased products in WooCommerce: the page after adding the code
After

Troubleshooting

Tag Doesn’t Get Applied

Double-check the term ID matches the tag you created — using the tag’s name or slug instead of its numeric ID is the most common mistake, since set_tag_ids() expects integers.

Tag Gets Applied Multiple Times on Repeat Orders

The in_array() check already prevents duplicate entries in the tag array, so re-purchasing the same product won’t create redundant tags — it simply confirms the tag is still present.

Frequently Asked Questions

Can I tag products only after a certain order status, not immediately on thank-you?

Yes, swap the hook to a status-based one like woocommerce_order_status_completed if you’d rather wait until payment/fulfillment is fully confirmed.

Can I use this to tag products differently based on purchase count?

Yes, but that needs additional logic — count how many times each product has been tagged/purchased and apply a different tag once a threshold is met.

Does this work for variable product variations?

Tags apply at the parent product level, so tagging happens on the parent even if a specific variation was purchased.

Conclusion

Automatically tagging purchased products in WooCommerce is a short snippet on the thank-you hook that keeps your tag-based logic (promotions, reporting, custom shortcodes) up to date without manual tagging after every sale.

See also How to Display All Products Purchased by a Customer in WooCommerce. Then read How to Count Product Purchases by ID With a Shortcode.

Prefer no code? All-in-One Utilities Woo Add-on adds over 50 WooCommerce features, so you can switch them on without code.

Leave a Reply

Schedule a call