Hey @dnay76,
Thank you for the confirmation! I’ve been able to put together a code snippet based on the code we already have in the plugin. Here it is:
function slicewp_custom_calculate_commissions_per_category_per_affiliate( $commission_amount, $amount, $args ) {
// The ID of the affiliate that has custom rates per category.
$affiliate_id = 21;
// The category IDs separated by comma for digital and physical products.
$product_categories_digital = array( 123, 124 );
$product_categories_physical = array( 125, 126 );
// The commission rates for category types.
$category_commission_rate_type_digital = 'percentage';
$category_commission_rate_digital = 20;
$category_commission_rate_type_physical = 'percentage';
$category_commission_rate_physical = 20;
// Check that the commission is from WooCommerce
if ( $args['origin'] != 'woo' ) {
return $commission_amount;
}
// Only sales and subscriptions are treated here
if ( ! in_array( $args['type'], array( 'sale', 'subscription') ) ) {
return $commission_amount;
}
// Check if the commission rate is per order
if ( slicewp_is_commission_basis_per_order() ) {
return $commission_amount;
}
if ( empty( $args['affiliate_id'] ) || $args['affiliate_id'] != $affiliate_id ) {
return $commission_amount;
}
// Get the product
$product_id = absint( $args['product_id'] );
$product = wc_get_product( $product_id );
// Get the product categories
$categories = get_the_terms( $product_id, 'product_cat' );
if ( ! empty( $categories[0]->term_id ) ) {
$category_commission_rate_type = '';
$category_commission_rate = '';
// Get the product category commission type and rate
if ( in_array( $categories[0]->term_id, $product_categories_digital ) ) {
$category_commission_rate_type = $category_commission_rate_type_digital;
$category_commission_rate = $category_commission_rate_digital;
}
if ( in_array( $categories[0]->term_id, $product_categories_physical ) ) {
$category_commission_rate_type = $category_commission_rate_type_physical;
$category_commission_rate = $category_commission_rate_physical;
}
// Check if the product category commission type and rate are ok
if ( ! empty( $category_commission_rate_type ) && ! empty( $category_commission_rate ) && is_numeric( $category_commission_rate ) ) {
// Calculate the commission using the product type and rate
$commission_amount = ( $category_commission_rate_type == 'percentage' ? round( ( $amount * $category_commission_rate / 100 ), 2 ) : $category_commission_rate );
return $commission_amount;
}
}
return $commission_amount;
}
add_filter( 'slicewp_calculate_commission_amount', 'slicewp_custom_calculate_commissions_per_category_per_affiliate', 20, 3 );
Please make sure to modify the $affiliate_id variable and set it to the ID of the affiliate you want to have custom rates per product category.
Then, set the $product_categories_digital and $product_categories_physical variables as the IDs of the product categories in each category type.
Lastly, set the $category_commission_rate_type_digital, $category_commission_rate_digital, $category_commission_rate_type_physical, $category_commission_rate_physical variables to the rates you wish to reward. The “rate_type” variables accept either “fixed_amount” or “percentage”. The other two accept positive integers. 1 to 100 for the “percentage” type, any other value for “fixed_amount”.
Please add the code to your website, give it a test and let me know how it goes.
Thank you and best wishes,
Mihai