To implement custom affiliate marketing functionality in WooCommerce, you can use a combination of WooCommerce hooks and custom PHP functions. This will allow you to track and reward affiliates for referring customers to your WooCommerce store.
One way to achieve this is by using the woocommerce_thankyou
hook, which is triggered after a successful order is placed. You can create a custom function that checks if the customer was referred by an affiliate, and if so, credit the affiliate with a commission.
Here’s an example code snippet that demonstrates this functionality:
// Add a custom affiliate commission on successful order
function wpsnippets_add_affiliate_commission( $order_id ) {
// Get the order object
$order = wc_get_order( $order_id );
// Get the customer who placed the order
$customer_id = $order->get_customer_id();
// Check if the customer has an affiliate ID
$affiliate_id = get_user_meta( $customer_id, 'affiliate_id', true );
// If the customer has an affiliate ID, credit the affiliate with a commission
if ( $affiliate_id ) {
// Calculate the commission amount based on the order total
$commission_amount = $order->get_total() * 0.1; // 10% commission
// Add the commission to the affiliate's balance
wpsnippets_add_affiliate_balance( $affiliate_id, $commission_amount );
}
}
add_action( 'woocommerce_thankyou', 'wpsnippets_add_affiliate_commission' );
// Function to add commission to affiliate's balance
function wpsnippets_add_affiliate_balance( $affiliate_id, $commission_amount ) {
// Get the affiliate's current balance
$current_balance = get_user_meta( $affiliate_id, 'affiliate_balance', true );
// Update the balance by adding the commission amount
$new_balance = $current_balance + $commission_amount;
// Update the affiliate's balance
update_user_meta( $affiliate_id, 'affiliate_balance', $new_balance );
}
In this example, when a customer places an order, the wpsnippets_add_affiliate_commission
function is triggered. It retrieves the customer’s ID and checks if they have an affiliate ID stored in their user meta. If an affiliate ID is found, it calculates the commission amount (in this case, 10% of the order total) and adds it to the affiliate’s balance using the wpsnippets_add_affiliate_balance
function.
You can modify this code snippet to suit your specific affiliate marketing requirements, such as adjusting the commission percentage or adding additional conditions for crediting commissions.
Examples
Example 1: Creating a Custom Affiliate Link for WooCommerce Products
This use case demonstrates how to create a custom affiliate link for WooCommerce products. The code example below shows a custom PHP function wpsnippets_get_affiliate_link()
that accepts a product ID as a parameter and returns the affiliate link for that product.
function wpsnippets_get_affiliate_link($product_id) {
$affiliate_id = get_current_user_id(); // Get the current user's affiliate ID
$product_url = get_permalink($product_id); // Get the product URL
$affiliate_link = add_query_arg('ref', $affiliate_id, $product_url); // Append the affiliate ID as a query parameter
return esc_url($affiliate_link); // Return the affiliate link
}
Explanation: The wpsnippets_get_affiliate_link()
function retrieves the current user’s affiliate ID using get_current_user_id()
. It then gets the product URL using get_permalink($product_id)
. The affiliate ID is appended to the product URL as a query parameter using add_query_arg()
. Finally, esc_url()
is used to sanitize and return the affiliate link.
Example 2: Tracking Affiliate Sales with WooCommerce
This use case demonstrates how to track affiliate sales with WooCommerce. The code example below shows a custom PHP function wpsnippets_track_affiliate_sale()
that can be called after a successful purchase to track the affiliate sale.
function wpsnippets_track_affiliate_sale($order_id) {
$order = wc_get_order($order_id); // Get the order object
$affiliate_id = get_query_var('ref'); // Get the affiliate ID from the query parameter
if ($affiliate_id) {
$order->add_meta_data('affiliate_id', $affiliate_id); // Store the affiliate ID as order meta data
$order->save(); // Save the order
}
}
Explanation: The wpsnippets_track_affiliate_sale()
function retrieves the order object using wc_get_order($order_id)
. It then gets the affiliate ID from the query parameter using get_query_var('ref')
. If an affiliate ID is found, it is stored as meta data for the order using $order->add_meta_data()
. Finally, the order is saved using $order->save()
.
Example 3: Displaying Affiliate Information on the Order Confirmation Page
This use case demonstrates how to display affiliate information on the order confirmation page in WooCommerce. The code example below shows how to retrieve and display the affiliate ID and name for the order.
add_action('woocommerce_thankyou', 'wpsnippets_display_affiliate_info');
function wpsnippets_display_affiliate_info($order_id) {
$order = wc_get_order($order_id); // Get the order object
$affiliate_id = $order->get_meta('affiliate_id'); // Get the affiliate ID from the order meta data
if ($affiliate_id) {
$affiliate_name = get_user_meta($affiliate_id, 'nickname', true); // Get the affiliate's nickname
echo 'Affiliate ID: ' . $affiliate_id . '<br>';
echo 'Affiliate Name: ' . $affiliate_name;
}
}
Explanation: The wpsnippets_display_affiliate_info()
function is hooked to the woocommerce_thankyou
action, which is triggered on the order confirmation page. It retrieves the order object using wc_get_order($order_id)
. The affiliate ID is then retrieved from the order meta data using $order->get_meta('affiliate_id')
. If an affiliate ID is found, the affiliate’s nickname is retrieved using get_user_meta($affiliate_id, 'nickname', true)
. Finally, the affiliate ID and name are displayed using echo
.