• itsupportacemsthailand

    (@itsupportacemsthailand)


    Hello

    I have created code to sort products by price, but now I am having a problem. When searching for results and selecting sort by price, the result is Variations with only one product price listed first, followed by Variations with more than one product price.

    But the result I want is these two Variations. Must be sorted according to the sort by price selected.

    Therefore would like to ask for help from the team.
    I have attached the code at the end of this message.

    Thank You.

    // Function to retrieve the price range of variations
    function get_variation_price_range($product) {
    if ($product->is_type('variable')) {
    $variation_prices = $product->get_variation_prices(true);
    $min_price = !empty($variation_prices['price_min']) ? $variation_prices['price_min'] : 0;
    $max_price = !empty($variation_prices['price_max']) ? $variation_prices['price_max'] : 0;

    return [
    'min' => $min_price,
    'max' => $max_price
    ];
    }
    return false;
    }

    // Function to customize the display of the price for products with multiple variations
    add_filter('woocommerce_get_price_html', 'custom_price_html_variation_range', 100, 2);
    function custom_price_html_variation_range($price, $product) {
    if ($product->is_type('variable')) {
    // Retrieve prices of all variations
    $variation_prices = $product->get_variation_prices(true);

    // Get the minimum and maximum prices
    $min_price = min($variation_prices['price']);
    $max_price = max($variation_prices['price']);

    // Check the price values
    if ($min_price > 0 && $max_price > 0) {
    // Check if the URL has orderby=price-desc
    if (isset($_GET['orderby']) && $_GET['orderby'] === 'price-desc') {
    // Display the maximum price first, followed by the minimum price
    $price = '?' . number_format($max_price, 2) . ' – ?' . number_format($min_price, 2);
    } else {
    // Display the minimum price first, followed by the maximum price
    $price = '?' . number_format($min_price, 2) . ' – ?' . number_format($max_price, 2);
    }
    } else {
    // If the price is 0, show a message indicating no price is available
    $price = 'No price available';
    }
    }

    return $price;
    }

    // Function to sort search results by price using Relevanssi
    add_filter('posts_orderby', 'custom_relevanssi_orderby', 10, 2);
    function custom_relevanssi_orderby($orderby, $query) {
    if ($query->is_search() && $query->get('post_type') === 'product') {
    if (isset($_GET['orderby'])) {
    if ($_GET['orderby'] === 'price') {
    global $wpdb;
    $orderby = "meta_value + 0 ASC"; // Change to DESC to sort from highest to lowest
    $query->set('meta_key', '_price');
    }
    }
    }
    return $orderby;
    }

    // Function to sort search results by product price
    add_action('pre_get_posts', 'sort_relevanssi_results_by_price');
    function sort_relevanssi_results_by_price($query) {
    if ($query->is_search() && !is_admin() && $query->get('post_type') === 'product') {
    // Check if orderby=price-desc or price to apply the selected sorting
    if (isset($_GET['orderby']) && $_GET['orderby'] === 'price-desc') {
    // Clear taxonomy filters like categories, tags, and attributes
    $query->set('tax_query', array());
    $query->set('meta_query', array());

    // Sort by price from highest to lowest
    $query->set('meta_key', '_price');
    $query->set('orderby', 'meta_value_num');
    $query->set('order', 'DESC');
    } elseif (isset($_GET['orderby']) && $_GET['orderby'] === 'price') {
    // Sort by price from lowest to highest
    $query->set('tax_query', array());
    $query->set('meta_query', array());

    // Sort by price from lowest to highest
    $query->set('meta_key', '_price');
    $query->set('orderby', 'meta_value_num');
    $query->set('order', 'ASC');
    }
    }
    }
Viewing 1 replies (of 1 total)
  • Plugin Author Mikko Saari

    (@msaari)

    When you order the posts like that, Relevanssi sorts them by the value in the _price meta field. The variation prices probably don’t have any effect on that.

    If you want to sort the products by price and use the variation prices, you probably need to replace the _price meta field sorting with a custom sort that uses the relevanssi_hits_filter hook to sort the posts. That lets you sort in any way you want, and the order can handle your variation prices exactly the way you want it to.

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.