2

I'm wondering how I can display the product price on the shop page. Right now my variable products are shown with their price range. Shope page

But these are both products that have been setup with default variable parameters: Default Parameters

When you click on a product, it's being shown as followed: Single Product

As you can see the price of that specific selection is €300, I'm wondering how I can display that €300 on the shop page instead of €150-€2.003

aynber
  • 22,380
  • 8
  • 50
  • 63
Arne De Belser
  • 121
  • 4
  • 13
  • Alright, thanks for the response! I knew about the possibility of the min and max because of some google searches. In the meanwhile I also found this plugin that does exactly what I want: [link](http://www.mojomarketplace.com/item/simple-variation-price-for-woocommerce-plugin) But it hasn't been updated in a while, and if you tell me that's it's not possible. I'm not sure. – Arne De Belser Jun 04 '18 at 14:11
  • 1
    Hello there @LoicTheAztec , I want to excuse myself for not having answered your question. I will have to implement your code tomorrow since I didn't have time to implement it today. I will let you know ASAP. I will now upvote & accept your answer since I'm sure this is going to work! I will comment beneath your solution once I have implemented it. Thanks for helping me out! – Arne De Belser Jun 05 '18 at 14:40

1 Answers1

4

It's possible to get the default variation price that is set in a variable product and to display it in shop and archives pages:

add_filter( 'woocommerce_variable_price_html', 'custom_variable_displayed_price', 10, 2 );
function custom_variable_displayed_price( $price_html, $product ) {
    // Only for archives pages
    if ( ! ( is_shop() || is_product_category() || is_product_tag() ) )
        return $price_html;

    // Searching for the default variation
    $default_attributes = $product->get_default_attributes();
    // Loop through available variations
    foreach($product->get_available_variations() as $variation){
        $found = true; // Initializing
        // Loop through variation attributes
        foreach( $variation['attributes'] as $key => $value ){
            $taxonomy = str_replace( 'attribute_', '', $key );
            // Searching for a matching variation as default
            if( isset($default_attributes[$taxonomy]) && $default_attributes[$taxonomy] != $value ){
                $found = false;
                break;
            }
        }
        // When it's found we set it and we stop the main loop
        if( $found ) {
            $default_variaton = $variation;
            break;
        } // If not we continue
        else {
            continue;
        }
    }

    // If no default variation is found we exit.
    if( ! isset($default_variaton) )
        $price_html;

    // Formatting the price
    if ( $default_variaton['display_price'] !== $default_variaton['display_regular_price'] && $product->is_on_sale()) {
        $price_html = '<del>' . wc_price($default_variaton['display_regular_price']) . '</del> <ins>' . wc_price($default_variaton['display_price']) . '</ins>';
    } else {
        $price_html = wc_price($default_variaton['display_price']);
    }
    return $price_html;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

Related answer: Display the default variation price and savings amount on Woocommerce 3

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Hello there, I have implemented your code since I felt a bit guilty that you took some time yesterday to help me out, and I haven't tested it today. I think I might be doing something wrong. This is how I implement the code [Implementation](https://i.gyazo.com/45cf10f02f91cf1d3874fc474f354703.png) And this is how the product is displayed in the shop: [shop view](https://i.gyazo.com/6f27d675e2e98100035a02686639be77.png) (1/2) – Arne De Belser Jun 05 '18 at 15:09
  • When I click that product, I automatically have the default selection selected, which costs $100 [standaard values](https://i.gyazo.com/9110b4ad9de25c3ecb9b33212a27511a.png) But the code is a great help, and this allows me to try and see for myself. I hope I can find out what's going wrong! (2/2) – Arne De Belser Jun 05 '18 at 15:10
  • Indeed, I've just implemented the code in functions.php of theme instead of child theme and it's actually running the filter. So it's working... I'll have to figure out why my child's functions.php is misbehaving! – Arne De Belser Jun 05 '18 at 15:17
  • I also wanted it to work on the homepage, so I added an is_front_page() check in the first check of the function. Everything is working great! Thank you very much @LoizTheAztec – Arne De Belser Jun 05 '18 at 15:23
  • @ArneDeBelser May be you can try to increase/discrease the hook priority which is `10` here. so try for example first `100` or more instead. Try to find if you are not yet already using `woocommerce_variable_price_html` or `woocommerce_get_price_html` hooks in your customizations. – LoicTheAztec Jun 05 '18 at 15:24
  • I just wrote a simple function that echo'ed something after the 'init' phase; And I couldn't find that anywhere on the page. Or isn't that a good check? I did something like [this](https://i.gyazo.com/03de0303cfded3dae9c88bb006d22173.png) and then searched my page for the string. Haha I will try your consult. Although I'm also trying it out in a test environment which I have setup today, so I probably haven't used the hooks before! :D – Arne De Belser Jun 05 '18 at 15:27
  • May be, it could be useful to test form the beginning if a default variation is set for the variable product like in [**the first function of this new answer**](https://stackoverflow.com/a/50707820/3730754), where a use partially a very similar code. – LoicTheAztec Jun 05 '18 at 21:03
  • I managed to solve the problem regarding the code not working in the child theme, the child theme wasn't active.. Consider some good nights rest & common sense the solution! I have also taken your advice into account and I've updated the code with the extra check , is it to make the code more performing? – Arne De Belser Jun 06 '18 at 07:30
  • @ArneDeBelser Is better to check that at start, to avoid running more heavier processes for nothing. – LoicTheAztec Jun 06 '18 at 07:32
  • Alright! I'm glad you told me that. I'm struggling my way through Woocommerce & WordPress development atm. I've read the developer handbook for themes & plugins from WordPress itself, and now I'm looking for usefull Woocommerce documentation. So far I've found [this](https://docs.woocommerce.com/wc-apidocs/index.html) to be the most usefull. Although, sometimes I see people do things, and when i try something similar, it doesn't workt for me! Haha. Well cheers man! Thanks for guiding me through a bit. – Arne De Belser Jun 06 '18 at 07:36