I’m working on a WooCommerce site with a lot of variable products and user roles that dynamically affect the price displayed. I need to create a snippet that I can add to my functions.php file to display variable product price ranges only for in stock items – and – display prices dependent on user role rules.
WooCommerce has 2 default functions that are at play:
1) Displays price range for ALL variations regardless of stock quantity. If an item has some variations “in stock” and some variations “out of stock” – then the shop page will display the “in stock” price range as a small gray piece of text with a strikethrough while the full price range is displayed normally.
2)WooCommerces default functions when it comes to dynamic pricing is that the discounted price is not displayed until the user adds the item to the cart.
What I have Tried:
This snippet worked alright for us until we added variable products where the variations all had the same price.
function patricks_custom_variation_price( $price, $product ) {
$target_product_types = array(
'variable'
);
if ( in_array ( $product->product_type, $target_product_types ) ) {
// if variable product return and empty string
return 'Multiple Sizes';
}
// return normal price
return $price;
}
add_filter('woocommerce_get_price_html', 'patricks_custom_variation_price', 10, 2);
CSS Didn’t Work at all for us:
.outofstock .price {
display:none
}
Yes, we have the “Do Not Display Out of Stock Items” checkbox checked within the settings
What I Need Your Help With
This is the most promising code snippet that might do the job, but it doesn’t take stock quantity into account nor user role. Can you help me re-arrange the pieces?
add_filter( 'woocommerce_variable_sale_price_html', 'wc_wc20_variation_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'wc_wc20_variation_price_format', 10, 2 );
function wc_wc20_variation_price_format( $price, $product ) {
// Main Price
$prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
$price = $prices[0] !== $prices[1] ? sprintf( __( 'From: %1$s', '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( __( 'From: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
if ( $price !== $saleprice ) {
$price = '<del>' . $saleprice . '</del> <ins>' . $price . '</ins>';
}
return $price;
}
My Crap Coding Attempt, Laugh Now
add_filter( 'woocommerce_variable_sale_price_html', 'wc_wc20_variation_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'wc_wc20_variation_price_format', 10, 2 );
function wc_wc20_variation_price_format( $price, $product ) {
// Display In Stock prices Only
$stockamount = $product->get_stock_quantity();
$price = $product->get_price_html();
$pricelabel = "";
if($stockamount == 0)
{
echo $pricelabel;
}
else
{
echo $price;
};
// Display Prices For User Role
add_filter('woocommerce_get_price', 'custom_price_WPA111772', 10, 2);
function custom_price_WPA111772($price, $product) {
if (!is_user_logged_in()) return $price;
}
return $price;
}
HELP