I have used the code snippet below to get 2 types of prices but it ruins everything when I add variable pricing.
How do I keep the above pricing to be shown but remove the chaos of pricing with the drop-down menu?
Ideally, I would like it to show the two pricing options and then when you choose one of the options in the dropdown menu I would like to show the pricing again of that option.
function edit_price_display($price, $instance) {
global $product;
if(is_singular('product')) {
$price = $product->price;
$price_incl_tax = $price + round($price * ( 25 / 100 ), 2);
$price_incl_tax = number_format($price_incl_tax, 2, ",", ".");
$price = number_format($price, 2, ",", ".");
$display_price = '<span class="price">';
$display_price .= '<span class="amount">' . $price .'<small class="woocommerce-price-suffix"> kr (exkl.moms) </small></span>';
$display_price .= '<br>';
$display_price .= '<span class="amount"> (' . $price_incl_tax .'<small class="woocommerce-price-suffix"> kr inkl.moms </small></span>)';
$display_price .= '</span>';
echo $display_price;
} else {
echo $price;
}
}
add_filter('woocommerce_get_price_html', 'edit_price_display', 10, 2);
My Answer:
To remove the chaos of pricing with the drop-down menu for variable products in WooCommerce, you can modify the code snippet to display the two pricing options without the dropdown menu. Here's an updated version of the code snippet:
php
function edit_price_display($price, $instance) {
global $product;
if(is_singular('product')) {
$price = $product->price;
$price_incl_tax = $price + round($price * ( 25 / 100 ), 2);
$price_incl_tax = number_format($price_incl_tax, 2, ",", ".");
$price = number_format($price, 2, ",", ".");
$display_price = '';
$display_price .= '' . $price .' kr (exkl.moms) ';
$display_price .= '
';
$display_price .= ' (' . $price_incl_tax .' kr inkl.moms )';
$display_price .= '';
echo $display_price;
} else {
echo $price;
}
}
add_filter('woocommerce_get_price_html', 'edit_price_display', 10, 2);
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 10);
In this updated code snippet, we are removing the default WooCommerce price display function for single product pages using the `remove_action` function. This will prevent the dropdown menu for variable products and only display the two pricing options as specified in the `edit_price_display` function.
Make sure to add this code to your theme's `functions.php` file or a custom plugin. This should help you achieve the desired pricing display for variable products in WooCommerce.
Rate this post
5 of 5 based on 7174 votesComments