Okay
  Public Ticket #3114351
Hide Variable price range ​​
Closed

Comments

  •  6
    bewconcept started the conversation

    Hello Team, 

    and great work with this plugin, been looking and awaiting a long time for such a jewel :)

    (the website i'm working on is using savoy theme, latest version)

    I have a small issue though, I made a rule to display the prices without VAT for B2B users, 

    + a rule to NOT display some variations to Guest and B2C. (price hide)

    and when I do so, the variable products would show by default the price range which I didn't want to, so I looked around and found a snippet to achieve what I want.

    Works almost everywhere but on the related-sells product block display, it doesn't show a the price when it's a variable product. (price is shown when simple product) 

    (and also when I call a product with this shortcode, no prices show up.)

    [product id="xxx"]
    

    Here's my snippet : 

    /* B2B King : hide Price range on variable products 
    =============================================================== */
    function iconic_format_price_range( $price, $from, $to ) {
        return ' '.wc_price( $from );
    } 
    add_filter( 'woocommerce_format_price_range', 'iconic_format_price_range', 10, 3 );
    /* WOO Remove Price Range for variable products.  
     * https://annaschneider.me/blog/hide-the-price-range-for-woocommerce-variable-products/
    // =============================================================== */
    add_filter( 'woocommerce_variable_sale_price_html', 'detect_variation_price_format', 10, 2 );
    add_filter( 'woocommerce_variable_price_html', 'detect_variation_price_format', 10, 2 );
    function detect_variation_price_format( $price, $product ) {
    // Main Price
    $prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
    if ($prices[0] !== $prices[1] && is_product()) {
    $price = $prices[0] !== $prices[1] ? sprintf( __( '', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
    }
    // Sale Price
    $prices = array( $product->get_variation_regular_price( 'min', true ), $product->get_variation_regular_price( 'max', true ) );
    sort( $prices );
    $saleprice = $prices[0] !== $prices[1] ? sprintf( __( '', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
    if ( $price !== $saleprice ) {
    $price = '<del>' . $saleprice . '</del> <ins>' . $price . '</ins>';
    }
    return $price;
    } 
    // Move Variations price above variations to have the same template even if variations prices are the same 
    remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation', 10 );
    add_action( 'woocommerce_before_variations_form', 'woocommerce_single_variation', 10 );

    if you could kindly give an eye on this, i guess there's a parameter for related-product to follow the same price display than the rest of the shop, but it's getting out of my skills to solve this alone. 

    Many thanks in advance, 
    Robin. 

    Attached files:  Screen_ 2022-08-30 à 04.09.20.png
      Screen_ 2022-08-30 à 04.10.59.png

  •  1,881
    WebWizards replied

    Hi Robin,

    Thank you for purchasing our plugin and for the kind words!


    I tried to add your snippet and to run some tests and to look at the results.


    First of all, the snippet seems to run into some problems with the prices cache. Let me show you what I mean:

    At first, the range is hidden correctly on the single product page:

    7212486771.png

    Range is also hidden in related products for variable items:

    8207188498.png


    But then if I go to the shop page, on the shop page I can see the minimum price (of the lowest price variation):

    5158953004.png

    Is this what you want to show instead of the range? the minimum price?


    But now, after I go to the shop page, now this minimum price shows everywhere, including on the single product page, where previously it did not show:

    9697977347.png



    So my question is, instead of the range, what you do want to show:

    - On the single product page you want to show nothing?

    - In related products and shop page and shortcodes you want to show the minimum price (lowest price variation?)


  •  1,881
    WebWizards replied

    A suggestion for you. Maybe simply replace the range with 'from $price' - if you replace this everywhere, it's quite reliable, it doesn't run into issues with the price cache, and it also hides the range (replaces the range with the minimum price).

    The code would be:

    function iconic_format_price_range( $price, $from, $to ) {
        return 'from '.wc_price($from);
    } 
    add_filter( 'woocommerce_format_price_range', 'iconic_format_price_range', 10, 3 );
    // Move Variations price above variations to have the same template even if variations prices are the same 
    remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation', 10 );
    add_action( 'woocommerce_before_variations_form', 'woocommerce_single_variation', 10 );
    

    And you will have this 'from X' display everywhere:

    Single product:

    5710920769.png

    Relted products:

    9476963253.png

    Shop page:

    5361750590.png


  •  6
    bewconcept replied

    Hey there, 

    In fact, I just need ALL variations, to NOT show the "from $price", I just want prices to show the first variation price, but just by it self , like if it was a simple product price. i.e, a variable product with a 25€ and a 75€ product, will only display 25€. (like if no variation would exist. but below, still the dropdown is here be able to select a variation).

    We're almost there with your last snippet
    your function is working indeed, BUT when your on the single product page, in case of a variable product, it displays the price twice (the from  $Price + the default variation price, I guess) 


    Attached files:  Screen_ 2022-08-31 à 22.56.51.png

  •  1,881
    WebWizards replied

    Hi there,

    Apologies for any delay in getting back to you, we've been seeing a high volume of tickets this week and we're still catching up,


    In that case what I would do is set the snippet to this, to remove that 'from':

    function iconic_format_price_range( $price, $from, $to ) {
        return wc_price($from);
    } 
    add_filter( 'woocommerce_format_price_range', 'iconic_format_price_range', 10, 3 );
    // Move Variations price above variations to have the same template even if variations prices are the same 
    remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation', 10 );
    add_action( 'woocommerce_before_variations_form', 'woocommerce_single_variation', 10 );
    


    And we have one more thing to do, to hide it completely on the single product page. I think that's best accomplished with CSS, because if we try to do it via PHP snippets we run into issues with the cache.

    This depends on the theme. On my local site with the Storefront theme, this CSS seems to work well:

    .single .summary > .price {
        display: none;
    }
    


    If this doesn't work on your site, can you give me a link to a product page ? I can modify the CSS depending on your theme, but I'd need to see the page.


    Kind regards,

    Stefan

  •  6
    bewconcept replied

    Hello Stefan, 

    No worries for the delays. 

    Regarding the Theme I use, as I said, i'm using the same theme as in your modern Demo : SAVOY theme. 

    as per your solution, the CCS Code to hide variation price is not working for my situation ; simply because when you select a variation, then the variation price is hidden !

    On Single product for Variable product only,

    I just need to display, on Variable product on single page : 
    The Default variation price, and the if you select 2OO ml, it's show the price of the 200ml... 

    https://josianelaureparis.com/produit/shampooing-d-tox/

    i have switched back to my snippet for now, as the website is currently live



  •  1,881
    WebWizards replied

    I am trying some CSS on your site, and this is what I'm seeing.

    First, I tried adding a range price for testing, I think that $price - $price there is where it would be, right?

    6761182432.png


    Then if I add this CSS:

    .single #nm-summary-pin .price:nth-child(2) {
        display: none;
    }


    That makes it go away:

    1249663883.png


    Could that help?

  •  6
    bewconcept replied

    Waaaaah,  u're a pro my friend ! 

    I guess if i had searched a bit deeper, i might have found the one..., sorry for the bugging.
    And thanks for your kind help

    Cheers, Robin

  •  1,881
    WebWizards replied

    Hi Robin,

    Glad to hear that's working! If there's anything else I can help with, please do let me know.


    Also, I have a small favor to ask. If the plugin is useful to you and if you think it deserves it, I would really appreciate it if you could leave a review here on on WordPress page: https://wordpress.org/plugins/b2bking-wholesale-for-woocommerce/#reviews  

    We are trying to grow the plugin and we need all the help we can get.


    Kind regards,

    Stefan



  •  6
  •  1,881
    WebWizards replied

    Thank you so much, really appreciate it 🙏 🙏 

    Have a great day,

  •  6
    bewconcept replied

    Hello Stefan, 

    Sorry, I just found out that on single product (non variable) with the function + CSS,  the price single price get hidden as well... ending up with a single product page without price... (only for single, variable is ok) ...

    That what i faced, if you hide the range variation prices by CSS it hides also the single product's price... 

    > can we target CSS to apply only on variable products ?

    Attached files:  Screen_ 2022-09-14 à 00.37.49.png

  •  1,881
    WebWizards replied

    Oh, I'm sorry, I didn't see that issue.

    I believe you can resolve it by changing your snippet to this one:

    .single .product-type-variable #nm-summary-pin .price:nth-child(2){
        display:none;
    }
    

    It should then target only variable products.


    Seems to work in my tests, but please let me know.

  •  6
    bewconcept replied

    wow, 
    Yep you nailed it again...!

       thanks a bunch