Untitled

 avatar
webwizards
plain_text
a month ago
4.2 kB
4
Indexable
add_action( 'wp_head', 'custom_b2bking_variation_table_initial_hide', 1 );
function custom_b2bking_variation_table_initial_hide() {
    if ( ! is_product() ) return;
    ?>
    <style id="b2bking-variation-table-initial-hide">
        .b2bking_tiered_price_table,
        .b2bking_product_variation_table_title {
            display: none !important;
        }
    </style>
    <?php
}
add_action( 'wp_footer', 'custom_b2bking_variation_table_swap', 100 );
function custom_b2bking_variation_table_swap() {
    if ( ! is_product() ) return;
    ?>
    <script>
    (function($){
        $(function(){
            // Group each title <p> + its table into a single wrapper, tagged with variation id.
            // Run once on DOM ready.
            function prepareTables(){
                var $tables = $('table.b2bking_tiered_price_table').not('.b2bking-grouped');
                if (!$tables.length) return;

                $tables.each(function(){
                    var $table = $(this);

                    // Extract variation id from class b2bking_productid_XXXXX
                    var variationId = null;
                    var classes = ($table.attr('class') || '').split(/\s+/);
                    for (var i = 0; i < classes.length; i++) {
                        var m = classes[i].match(/^b2bking_productid_(\d+)$/);
                        if (m) { variationId = m[1]; break; }
                    }
                    if (!variationId) return;

                    // The title <p> sits immediately before the table (with optional <br> before it).
                    var $title = $table.prevAll('p.b2bking_product_variation_table_title').first();

                    // Wrap title + table together
                    var $wrapper = $('<div class="b2bking-variation-pricing-table b2bking-grouped" data-variation-id="' + variationId + '"></div>');
                    if ($title.length) {
                        $title.before($wrapper);
                        $wrapper.append($title).append($table);
                    } else {
                        $table.before($wrapper);
                        $wrapper.append($table);
                    }

                    // Also pull along the trailing <br> if any, so spacing stays clean when hidden
                    var $br = $wrapper.next('br');
                    if ($br.length) $wrapper.append($br);
                });

                // Hide all by default, show a placeholder
                $('.b2bking-variation-pricing-table').hide();
                if (!$('.b2bking-variation-pricing-notice').length) {
                    $('.b2bking-variation-pricing-table').first().before(
                        '<p class="b2bking-variation-pricing-notice"><em>Seleziona una variazione per vedere il prezzo a quantità.</em></p>'
                    );
                }
				
				// At the end of prepareTables(), after everything is wrapped and hidden:
				$('#b2bking-variation-table-initial-hide').remove();
            }

            prepareTables();

            // In case Elementor renders the shortcode area later, retry briefly.
            var retries = 0;
            var retry = setInterval(function(){
                if ($('table.b2bking_tiered_price_table').not('.b2bking-grouped').length) {
                    prepareTables();
                }
                if (++retries >= 10) clearInterval(retry);
            }, 300);

            // Swap on variation selection
            $(document).on('found_variation', 'form.variations_form', function(e, variation){
                var $tables = $('.b2bking-variation-pricing-table');
                if (!$tables.length) return;
                $tables.hide();
                $tables.filter('[data-variation-id="' + variation.variation_id + '"]').show();
                $('.b2bking-variation-pricing-notice').hide();
            });

            $(document).on('reset_data', 'form.variations_form', function(){
                $('.b2bking-variation-pricing-table').hide();
                $('.b2bking-variation-pricing-notice').show();
            });
        });
    })(jQuery);
    </script>
    <?php
}
Editor is loading...
Leave a Comment