Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

PHP How to add a Refer a Friend using php

Matt231

New Coder
Hi - rather than add a plugin to my wordpress site, I would prefer to edit the php (to keep page loading speed good), so can someone please guide me on the php code to use? I need to have some text on the thank you page of woocommerce to tell the user to recommend to their friend to use the site (by giving the referer a session version of the site's url, and once used to buy it fires a coupon code back to the referer (either by email or appearing in the woocommerce account page, or ideally both). The site to view existing coding is Propress.co.uk. Thanking you in advance!
 
Implementing a referral system involves multiple steps, and the details can vary based on your specific requirements and the structure of your WordPress theme. Below is a basic guide on how you might approach adding a "Refer a Friend" feature to your WordPress site with WooCommerce using PHP. This is a simplified example, and you may need to adjust it based on your specific needs and the structure of your theme.

1. Identify the Thank You Page​

First, find the thank you page template in your WordPress theme. This is the page that users see after completing a purchase. It might be named thankyou.php or similar.

2. Modify the Thank You Page​

Edit the thank you page template and add the referral message. You can use the wc_get_order function to get information about the order. Add the referral link with a unique identifier to the thank you page.

PHP:
// Inside your thank you page template

$order_id = get_query_var('order-received');
$order = wc_get_order($order_id);

if ($order) {
$referral_link = add_query_arg('referrer', get_current_user_id(), home_url());
echo '<p>Refer a friend and get rewards! Share this link: <a href="' . esc_url($referral_link) . '">' . esc_url($referral_link) . '</a></p>';
}

3. Create a Coupon for Referrers​

In your WordPress admin panel, create a new coupon under WooCommerce → Coupons. Set up the coupon as you like (e.g., a percentage or fixed discount). Remember the coupon code, as you'll use it to apply discounts to referrers' orders.

4. Implement Referral Tracking​

In your theme's functions.php file, add the following code to track referrals. This code checks if there's a referrer parameter in the URL and applies the coupon to the referrer's account.

PHP:
// Inside your theme's functions.php

add_action('woocommerce_thankyou', 'apply_referral_coupon');

function apply_referral_coupon($order_id) {
$referrer_id = isset($_GET['referrer']) ? intval($_GET['referrer']) : 0;

if ($referrer_id > 0) {
// Apply the coupon to the referrer's account
$coupon_code = 'YOUR_COUPON_CODE'; // Replace with your actual coupon code
WC()->cart->add_discount($coupon_code);
}
}
Replace 'YOUR_COUPON_CODE' with the actual coupon code you created in step 3.

Important Notes:​

  • This is a basic example, and you might need to enhance it based on your specific needs.
  • Ensure you thoroughly test the referral system in a staging environment before deploying it to a live site.
  • Consider implementing security measures to prevent abuse, like limiting the number of times a coupon can be used.
Remember to backup your site before making any changes, and consider consulting with a developer if you're not comfortable making these modifications yourself.
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom