WooCommerce复合产品+变体设置的jQuery问题

时间:2020-09-01 18:42:00

标签: php jquery wordpress woocommerce hook-woocommerce

对于这种类型的开发,我是超级新手,所以请在这里忍受。 一切都陷入不确定的境地,如果我尝试清空,那就是:if(typeof choices =='')。我收到此错误:“ JSON在位置0处出现意外令牌u”。 我可能没有足够的信息来帮助您,但目前为止就这么多,所以我只需要问您要解决的问题就可以告诉我。 在下面找到导致问题的代码部分:

function get_totals_for(type) {

        var result = 0;
        
        $(".component").each(function(index) {  
            console.log($(this));       
            var choices = $(this).data('choices');
            if(typeof choices == 'undefined') {
                result += 0;
            } else {
                choices = JSON.parse(choices);
                console.log(choices);
                switch(type) {
                    case 'kolhydrater':
                    result += ((choices.kolhydrater) ? choices.kolhydrater : 0);
                    break;
                    case 'fett':
                    result += ((choices.fett) ? choices.fett : 0);
                    break;
                    case 'kcal':
                    result += ((choices.kcal) ? choices.kcal : 0);      
                    break;
                    case 'protein':
                    result += ((choices.protein) ? choices.protein : 0);
                    break;
                }
            }
        }); 

        return (result).toFixed(2);
    }

0 个答案:

没有答案