To display the default selected variation regular price and sale price on the WooCommerce archive Shop or Category pages, you’ll need to modify the code to identify and display the prices of the default selected variation.
Suppose you are a WordPress/ WooCommerce developer or business owner and looking for a WooCommerce function to display the default selected variation’s regular price and sale price on the shop/category page. Then this article is important for you to resolve your problem.
Before Layout/Output: Refer the below screenshot
Display Default Variation Regular and Sale Price on Archive Page (shop, category, etc.)
Place the below code in your child theme functions.php file or a custom plugin dedicated to custom WooCommerce functionalities.
Always test any changes on a staging site or make a backup before applying them to a live website.
Also Read: Create WooCommerce custom product type programmatically
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
function wc_codevyne_custom_variable_price( $price, $product ) { if ( $product->is_type( 'variable' ) ) { $default_attributes = $product->get_default_attributes(); foreach($product->get_available_variations() as $variation_values ){ foreach($variation_values['attributes'] as $key => $attribute_value ){ $attribute_name = str_replace( 'attribute_', '', $key ); $default_value = $product->get_variation_default_attribute($attribute_name); if( $default_value == $attribute_value ){ $is_default_variation = true; } else { $is_default_variation = false; break; // Stop this loop to start next main lopp } } if( $is_default_variation ){ $variation_id = $variation_values['variation_id']; if ( ! empty( $variation_id ) ) { $variation = wc_get_product( $variation_id ); if ( $variation ) { $pricereg = $variation->get_regular_price(); $saleprice = $variation->get_sale_price(); if ( $pricereg !== $saleprice ) { $price = ' <del> '.wc_price($pricereg) .' </del><ins style="font-weight:600;"> '. wc_price($saleprice).' </ins> '; }else{ $price ='<ins style="font-weight:600;"> '. wc_price($pricereg).' </ins> '; } } } break; // Stop the main loop } } } return $price; } add_filter( 'woocommerce_get_price_html', 'wc_codevyne_custom_variable_price', 10, 2 ); |
After Layout/Output: Default Variation Regular and Sale Price are Showing now below the screenshot
Let’s see how this code works:
- It hooks into the woocommerce_get_price_html filter, which allows us to modify the price HTML displayed on product archive pages.
- Check if the product is of type “variable”.
- Retrieves available variations and their regular and sale prices.
- Constructs the HTML markup to display the regular price and sale price (if applicable) for variable products.
Are you want to get implementation help, or modify or extend the functionality of this script?
A Tutorialswebsite Expert can do it for you.
Display Variation Minimum Sale Price and Maximum Regular Price on Archive Page
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
function wc_codevyne_custom_variable_price( $price, $product ) { if ( $product->is_type( 'variable' ) ) { $min_reg_price = $product->get_variation_regular_price('max'); $min_sale_price = $product->get_variation_sale_price('min'); if ( $min_reg_price !== $min_sale_price ) { $price = ' <del> '.wc_price($min_reg_price) .' </del><ins style="font-weight:600;"> '. wc_price($min_sale_price).' </ins> '; } } return $price; } add_filter( 'woocommerce_get_price_html', 'wc_codevyne_custom_variable_price', 10, 2 ); |
This code snippet filters the display of the variable product price HTML. It gathers all visible variations’ regular and sale prices and finds the minimum sale and maximum regular price.
This code will display the regular price and sale price range for variable products on archive pages (shop, category, etc.). You can place this code snippet in your theme’s functions.php file or a custom plugin created for this purpose. As always, ensure you test this code in a staging environment or make a backup before implementing it on a live site.
Wrapping Words
Thanks for reading 🙏, I hope you found the How to Display Default Variation Regular and Sale Price in WooCommerce tutorial helpful for your project. Keep learning! If you face any problems – I am here to solve your problems.
FAQs
If you want to display the sale and regular prices for the default selected variation of variable products on the WooCommerce archive pages (shop, category, etc.), you can utilize the above code snippets.
woocommerce_get_price_html filter allows us to modify the price HTML displayed on product archive pages. display variable product’s Regular & Sale Prices in the loop you can use “Display Default Variation Regular and Sale Price on Archive Page (shop, category, etc.)” code Snippets.
Yes, It is possible to display only the maximum regular and sale prices using $product->get_variation_regular_price(‘max’); and $product->get_variation_sale_price(‘max’);
Suggested Articles
- How to check if a product is in a WooCommerce order
- WooCommerce: Display Product Discount After Coupon Applied in Cart Summary @ Checkout
- How to Get Best Selling Products in Woocommerce Programmatically
- How to Display Sale Price End Date in woocommerce
Pradeep Maurya is the Professional Web Developer & Designer and the Founder of “Tutorials website”. He lives in Delhi and loves to be a self-dependent person. As an owner, he is trying his best to improve this platform day by day. His passion, dedication and quick decision making ability to stand apart from others. He’s an avid blogger and writes on the publications like Dzone, e27.co