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.