Detect Product Type with code

The is_virtual() method in WC_Product class it return boolean.

It means that, if $product->is_virtual() return true, it is virtual product, vice versa if return false, then the product is physical.

In your case, you can use this

if( $product->is_virtual() ) {
  //is Virtual Product, do something here

if( !$product->is_virtual() ) {
  //is Physical Product, do something here

For the affiliate or so-called External/Affiliate, it’s a product type in woocommerce.

To detect whether it’s External/Affiliate, you can use is_type() method in WC_Product class.

if( $product->is_type('external') ) {
 //is Affiliate Product, do something here