• Resolved supermichael

    (@supermichael)


    I have the following issue: almost all my products are variable products, which means that there is a price range. So in the beginning on the shop-page and homepage the price ranges where shown. But I wanted to display the default variation price I have set on the products. So I used to following code:

    add_filter('woocommerce_variable_price_html', 'custom_variation_price', 10, 2);
    function custom_variation_price( $price, $product ) {
        $available_variations = $product->get_available_variations();
        $selectedPrice = '';
        $dump = '';
    
        foreach ( $available_variations as $variation )
        {
            // $dump = $dump . '<pre>' . var_export($variation['attributes'], true) . '</pre>';
    
            $isDefVariation=false;
            foreach($product->get_default_attributes() as $key=>$val){
                // $dump = $dump . '<pre>' . var_export($key, true) . '</pre>';
                // $dump = $dump . '<pre>' . var_export($val, true) . '</pre>';
                if($variation['attributes']['attribute_'.$key]==$val){
                    $isDefVariation=true;
                }   
            }
            if($isDefVariation){
                $price = $variation['display_price'];         
            }
        }
        $selectedPrice = wc_price($price);
    
    //  $dump = $dump . '<pre>' . var_export($available_variations, true) . '</pre>';
    
        return $selectedPrice . $dump;
    }

    This code worked the way I wanted and right now on my shop all the products are displaying the default variation price. But the problem is now when I want to ‘sort by price’ from low to high or the other way around, it still on the price range. So when the price ranges are:

    1.00 – 5.00 –> Default variation price is 2.50
    1.50 – 6.00 –> Default variation price is 2.00
    2.00 – 7.50 –> Default variation price is 2.25

    It returns like this from high to low:

    (1) 2.25
    (2) 2.00
    (3) 2.50

    So my question is, what can I do to make sure that ‘sort by price’ sorts on the default variation prices which are shown to the customers.

Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘‘Sort by price’ filter not working with the default variation price’ is closed to new replies.