1

There are many similar questions like this already around. But I have not been able to find a single solution for this question specifically.

I, like many others, are using this snippet to show "As Low As" or "From" in place of standard price range that is displayed:

https://businessbloomer.com/disable-variable-product-price-range-woocommerce/

/**
 * @snippet       Variable Product Price Range: "From: $$$min_price"
 * @how-to        Get CustomizeWoo.com FREE
 * @sourcecode    https://businessbloomer.com/?p=275
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.5.4
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

add_filter( 'woocommerce_variable_price_html', 'bbloomer_variation_price_format_min', 9999, 2 );

function bbloomer_variation_price_format_min( $price, $product ) {
   $prices = $product->get_variation_prices( true );
   $min_price = current( $prices['price'] );
   $price = sprintf( __( 'From: %1$s', 'woocommerce' ), wc_price( $min_price ) );
   return $price;
}

And it works great, except for that I need this code to take into account any "Out Of Stock" variables.

As of now, it will only show the lowest variable price if it's "In Stock".

I need it to display the lowest price even if that current price variable is currently "Out Of Stock".

  • Hey @Chronic_Built_Support. Welcome to SO. You should probably note somewhere in the question that the language you're using is PHP. You did well to put it as a tag, but I would put it into the question and even into the title. – bballdave025 Dec 04 '19 at 23:58
  • Thanks for the tip! –  Dec 05 '19 at 23:05
  • Note my comments under my answer. Also, it might help you to look at a couple answers about getting PHP errors to display, so you can see where something might be going wrong - [answer 1](https://stackoverflow.com/a/21429652/6505499) and [answer 2](https://stackoverflow.com/a/21427437/6505499). – bballdave025 Dec 06 '19 at 00:34
  • Thanks. I am checking those out too. –  Dec 08 '19 at 19:10

1 Answers1

0

I don't have WooCommerce installed, so you should probably wait for and accept an answer that actually gives executable code. However, to give you an idea of what you'll need (and give you a chance to show your own efforts), I'll include the kind of thing you'll need to check for. My guess is that you'll check for it before doing anything else inside your bbloomer_variation_price_format_min method, but the exact point at which you check for "Out of Stock" might be different. Anyway, here's the idea.

You'll need to use some kind of method that finds out if the product is "Out of Stock". This will likely be an if statement. Actually, a quick search led me to a specific if statement, so you might have executable code here, after all.

From another SO answer, it seems that the method is get_stock_quantity(). To be a little more specific, you'll perform a test such as

if ( $product->get_stock_quantity() == 0 )

or, to make the code even more readable.

$product_is_out_of_stock = ( $product->get_stock_quantity() == 0 );
if ( $product_is_out_of_stock == True ) 

Note that I've only noted which if statement should be used; you'll need to fill out the code for the if condition and the else condition, probably surrounding each with brackets, { and }.

Do that check. If you find that the product is out of stock, return a string that tells the customer so. If it's not out of stock, continue with your function as it is and return the string that you've sprintf-ed into the $price variable.

I hope this gets you on the right track. If you have trouble getting code that will execute, add an edit showing your attempts to check if something is Out of Stock.

NOTE: I didn't try to run this. It's more of a conceptual answer.

Edit

It sounds like you've worked on it. Let me be a little more specific with the code you might use. I'll post the code, then I'll hopefully get some time soon to try it with the program downloaded.

/**
 * @snippet       Variable Product Price Range: "From: $$$min_price"
 * @how-to        Get CustomizeWoo.com FREE
 * @sourcecode    https://businessbloomer.com/?p=275
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.5.4
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

add_filter( 'woocommerce_variable_price_html', 'bbloomer_variation_price_format_min', 9999, 2 );

function bbloomer_variation_price_format_min( $price, $product ) {
   $product_is_out_of_stock = ( $product->get_stock_quantity() == 0 );
   if ( $product_is_out_of_stock == True ) {
      $price = sprintf( 'Product, %s , is Out of Stock.', $product );
      return $price;
   }
   else {
      $prices = $product->get_variation_prices( true );
      $min_price = current( $prices['price'] );
      $price = sprintf( __( 'From: %1$s', 'woocommerce' ), wc_price( $min_price ) );
      return $price;
   }
}

Try that out for what you have now. As for cloning a database, I'm probably not the best to help you with that, now. However, it would be useful to know what you're using. From a search online for the "__" operator, it seems that you're either using wordpress or CakePHP (or another PHP framework.) Details on that would be good to add for me or for whomever ends up helping you.

bballdave025
  • 1,347
  • 1
  • 15
  • 28
  • Thank you. I understand where you're coming from kinda. I think I should add more to the post to show what I need it to do. I think I maybe didn't explain it correctly. I need the price displayed to show the lowest price no matter if it is in or out of stock. For some reason, this snippet only shows the lowest price of variables that are in stock. And so I need it to show the lowest price out of all variables (out of stock and in stock). If this is what you're referring to, then thank you. Maybe I just don't get it. Thank you so much either way. –  Dec 05 '19 at 23:08
  • I will try in now. –  Dec 05 '19 at 23:10
  • I think you're understanding. I would show (in your question) and example of running the code where you actually get a price. Then, I would show the output when you have realized that you're "Out of Stock." I congratulate you for taking the time to learn how to post a good question. It makes it so much easier to help you! – bballdave025 Dec 06 '19 at 00:26
  • By the way... If you can't get it to work, post the update with your attempts. (If you have a website with PHP underneath - which I'm guessing is the case - feel free to include screenshots of the web page.) I'll keep an eye on this question to see how it goes for you. If you're still having problems, I'll try downloading WooCommerce and seeing if I can replicate your issue. Good luck! – bballdave025 Dec 06 '19 at 00:28
  • Hey. I am sorry, I am just not getting this at all. It is not you, it's me. I just need to do some more background and research to figure out how to even attempt this. I need to clone a server too. –  Dec 08 '19 at 19:09
  • I updated my answer a bit to include some code that I hope should execute. I also let you know some information you might want to add to your question as you either continue with this question or ask other questions. Good luck! – bballdave025 Dec 09 '19 at 21:23