10h30
10/12/2016 - 2:02 PM

AffiliateWP + WooCommerce - Alter the product commission depending on which category it's assigned to

AffiliateWP + WooCommerce - Alter the product commission depending on which category it's assigned to

<?php

/**
 * Change the commission amount if products belong to certain categories
 * 
*/
function affwp_custom_wc_commission_per_category( $referral_amount, $affiliate_id, $amount, $reference, $product_id ) {
 	
 	// You can specify an array of categories to alter the commission for. Separate by a comma and use either the term name, term_id, or slug 
	$categories = array( 'category-one', 5 );

	// Alter commission per product if they exist in the $categories array abve
	if ( has_term( $categories, 'product_cat', $product_id ) ) {
		$referral_amount = $amount * 0.3; // 30% commission for these categories
	}

	// or just specify 1 category
	if ( has_term( 'some-category', 'product_cat', $product_id ) ) {
		$referral_amount = 50; // $50.00 commission for a product in this category
	}

	// and another one
	if ( has_term( 2, 'product_cat', $product_id ) ) {
		$referral_amount = $amount * 0.8; // 80% commission for a product in this category
	}

    return $referral_amount;
}
add_filter( 'affwp_calc_referral_amount', 'affwp_custom_wc_commission_per_category', 10, 5 );