Created
May 16, 2023 15:46
-
-
Save plugin-republic/47f22104b041c959963cb00a1f16f9a1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Filter global role-based discounts | |
*/ | |
function prefix_role_pricing_rules( $rules, $product=false ) { | |
if( ! $product ) { | |
return $rules; | |
} | |
// List your discounts here | |
// List each brand ID with an array of roles and their adjusted discounts | |
$discounts_by_brand = array( | |
'32' => array( | |
'administrator' => 12, | |
'editor' => 15 | |
), | |
'33' => array( | |
'administrator' => 12, | |
'editor' => 18 | |
), | |
); | |
// Check what the user role is | |
$roles = wcfad_get_current_user_roles(); | |
if( ! $roles ) { | |
return $rules; | |
} | |
// Check what brand the product is in | |
$brands = get_the_terms( $product->get_id(), 'product_brand' ); | |
if( ! empty( $brands[0] ) ) { | |
$brand = $brands[0]; | |
$brand_id = $brand->term_id; | |
// Let's check if the brand applies | |
if( ! empty( $discounts_by_brand[$brand_id] ) ) { | |
$user_role = $roles[0]; | |
$rules['amount'][$user_role] = $discounts_by_brand[$brand_id][$user_role]; | |
} | |
} | |
return $rules; | |
} | |
add_filter( 'wcfad_role_pricing_rules', 'prefix_role_pricing_rules', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment