需要在购物车中计算运输成本,但前提是购物车中有实物



在我的客户WooCommerce网站上,我们运行了一个脚本,强制在购物车页面上计算运输成本:

add_action('wp_head','prevent_proceed_to_checkout');
function prevent_proceed_to_checkout() { 
echo '  <script>
jQuery(function(){  
jQuery(".woocommerce-cart .wc-proceed-to-checkout a.checkout-button").on("click",function(e){
var _this = this;
alert("Bitte berechne die Versandkosten!");
e.preventDefault();
jQuery(".shipping-calculator-form .button").on("click",function(){
jQuery(_this).off();
});
});
});
</script>';
}

我需要对此进行更改,以便在购物车中没有实物产品的情况下不执行。我尝试使用javascript来检查.woocommerce shipping totals-class是否在dom中,但我提出的解决方案没有成功。

有什么想法吗?

我现在通过在onclick函数中添加一个if语句来解决这个问题,该函数基本上是检查".woocommerce shipping totals"-class是否在点击结账按钮后立即显示在页面上。这是我添加的代码:(就在警报之前(

var shipping = document.getElementsByClassName("woocommerce-shipping-totals").length;      
if (shipping > 0) {

最新更新