- It is possible to add global user, role & group based pricing rules which are applicable to all the products on your WooCommerce store.
- Adding individual rules for all the products will be time-consuming.
- Hence, it will be useful for the admins managing large web stores.
- It is possible with the help of the following filters.
- “wdm_csp_filter_usp_rules_for_a_product”.
- “wdm_csp_filter_rsp_rules_for_a_product”.
- “wdm_csp_filter_gsp_rules_for_a_product”.
- Priorities for these rules will work the same as the default priorities in CSP.
Adding a user-specific global discount
- The following code will explain how you can apply global discounts for your site users.
add_filter('wdm_csp_filter_usp_rules_for_a_product', 'applyUSPglobalDiscounts', 99, 3);
function applyUSPglobalDiscounts( $uspRules, $user, $productIds) {
//get global discount value for the user
$discountValue = 10; //Replace this line with your logic to get the discount value for the user
if (empty($discountValue)) {
return $uspRules;
}
if (is_array($productIds)) {
//Variable product variations
foreach ($productIds as $productId) {
$temp = new \stdClass();
$temp->price = $discountValue;
$temp->min_qty = 1;
$temp->flat_or_discount_price = 2;
$temp->product_id = $productId;
$uspRules[] = $temp;
}
} else {
//Simple products
$temp = new \stdClass();
$temp->price = $discountValue;
$temp->min_qty = 1;
$temp->price_type = 2;
$temp->product_id = $productIds;
$uspRules[] = $temp;
}
return $uspRules;
}
- Adding the above code in your child themes functions.php file will allow a 10% sitewide discount for all users because of the code on Line #5.
Adding a role-specific global discount
- The following code will explain how you can apply global discounts for the user roles on your site.
add_filter('wdm_csp_filter_rsp_rules_for_a_product', 'applyRSPglobalDiscounts', 99, 3);
function applyRSPglobalDiscounts( $rspRules, $userRoles, $productIds) {
//TODO: add your logic to get the discount value for all the user roles & pick the value which will give the most discount
$discountValue = 10;
if (empty($discountValue)) {
return $rspRules;
}
if (is_array($productIds)) {
foreach ($productIds as $productId) {
$temp = new \stdClass();
$temp->price = $discountValue;
$temp->min_qty = 1;
$temp->flat_or_discount_price = 2;
$temp->product_id = $productId;
$rspRules[] = $temp;
}
} else {
$temp = new \stdClass();
$temp->price = $discountValue;
$temp->min_qty = 1;
$temp->price_type = 2;
$temp->product_id = $productIds;
$rspRules[] = $temp;
}
return $rspRules;
}
- Adding the above code in your child themes functions.php file will allow a 10% sitewide discount for all user roles because of the code on Line #5.
- Refer to the comment on line #4 to understand more.
Adding a group-specific global discount
- The following code will explain how you can apply global discounts for the user groups on your site.
add_filter('wdm_csp_filter_gsp_rules_for_a_product', 'applyGSPglobalDiscounts', 99, 3);
function applyGSPglobalDiscounts( $gspRules, $userGroups, $productIds) {
//get globalCSP discount values for the group or for the defined groups
//TODO: get the discount value for all the user groups & pick the value which will give the most discount
$discountValue = 10;
if (empty($discountValue)) {
return $gspRules;
}
if (is_array($productIds)) {
foreach ($productIds as $productId) {
$temp = new \stdClass();
$temp->price = $discountValue;
$temp->min_qty = 1;
$temp->flat_or_discount_price = 2;
$temp->product_id = $productId;
$gspRules[] = $temp;
}
} else {
$temp = new \stdClass();
$temp->price = $discountValue;
$temp->min_qty = 1;
$temp->price_type = 2;
$temp->product_id = $productIds;
$gspRules[] = $temp;
}
return $gspRules;
}
- Adding the above code in your child themes functions.php file will allow a 10% sitewide discount for all user groups because of the code on Line #5.
- Refer to the comment on line #4 to understand more.