javascript Woocommerce -如何将变异数据传输到一个函数,以便在单个产品页面上实时显示价格计算公式?

uemypmqf  于 2023-01-16  发布在  Java
关注(0)|答案(1)|浏览(92)

我已经在这上面花了几天时间了,但是我对javascript还是个新手,所以我一直在努力让它工作,对于一个客户,当用户从下拉菜单中选择不同的产品变体和/或输入不同的产品长度时,我需要在单个产品页面上实时显示整个价格计算公式(数字字段)。为了得到变化价格,我一直在使用以下方法:

$( ".single_variation_wrap" ).on( "show_variation", function ( event, variation ) {
    var preis_variation = variation.display_regular_price;
    var gewicht_variation = variation.weight;
} );

我的问题是我无法访问下面函数中的这两个变量,我也尝试在开始时设置全局变量,但是这些变量没有用上面的代码更新。
这是完整的代码:

<script>
jQuery( function( $ ) {

    var preis_variation = 0;
    var gewicht_variation = 0;
    
    window.onload = function () {
                // initiate function grundkosten on dropdown change
        document.getElementById("pa_durchmesser").addEventListener('change', grundkosten);
                // initiate function grundkosten on number field change
        document.getElementById("wck_laenge").addEventListener('change', grundkosten);

               // get variation price and weight
        $( ".single_variation_wrap" ).on( "show_variation", function ( event, variation ) {
            var preis_variation = variation.display_regular_price;
            var gewicht_variation = variation.weight;
        } );
        
                // get additional information and display the calculation within a given html structure
        function grundkosten() {
            var uebertrag_laenge = document.getElementById("wck_laenge").value;
            document.getElementById("facon").innerHTML = preis_variation;
            document.getElementById("kosten_laenge").innerHTML = uebertrag_laenge;
            document.getElementById("total_grundkosten").innerHTML = uebertrag_laenge * 0.50;
            
            var uebertrag_goldpreis = document.getElementById("gold_preis_hidden").value;
            document.getElementById("goldpreis_laenge").innerHTML = uebertrag_laenge;
            document.getElementById("gewicht").innerHTML = gewicht_variation;
            document.getElementById("goldpreis_aktuell").innerHTML = uebertrag_goldpreis;
            document.getElementById("total_goldpreis").innerHTML = uebertrag_laenge * uebertrag_goldpreis;  
        }
    }
} );
</script>

我试着移动这个街区

$( ".single_variation_wrap" ).on( "show_variation", function ( event, variation ) {
    var preis_variation = variation.display_regular_price;
    var gewicht_variation = variation.weight;
} );

进入函数grundkosten希望获得对变量的访问权限,但没有任何更改。函数grundkosten仍然不允许访问preis_variation和gewicht_variation。
我将非常感谢一些帮助,我可以如何转移到函数grundkosten变量。谢谢很多提前!

sd2nnvve

sd2nnvve1#

解决上述问题的办法是对守则的这一部分作如下修改:

// get variation price and weight
    $( ".single_variation_wrap" ).on( "show_variation", function ( event, variation ) {
        preis_variation = variation.display_regular_price;
        gewicht_variation = variation.weight;
        
        grundkosten();
    } );

相关问题