Compatible with YITH WooCommerce Role Based Prices Premium by YITHEMES

Ensure compatibility with YITH WooCommerce Role Based Prices Premium by YITHEMES.

/**
 * Compatible with YITH WooCommerce Role Based Prices Premium by YITHEMES
 * */
add_filter('yith_wcrbp_get_role_based_price', 'yith_wcrbp_get_role_based_price_compatible_with_discount_rules', 10, 2);

function yith_wcrbp_get_role_based_price_compatible_with_discount_rules($return, $_product){
    remove_filter('yith_wcrbp_get_role_based_price', 'yith_wcrbp_get_role_based_price_compatible_with_discount_rules', 10, 2);
    global $flycart_woo_discount_rules;
    if(!empty($flycart_woo_discount_rules)){
        $discountPrice = $flycart_woo_discount_rules->pricingRules->getDiscountPriceOfProduct($_product);
        if($discountPrice !== null){
            $return = $discountPrice;
        }
    }
    add_filter('yith_wcrbp_get_role_based_price', 'yith_wcrbp_get_role_based_price_compatible_with_discount_rules', 10, 2);
    return $return;
}

add_action('woocommerce_before_calculate_totals', function (){
    remove_filter('yith_wcrbp_get_role_based_price', 'yith_wcrbp_get_role_based_price_compatible_with_discount_rules', 10, 2);
}, 999);
add_action('woocommerce_before_calculate_totals', function (){
    add_filter('yith_wcrbp_get_role_based_price', 'yith_wcrbp_get_role_based_price_compatible_with_discount_rules', 10, 2);
}, 1001);