How To Override The Role-based Price When The Sale Price Is Lower?

Do you want to override the role-based price when the sale price is lower? The following code snippet will help you display the Sale price when it is lower than the role-based pricing.

add_filter('xa_pbu_skip_product_on_sale','skip_product_on_sale_from_discount',1,2);
function skip_product_on_sale_from_discount($return_val,$pid) {
$current_user = wp_get_current_user()->user_email;
$current_user_role= wp_get_current_user()->roles;
$display_price = get_post_meta( $pid,'_price',true );
$sale_price = get_post_meta( $pid,'_sale_price',true );
$product_user_role_price = get_post_meta($pid, 'product_role_based_price_'.$current_user_role[0],true);
$product_users_price = get_post_meta($pid, 'product_role_based_price_user_'. $current_user);
if(!empty($sale_price) && !empty($product_users_price) && $product_users_price<$sale_price){
return false;
}elseif(!empty($sale_price) && !empty($product_user_role_price) && $product_user_role_price<$sale_price ){
return false;
}
if( !empty($sale_price) && !empty($display_price) && $display_price == $sale_price ) {
return true; // true to skip this product
}
return $return_val;
}

You can simply add the above code in the activated theme’s functions.php file on your site and save the changes for implementing the code. Check the below screenshot to understand this better:

How To Override The Role-based Price When The Sale Price Is Lower?

You may also need to select any desired product and define the sale and role-based pricing. In the below screenshot, you can see we have set up a role-based price of $90 for ‘Administrator’ user role.

Product page visible to a user with the Administrator user role before implementing the code snippet:

Product Page after implementing the code snippet:

This is how you can override the role-based price when the sale price is lower.

To know more about the product, check out the ELEX WooCommerce Catalog Mode, Wholesale & Role Based Pricing

Or check out the documentation section for more related articles.

For more creative snippets of information visit elex-snippets.

Next How to Display Stock Status in the Product Page Without Enabling the Manage Stock Option?

Leave a Reply

WordPress Image Lightbox Plugin