Only reward referrals that include specific products

By default, any order from a referred friend earns the advocate a reward. This snippet rewards the advocate only when the friend’s order includes products or categories you choose.

To limit the friend’s discount to certain products instead, you don’t need code. Set Products or Product categories on the friend coupon in Marketing → Coupons. See Coupon types and restrictions explained.

Add the snippet

Read Add code snippets safely first. Checked with version 4.4.12.

Put your product IDs in $product_ids and your category IDs in $category_ids. Leave either list empty if you don’t need it. You’ll see the IDs when you hover a product in Products, or a category in Products → Categories.

add_filter( 'gens_raf_order_info', function ( $raf_info, $order, $referrer_id ) {
    $product_ids  = array( 123, 456 );
    $category_ids = array( 78 );

    // Leave referrals that are already blocked, for example by fraud checks, unchanged.
    if ( ! isset( $raf_info['generate'] ) || 'true' !== $raf_info['generate'] ) {
        return $raf_info;
    }

    foreach ( $order->get_items() as $item ) {
        $product_id = $item->get_product_id();
        if ( in_array( $product_id, $product_ids, true ) ) {
            return $raf_info;
        }
        if ( $category_ids && has_term( $category_ids, 'product_cat', $product_id ) ) {
            return $raf_info;
        }
    }

    $raf_info['info']               = 'Order has no qualifying products, so no referral reward.';
    $raf_info['generate']           = 'false';
    $raf_info['increase_referrals'] = 'false';
    return $raf_info;
}, 10, 3 );

How it works

  • The check runs when the friend places the order.
  • Variations count as their parent product, so use the parent product’s ID.
  • The friend still gets their first-purchase discount. Only the advocate’s reward depends on the products.
  • Orders without a qualifying product show “Order has no qualifying products, so no referral reward.” in the Refer a Friend Data box, and don’t count towards the advocate’s referrals.

Check it worked

Place one test referral order with a qualifying product and one without. Only the first should say “Referral is fine. Coupon will be generated on the order complete.” in its Refer a Friend Data box.

Related

Browse our plugins

Lightweight WooCommerce plugins built for speed. No bloat, no frameworks -- just clean code that works.

View all plugins
Stay in the loop

Get notified when we launch new plugins. No spam, just product updates.