Untitled

mail@pastecode.io avatar
unknown
plain_text
22 days ago
1.8 kB
4
Indexable
Never
function filter_b2bking_user_prices($check, $object_id, $meta_key, $single) {
    static $is_filtering = false;

    // Check if we're already filtering to prevent recursion
    if ($is_filtering) {
        return $check;
    }

    // Check if the meta key starts with the required prefixes
    if (strpos($meta_key, 'b2bking_regular_price_user_') === 0 || strpos($meta_key, 'b2bking_sale_price_user_') === 0) {
        $is_filtering = true;

        // Get the actual meta value
        $meta_value = get_post_meta($object_id, $meta_key, true);

        // Extract the user identifier (ID or username)
        $user_identifier = substr($meta_key, strrpos($meta_key, '_') + 1);
        if (!is_numeric($user_identifier)) {
            $user = get_user_by('login', $user_identifier);
            $user_id = $user ? $user->ID : 0;
        } else {
            $user_id = intval($user_identifier);
        }
        
        // Get the user group
        $currentusergroupidnr = b2bking()->get_user_group($user_id);
        
        // Get group prices
        $grregprice = get_post_meta($object_id, 'b2bking_regular_product_price_group_' . $currentusergroupidnr, true);
        $grsaleprice = get_post_meta($object_id, 'b2bking_sale_product_price_group_' . $currentusergroupidnr, true);
        
        // Compare prices
        if ($grregprice !== '' && floatval($grregprice) < floatval($meta_value)) {
            $is_filtering = false;
            return '';
        }
        if ($grsaleprice !== '' && floatval($grsaleprice) < floatval($meta_value)) {
            $is_filtering = false;
            return '';
        }

        $is_filtering = false;
        
        return $meta_value;
    }

    return $check;
}

add_filter('get_post_metadata', 'filter_b2bking_user_prices', 10, 4);
Leave a Comment