im trying to figure out how to show all variation prices as text in single product page.
ex if product has 5 diffrent prices, 1 - 500, 2 - 900, 3 - 2100, 4 - 2500, 5 - 3000 i want it to show in text on all product pages.
as this:
VARIATIONS
1 - 500
2 - 900.
right now i do this manually, write them all up so ppl dont have to scroll down in the roll down menu to se what prices are but i want this done automaticly when i change the prices so i dont have to change this text everythime. i tryed diffrent hooks but couldnt find right one.
i tried this one but cant the hook to work and show it in product page
add_filter( 'woocommerce_get_price_html', 'custom_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'custom_price_format', 10, 2 );
function custom_price_format( $price, $product ) {
// 1. Variable products
if( $product->is_type('variable') ){
// 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;
}
}
// Get the default variation prices or if not set the variable product min prices
$regular_price = isset($default_variaton) ? $default_variaton['display_price']: $product->get_variation_regular_price( 'min', true );
$sale_price = isset($default_variaton) ? $default_variaton['display_regular_price']: $product->get_variation_sale_price( 'min', true );
}
// 2. Other products types
else {
$regular_price = $product->get_regular_price();
$sale_price = $product->get_sale_price();
}
// Formatting the price
if ( $regular_price !== $sale_price && $product->is_on_sale()) {
// Percentage calculation and text
$percentage = round( ( $regular_price - $sale_price ) / $regular_price * 100 ).'%';
$percentage_txt = __(' Save', 'woocommerce' ).' '.$percentage;
$price = '<del>' . wc_price($regular_price) . '</del> <ins>' . wc_price($sale_price) . $percentage_txt . '</ins>';
}
return $price;
}
Place the following function in your functions.php . You can replace get_price_html()
with $product->get_price();
or $product->get_regular_price();
$product->get_sale_price();
depending on how you want to show your data.
function list_variation_prices() {
global $product;
if($product->get_type() === 'variable'):
$variations = $product->get_children();
if($variations):
echo '<ul>';
foreach($variations as $variation):
$vproduct = wc_get_product($variation);
echo '<li>'.$vproduct->get_price_html().'</li>';
endforeach;
echo '</ul>';
endif;
endif;
}
/** Change priority to change where you want to show it. Default priorities
* @hooked woocommerce_template_single_title - 5
* @hooked woocommerce_template_single_rating - 10
* @hooked woocommerce_template_single_price - 10
* @hooked woocommerce_template_single_excerpt - 20
* @hooked woocommerce_template_single_add_to_cart - 30
* @hooked woocommerce_template_single_meta - 40
* @hooked woocommerce_template_single_sharing - 50
* @hooked WC_Structured_Data::generate_product_data() - 60
*/
add_action('woocommerce_single_product_summary','list_variation_prices',25);