Make referral link to work for specific product or category of products only

READ FIRST: Code is updated for plugin version 2.0! Also, this code needs to be added at the end of the functions.php file. If you are familiar with editing functions.php, feel free to continue reading. If you haven't done this before, adding this code in wrong place might bring the site down and the only way to access it is by editing functions.php file again via FTP. Feel free to contact us if you need help!

If you want to restrict referral link to work only when purchasing a specific product, add a code below to functions.php file, of course, change "12345" to your product ID:

function only_specific_product($raf_info,$order,$referrer_id) {
    $items = $order->get_items();
    $enable_referral = "no";
    foreach ( $items as $item ) {
      $product_id = $item['product_id'];
      if($product_id == "12345") {
      	$enable_referral = "yes";
      }
    }
    if($enable_referral === "no") {
    	$raf_info = array("info" => "Specific product wasnt in the cart, referral wont be generated.", "generate" => "false", "increase_referrals" => "false");
    }
    return $raf_info;
}
add_filter( 'gens_raf_order_info', 'only_specific_product',10,3);

And if you want to restrict referral link to work only when purchasing a products from specific category, add a code below to functions.php file but change "12345" to your category ID:

function wpgens_only_specific_cat($raf_info,$order,$referrer_id) {
    $items = $order->get_items();
    $enable_referral = "no";
    foreach ( $items as $item ) {
      $product_id = $item['product_id'];
      $term_list = wp_get_post_terms($product_id, 'product_cat', array('fields' => 'ids'));
      if (is_array($term_list) && in_array("add_your_cat_id_here", $term_list)) {
      	$enable_referral = "yes";
      }
    }
    if($enable_referral === "no") {
    	$raf_info = array("info" => "Specific category wasnt in the cart, referral wont be generated.", "generate" => "false", "increase_referrals" => "false");
    }
    return $raf_info;
}
add_filter( 'gens_raf_order_info', 'wpgens_only_specific_cat',10,3);

Still need help? Contact Us Contact Us