Created
July 7, 2021 09:57
-
-
Save jorpdesigns/2cfbb4f34654ac32e11ba4368dd41baa to your computer and use it in GitHub Desktop.
Snippet to display dynamic prices on WooCommerce product pages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // You can add this function to any hook listed here - https://www.businessbloomer.com/woocommerce-visual-hook-guide-single-product-page/ | |
| function woocommerce_total_product_price() { | |
| global $woocommerce, $product; | |
| if( $product->is_type( 'simple' ) ){ | |
| echo '<div class="total-cost-wrapper">Total Cost <span class="dynamic-price">' . get_woocommerce_currency_symbol() . $product->get_price() . '</span></div>'; | |
| ?> | |
| <script> | |
| jQuery(function($){ | |
| var price = <?php echo $product->get_price(); ?>, | |
| currency = '<?php echo get_woocommerce_currency_symbol(); ?>'; | |
| $('[name=quantity]').change(function(){ | |
| if (!(this.value < 1)) { | |
| var product_total = parseFloat(price * this.value); | |
| $('span.dynamic-price').html( currency + product_total.toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ",")); | |
| } | |
| }); | |
| }); | |
| </script> | |
| <?php | |
| } | |
| if( $product->is_type( 'variable' ) ){ | |
| echo '<div class="regular-cost-wrapper"></div>'; | |
| echo '<div class="total-cost-wrapper">Total Cost <span class="dynamic-price"></span></div>'; | |
| $variations_data =[]; | |
| $variations_price_data =[]; | |
| foreach ($product->get_available_variations() as $variation ) { | |
| $variations_data[$variation['variation_id']] = $variation['display_price']; | |
| $variations_price_data[$variation['variation_id']] = $variation['display_regular_price']; | |
| } | |
| ?> | |
| <script> | |
| jQuery(function($) { | |
| var jsonData = <?php echo json_encode($variations_data); ?>, | |
| jsonPriceData = <?php echo json_encode($variations_price_data); ?>, | |
| inputVID = 'input.variation_id', | |
| currency = '<?php echo get_woocommerce_currency_symbol(); ?>'; | |
| $('input').change( function(){ | |
| if( '' != $(inputVID).val() ) { | |
| var vid = $(inputVID).val(), | |
| vprice = '', | |
| vregularprice = ''; | |
| $.each( jsonData, function( index, price ) { | |
| if( index == $(inputVID).val() ) { | |
| vprice = price; | |
| } | |
| }); | |
| $.each( jsonPriceData, function( index, regularprice ) { | |
| if( index == $(inputVID).val() ) { | |
| vregularprice = regularprice; | |
| } | |
| }); | |
| var product_total = parseFloat(vprice * $('[name=quantity]').val()); | |
| $('span.dynamic-price').html( currency + product_total.toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ",")); | |
| if ( vprice != vregularprice ) { | |
| var regular_product_total = parseFloat(vregularprice * $('[name=quantity]').val()); | |
| $('.regular-cost-wrapper').html( '<div><span>SALE!</span><span class="regular-price">' + currency + regular_product_total.toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ",") + '</span></div>'); | |
| } else { | |
| $('.regular-cost-wrapper').empty(); | |
| } | |
| } else { | |
| $('span.dynamic-price').html( currency + '0.00'); | |
| $('.regular-cost-wrapper').empty(); | |
| } | |
| }); | |
| $('[name=quantity]').change(function(){ | |
| if (!(this.value < 1)) { | |
| var vid = $(inputVID).val(), | |
| vprice = '', | |
| vregularprice = ''; | |
| $.each( jsonData, function( index, price ) { | |
| if( index == $(inputVID).val() ) { | |
| vprice = price; | |
| } | |
| }); | |
| $.each( jsonPriceData, function( index, regularprice ) { | |
| if( index == $(inputVID).val() ) { | |
| vregularprice = regularprice; | |
| } | |
| }); | |
| var product_total = parseFloat(vprice * $('[name=quantity]').val()); | |
| $('span.dynamic-price').html( currency + product_total.toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ",")); | |
| if ( vprice != vregularprice ) { | |
| var regular_product_total = parseFloat(vregularprice * $('[name=quantity]').val()); | |
| $('.regular-cost-wrapper').html( '<div><span>SALE!</span><span class="regular-price">' + currency + regular_product_total.toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ",") + '</span></div>'); | |
| } else { | |
| $('.regular-cost-wrapper').empty(); | |
| } | |
| } | |
| }); | |
| }); | |
| </script> | |
| <?php | |
| } | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment