Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_action( 'woocommerce_after_single_product_summary', 'add_custom_script', 5 );
- function add_custom_script(){
- global $product;
- if( $product->get_type() === 'variable' ){
- ?>
- <script type="text/javascript">
- // === 171 ===
- dlnAdjust();
- function dlnAdjust(){
- let entrySummary = document.querySelector( '.summary.entry-summary' );
- let price = entrySummary.querySelector( 'p.price' );
- let form = entrySummary.querySelector( 'form' );
- let html = price.innerHTML;
- html = '<span class="box-for-range-price">' + html + '</span><span class="box-for-single-price"></span>';
- price.innerHTML = html;
- let rangeBox = price.querySelector( '.box-for-range-price' );
- let singleBox = price.querySelector( '.box-for-single-price' );
- let singleVar = entrySummary.querySelector( '.woocommerce-variation.single_variation' );
- html = singleVar.outerHTML;
- html = '<div class="box-for-single-variation" style="display: none;">' + html + '</div>';
- singleVar.outerHTML = html;
- form.addEventListener( 'change', formChange );
- formChange();
- function formChange(){
- let chooseSel = fChooseSel();
- let variations = form.getAttribute( 'data-product_variations' );
- variations = eval( variations );
- let chosenVar = fChosenVar( chooseSel, variations );
- if( chosenVar !== undefined ){
- let html = chosenVar[ 'price_html' ];
- rangeBox.style.display = 'none';
- singleBox.innerHTML = html; }
- else{
- singleBox.innerHTML = '';
- rangeBox.style.display = 'inline'; }
- } // formChange
- function fChooseSel(){
- let selects = form.querySelectorAll( 'select' );
- let choose = [];
- selects.forEach( function ( for_select ){
- let name = for_select.getAttribute( 'data-attribute_name' );
- let vValue = for_select.value;
- let elem = { 'name': name, 'value': vValue }
- choose.push( elem );
- });
- return choose;
- }
- function fChosenVar( chooseSel, variations ){
- for( let forVar of variations ){
- let attributes = forVar[ 'attributes' ];
- let check = true;
- for( let forSel of chooseSel ){
- let name = forSel[ 'name' ]; let vValue = forSel[ 'value' ];
- if( attributes[ name ] != vValue){ check = false; break; }}
- if( check ){ return forVar; }}
- }
- } // dlnAdjust
- </script>
- <?php
- }
- }
Add Comment
Please, Sign In to add comment