Make referral link to work for specific product or category of products only
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);