How to Automatically Tag WooCommerce Customers as B2B or B2C

Want to automatically tag WooCommerce customers as B2B or B2C based on whether they fill in a company name at checkout?

This quick tutorial shows you how to set it up using a simple code snippet and Noptin.

What You Need

  • At least version 3.0 of WooCommerce.
  • At least version 3.5 of Noptin.
  • (Optional) Enable the Noptin and WooCommerce integration to collect subscribers before tagging them since the below code snippet does not collect new subscribers. It just tags them.

Step 1: Add the Code Snippet

Add this PHP code snippet to your site.

<?php
add_action( 'woocommerce_checkout_order_processed', 'tag_b2b_or_b2c_customer' );
add_action( 'woocommerce_store_api_checkout_order_processed', 'tag_b2b_or_b2c_customer' );

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

    $email = $order->get_billing_email();
    $company = $order->get_billing_company();

    // Try to get the Noptin subscriber by email
    $subscriber = noptin_get_subscriber( $email );
    if ( ! $subscriber->exists() ) return;

    // Determine the tag
    $tag_to_add = empty( $company ) ? 'B2C' : 'B2B';
    $tag_to_remove = empty( $company ) ? 'B2B' : 'B2C';

    // Update tags
    $subscriber->set( 'tags::add', $tag_to_add );
    $subscriber->set( 'tags::remove', $tag_to_remove );
    $subscriber->save();
}

Step 2: Test Your Checkout

Place a test order:

  • With a company name, Noptin should tag the subscriber as B2B.
  • Without a company name, Noptin should tag the subscriber as B2C.

Check the subscriber record in Noptin > Subscribers to confirm the tag was added correctly.

Auto-tag customers in WooCommerce checkout

Wrap Up

This automation makes it easy to segment your B2B or B2C marketing audience. Once tagged, you can send targeted emails or add them to different sequences in Noptin.

Leave a Reply

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