Show a message to referred friends or advocates

Last updated

This page shows how to display your own message in the cart and checkout: a welcome for referred friends, or a reminder for advocates who haven’t used their coupons.

Built-in options first

  • Cart Message in Settings → Friend Reward appears when the friend discount is applied. See Friend reward settings.
  • With Popup Maker, the Referral link condition shows a popup to visitors who arrive through a referral link. See Show the referral link after checkout.
  • Advocates see their unused coupons in My Account → Refer a Friend.

Snippets

Read Add code snippets safely first. Both snippets are checked with version 4.4.12. They work with the classic and block cart and checkout.

Welcome referred friends

add_action( 'template_redirect', function () {
    $message = 'Welcome! A friend sent you here, so your first order is discounted.';

    if ( empty( $_COOKIE['gens_raf'] ) || ! function_exists( 'is_cart' ) || ( ! is_cart() && ! is_checkout() ) || is_wc_endpoint_url( 'order-received' ) ) {
        return;
    }
    if ( ! wc_has_notice( $message, 'notice' ) ) {
        wc_add_notice( $message, 'notice' );
    }
} );

The message shows to anyone with a referral cookie, including customers who aren’t eligible for the discount. Keep the wording general, or check eligibility in Friend discount not applied before promising a discount.

Remind advocates about unused coupons

add_action( 'template_redirect', function () {
    $message = 'You have unused referral coupons. Add them in the coupon field to get your discount.';

    if ( ! is_user_logged_in() || ! function_exists( 'is_checkout' ) || ! is_checkout() || is_wc_endpoint_url( 'order-received' ) ) {
        return;
    }

    $coupons = get_posts( array(
        'post_type'   => 'shop_coupon',
        'post_status' => 'publish',
        'numberposts' => 20,
        'meta_query'  => array(
            array( 'key' => 'customer_email', 'value' => wp_get_current_user()->user_email, 'compare' => 'LIKE' ),
            array( 'key' => 'usage_count', 'value' => '0' ),
        ),
    ) );

    foreach ( $coupons as $post ) {
        if ( strpos( $post->post_title, 'RAF-' ) !== 0 ) {
            continue;
        }
        $expires = ( new WC_Coupon( $post->post_title ) )->get_date_expires();
        if ( ! $expires || $expires->getTimestamp() > time() ) {
            if ( ! wc_has_notice( $message, 'notice' ) ) {
                wc_add_notice( $message, 'notice' );
            }
            return;
        }
    }
} );

To apply the coupons for them instead, see Apply earned coupons automatically at checkout.

Check it worked

  • For the welcome message, open a referral link in a private window, add a product and open the cart.
  • For the reminder, log in as a customer with an unused RAF-… coupon and open the checkout.

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.