Problem with the price range
-
I am customizing the price filter and have modified the
get_min_price
andget_max_price
functions in the helper file. However, when I set a price range, it doesn’t correctly consider the actual prices of the products.Thank you in advance.
-
Hello
Ok! In this case you should delete your customization.
Unfortunately, I don’t know your custom code and can’t determine the reason for this behavior.
Hi , I added this customisation : same idea for the function get min :Price based on type user connected .
public static function get_max_price($additional_taxes = "") {
global $wpdb;
$meta_key = (is_user_logged_in() && get_user_meta(get_current_user_id(), 'statut_pro', true) === 'yes')
? '_prix_pro'
: '_price';
if (version_compare(WOOCOMMERCE_VERSION, '2.6', '>')) {
// Pour WooCommerce version supérieure à 2.6
$prices = self::get_filtered_price($additional_taxes);
$max = ceil($prices->max_price ?: 0);
} else {
// Pour WooCommerce version 2.6 ou inférieure
self::set_layered_nav_product_ids();
if (0 === sizeof(WC()->query->layered_nav_product_ids)) {
$sql_data = array(
array(
'val' => $wpdb->posts,
'type' => 'string',
),
array(
'val' => $wpdb->postmeta,
'type' => 'string',
),
array(
'val' => $meta_key, // Utiliser le bon meta_key selon le statut de l'utilisateur
'type' => 'string',
),
);
$query_txt = self::woof_prepare('
SELECT max(meta_value + 0)
FROM %1$s
LEFT JOIN %2$s ON %1$s.ID = %2$s.post_id
WHERE meta_key IN ("' . implode('","', apply_filters('woocommerce_price_filter_meta_keys', array($meta_key))) . '")
', $sql_data);
$max = ceil($wpdb->get_var($query_txt));
} else {
$sql_data = array(
array(
'val' => $wpdb->posts,
'type' => 'string',
),
array(
'val' => $wpdb->postmeta,
'type' => 'string',
),
);
$max = ceil($wpdb->get_var(
self::woof_prepare('
SELECT max(meta_value + 0)
FROM %1$s
LEFT JOIN %2$s ON %1$s.ID = %2$s.post_id
WHERE meta_key IN ("' . implode('","', apply_filters('woocommerce_price_filter_meta_keys', array($meta_key))) . '")
AND (
%1$s.ID IN (' . implode(',', array_map('absint', WC()->query->layered_nav_product_ids)) . ')
OR (
%1$s.post_parent IN (' . implode(',', array_map('absint', WC()->query->layered_nav_product_ids)) . ')
AND %1$s.post_parent != 0
)
)
', $sql_data
)));
}
}
// Added 20% for user not pro
if (!is_user_logged_in() || get_user_meta(get_current_user_id(), 'statut_pro', true) !== 'yes') {
$max = ceil($max * 1.2);
}
return $max;
}Hello
Your changes to the code don’t work because the price is taken from get_filtered_price
Hi, thank you , but when I want to added condition to use custom meta like “price-pro” based on type user connected, the range still false , the meta not taked.
- This reply was modified 1 week, 3 days ago by aliceevra.
Hello
ok! Give me the code you changed.
Hi , this is the code
public static function get_filtered_price($additional_taxes = “”) {
global $wpdb, $wp_the_query, $WOOF;// Check if the user is logged in and has the 'pro' role
$user_is_pro = is_user_logged_in() && current_user_can('pro'); // Adjust 'pro' to the correct user role or custom condition
$args = $wp_the_query->query_vars;
$tax_query = isset($args['tax_query']) ? $args['tax_query'] : array();
if (is_object($wp_the_query->tax_query)) {
$tax_query = $wp_the_query->tax_query->queries; //fix for cat page
}
$meta_query = isset($args['meta_query']) ? $args['meta_query'] : array();
// Adapt tax query for additional taxes
$tax_query = woof()->get_tax_query($additional_taxes);
// Get the current taxonomy term
$current_term = woof()->get_really_current_term();
if (!empty($current_term)) {
$tax_query[] = array(
'taxonomy' => $current_term->taxonomy,
'field' => 'slug', //id
'terms' => $current_term->slug
);
}
// Handle cases with more than one taxonomy
$temp_arr = array();
if (isset($args['taxonomy']) AND isset($args[$args['taxonomy']]) AND !empty($args[$args['taxonomy']])) {
$temp_arr = explode(',', $args[$args['taxonomy']]);
if (!$temp_arr OR count($temp_arr) < 1) {
$temp_arr = array();
}
}
// Apply taxonomy filters
if (!empty($args['taxonomy']) && !empty($args['term'])) {
$tax_query[] = array(
'taxonomy' => $args['taxonomy'],
'terms' => (empty($temp_arr)) ? array($args['term']) : $temp_arr,
'field' => 'slug',
);
}
// Exclude price and rating filters from meta query
if (!empty($meta_query) AND is_array($meta_query)) {
foreach ($meta_query as $key => $query) {
if (!empty($query['price_filter']) || !empty($query['rating_filter'])) {
unset($meta_query[$key]);
}
}
}
// Create meta and taxonomy queries
$meta_query = new WP_Meta_Query($meta_query);
$tax_query = new WP_Tax_Query($tax_query);
// Generate SQL for meta and tax queries
$meta_query_sql = $meta_query->get_sql('post', $wpdb->posts, 'ID');
$tax_query_sql = $tax_query->get_sql($wpdb->posts, 'ID');
// Check whether to use the 'prix_profesionnel' meta key for pro users
$price_meta_key = $user_is_pro ? '_prix_profesionnel' : '_price';
// Build SQL query
$sql = "SELECT min( FLOOR( price_meta.meta_value + 0.0) ) as min_price, max( CEILING( price_meta.meta_value + 0.0) ) as max_price
FROM {$wpdb->posts} ";
$sql .= " LEFT JOIN {$wpdb->postmeta} as price_meta ON {$wpdb->posts}.ID = price_meta.post_id " . $tax_query_sql['join'] . $meta_query_sql['join'];
$sql .= " WHERE {$wpdb->posts}.post_type = 'product'
AND {$wpdb->posts}.post_status = 'publish'
AND price_meta.meta_key IN ('" . implode("','", array_map('esc_sql', apply_filters('woocommerce_price_filter_meta_keys', array($price_meta_key)))) . "')
AND price_meta.meta_value > '' ";
$sql .= $tax_query_sql['where'] . $meta_query_sql['where'];
$sql = apply_filters('woof_get_filtered_price_query', $sql);
// Caching mechanism
if (isset(woof()->settings['price_transient']) AND woof()->settings['price_transient']) {
$data_key = md5($sql . 'woof');
$data = get_transient('woof_min_max_prices');
if (!is_array($data)) {
$data = array();
}
if (isset($data[$data_key])) {
$value = $data[$data_key];
return $value;
}
$prices = $wpdb->get_row($sql);
$data[$data_key] = $prices;
set_transient('woof_min_max_prices', $data, 1 * 24 * 3600); // 1 day
}
$prices = $wpdb->get_row($sql);
return $prices;}
- This reply was modified 1 week ago by aliceevra.
Hello
Make sure you are using the correct meta key. For example, these two snippets have different meta keys
Check how the data is stored in this meta field, it should be a number
Hi , I use meta key based on type account user connected. For user pro : _prix_professionnel : it’is a custom price and for user not pro : regular price the standard of woocommerce.
- You must be logged in to reply to this topic.